from optparse import OptionParser
from os import environ
from sys import argv, exit, stdin, stdout
-from pithos.lib.client import Client, Fault
from datetime import datetime
+from lib.client import Pithos_Client, Fault
+from lib.util import get_user, get_auth, get_server, get_api
import json
import logging
import time as _time
import os
-#DEFAULT_HOST = 'pithos.dev.grnet.gr'
-DEFAULT_HOST = '127.0.0.1:8000'
-DEFAULT_API = 'v1'
-
_cli_commands = {}
def cli_command(*args):
def __init__(self, name, argv):
parser = OptionParser('%%prog %s [options] %s' % (name, self.syntax))
parser.add_option('--host', dest='host', metavar='HOST',
- default=DEFAULT_HOST, help='use server HOST')
+ default=get_server(), help='use server HOST')
parser.add_option('--user', dest='user', metavar='USERNAME',
- default=_get_user(),
+ default=get_user(),
help='use account USERNAME')
parser.add_option('--token', dest='token', metavar='AUTH',
- default=_get_auth(),
+ default=get_auth(),
help='use account AUTH')
parser.add_option('--api', dest='api', metavar='API',
- default=DEFAULT_API, help='use api API')
+ default=get_api(), help='use api API')
parser.add_option('-v', action='store_true', dest='verbose',
default=False, help='use verbose output')
parser.add_option('-d', action='store_true', dest='debug',
val = getattr(options, key)
setattr(self, key, val)
- self.client = Client(self.host, self.token, self.user, self.api, self.verbose,
+ self.client = Pithos_Client(self.host, self.token, self.user, self.api, self.verbose,
self.debug)
self.parser = parser
self.args = args
-
+
+ def _build_args(self, attrs):
+ args = {}
+ for a in [a for a in attrs if getattr(self, a)]:
+ args[a] = getattr(self, a)
+ return args
+
def add_options(self, parser):
pass
parser.add_option('-l', action='store_true', dest='detail',
default=False, help='show detailed output')
parser.add_option('-n', action='store', type='int', dest='limit',
- default=1000, help='show limited output')
+ default=10000, help='show limited output')
parser.add_option('--marker', action='store', type='str',
dest='marker', default=None,
help='show output greater then marker')
dest='if_unmodified_since', default=None,
help='show output if not modified since then')
parser.add_option('--until', action='store', dest='until',
- default=False, help='show metadata until that date')
+ default=None, help='show metadata until that date')
parser.add_option('--format', action='store', dest='format',
default='%d/%m/%Y', help='format to parse until date')
self.list_containers()
def list_containers(self):
- params = {'limit':self.limit, 'marker':self.marker}
- headers = {'IF_MODIFIED_SINCE':self.if_modified_since,
- 'IF_UNMODIFIED_SINCE':self.if_unmodified_since}
+ attrs = ['limit', 'marker', 'if_modified_since',
+ 'if_unmodified_since']
+ args = self._build_args(attrs)
+ args['format'] = 'json' if self.detail else 'text'
- if self.until:
+ if getattr(self, 'until'):
t = _time.strptime(self.until, self.format)
- params['until'] = int(_time.mktime(t))
+ args['until'] = int(_time.mktime(t))
- l = self.client.list_containers(self.detail, params, headers)
+ l = self.client.list_containers(**args)
print_list(l)
def list_objects(self, container):
#prepate params
params = {}
- attrs = ['limit', 'marker', 'prefix', 'delimiter', 'path', 'meta']
- for a in [a for a in attrs if getattr(self, a)]:
- params[a] = getattr(self, a)
+ attrs = ['limit', 'marker', 'prefix', 'delimiter', 'path',
+ 'meta', 'if_modified_since', 'if_unmodified_since']
+ args = self._build_args(attrs)
+ args['format'] = 'json' if self.detail else 'text'
if self.until:
t = _time.strptime(self.until, self.format)
- params['until'] = int(_time.mktime(t))
+ args['until'] = int(_time.mktime(t))
- headers = {'IF_MODIFIED_SINCE':self.if_modified_since,
- 'IF_UNMODIFIED_SINCE':self.if_unmodified_since}
container, sep, object = container.partition('/')
if object:
return
detail = 'json'
#if request with meta quering disable trash filtering
show_trashed = True if self.meta else False
- l = self.client.list_objects(container, detail, headers,
- include_trashed = show_trashed, **params)
+ l = self.client.list_objects(container, **args)
print_list(l, detail=self.detail)
@cli_command('meta')
class Meta(Command):
syntax = '[<container>[/<object>]]'
- description = 'get the metadata of an account, a container or an object'
+ description = 'get account/container/object metadata'
def add_options(self, parser):
parser.add_option('-r', action='store_true', dest='restricted',
default=False, help='show only user defined metadata')
parser.add_option('--until', action='store', dest='until',
- default=False, help='show metadata until that date')
+ default=None, help='show metadata until that date')
parser.add_option('--format', action='store', dest='format',
default='%d/%m/%Y', help='format to parse until date')
parser.add_option('--version', action='store', dest='version',
def execute(self, path=''):
container, sep, object = path.partition('/')
- if self.until:
+ args = {'restricted':self.restricted}
+ if getattr(self, 'until'):
t = _time.strptime(self.until, self.format)
- self.until = int(_time.mktime(t))
+ args['until'] = int(_time.mktime(t))
+
if object:
meta = self.client.retrieve_object_metadata(container, object,
self.restricted,
self.version)
elif container:
- meta = self.client.retrieve_container_metadata(container,
- self.restricted,
- self.until)
+ meta = self.client.retrieve_container_metadata(container, **args)
else:
- meta = self.client.account_metadata(self.restricted, self.until)
+ meta = self.client.retrieve_account_metadata(**args)
if meta == None:
print 'Entity does not exist'
else:
description = 'create a container'
def execute(self, container, *args):
- headers = {}
meta = {}
for arg in args:
key, sep, val = arg.partition('=')
meta[key] = val
- ret = self.client.create_container(container, headers, **meta)
+ ret = self.client.create_container(container, **meta)
if not ret:
print 'Container already exists'
syntax = '<container>[/<object>]'
description = 'delete a container or an object'
+ def add_options(self, parser):
+ parser.add_option('--until', action='store', dest='until',
+ default=None, help='remove history until that date')
+ parser.add_option('--format', action='store', dest='format',
+ default='%d/%m/%Y', help='format to parse until date')
+
def execute(self, path):
container, sep, object = path.partition('/')
+ until = None
+ if getattr(self, 'until'):
+ t = _time.strptime(self.until, self.format)
+ until = int(_time.mktime(t))
+
if object:
- self.client.delete_object(container, object)
+ self.client.delete_object(container, object, until)
else:
- self.client.delete_container(container)
+ self.client.delete_container(container, until)
@cli_command('get')
class GetObject(Command):
default=False, help='show detailed output')
parser.add_option('--range', action='store', dest='range',
default=None, help='show range of data')
- parser.add_option('--if-range', action='store', dest='if-range',
+ parser.add_option('--if-range', action='store', dest='if_range',
default=None, help='show range of data')
- parser.add_option('--if-match', action='store', dest='if-match',
+ parser.add_option('--if-match', action='store', dest='if_match',
default=None, help='show output if ETags match')
parser.add_option('--if-none-match', action='store',
- dest='if-none-match', default=None,
+ dest='if_none_match', default=None,
help='show output if ETags don\'t match')
parser.add_option('--if-modified-since', action='store', type='str',
- dest='if-modified-since', default=None,
+ dest='if_modified_since', default=None,
help='show output if modified since then')
parser.add_option('--if-unmodified-since', action='store', type='str',
- dest='if-unmodified-since', default=None,
+ dest='if_unmodified_since', default=None,
help='show output if not modified since then')
parser.add_option('-o', action='store', type='str',
dest='file', default=None,
help='get the full object version list')
def execute(self, path):
- headers = {}
+ attrs = ['if_match', 'if_none_match', 'if_modified_since',
+ 'if_unmodified_since']
+ args = self._build_args(attrs)
+ args['format'] = 'json' if self.detail else 'text'
if self.range:
- headers['RANGE'] = 'bytes=%s' %self.range
- if getattr(self, 'if-range'):
- headers['IF_RANGE'] = 'If-Range:%s' % getattr(self, 'if-range')
- attrs = ['if-match', 'if-none-match', 'if-modified-since',
- 'if-unmodified-since']
- attrs = [a for a in attrs if getattr(self, a)]
- for a in attrs:
- headers[a.replace('-', '_').upper()] = getattr(self, a)
+ args['range'] = 'bytes=%s' %self.range
+ if getattr(self, 'if_range'):
+ args['if-range'] = 'If-Range:%s' % getattr(self, 'if_range')
+
container, sep, object = path.partition('/')
+ data = None
if self.versionlist:
- self.version = 'list'
+ if 'detail' in args.keys():
+ args.pop('detail')
+ args.pop('format')
self.detail = True
- data = self.client.retrieve_object(container, object, self.detail,
- headers, self.version)
+ data = self.client.retrieve_object_versionlist(container, object, **args)
+ elif self.version:
+ data = self.client.retrieve_object_version(container, object,
+ self.version, **args)
+ else:
+ data = self.client.retrieve_object(container, object, **args)
+
f = self.file and open(self.file, 'w') or stdout
if self.detail:
- data = json.loads(data)
if self.versionlist:
print_versions(data, f=f)
else:
@cli_command('put')
class PutObject(Command):
- syntax = '<container>/<object> <path> [key=val] [...]'
+ syntax = '<container>/<object> [key=val] [...]'
description = 'create/override object'
def add_options(self, parser):
parser.add_option('--etag', action='store', dest='etag',
default=None, help='check written data')
parser.add_option('--content-encoding', action='store',
- dest='content-encoding', default=None,
+ dest='content_encoding', default=None,
help='provide the object MIME content type')
parser.add_option('--content-disposition', action='store', type='str',
- dest='content-disposition', default=None,
+ dest='content_disposition', default=None,
help='provide the presentation style of the object')
- parser.add_option('-S', action='store',
- dest='segment-size', default=False,
- help='use for large file support')
- parser.add_option('--manifest', action='store_true',
- dest='manifest', default=None,
+ #parser.add_option('-S', action='store',
+ # dest='segment_size', default=False,
+ # help='use for large file support')
+ parser.add_option('--manifest', action='store',
+ dest='x_object_manifest', default=None,
help='upload a manifestation file')
- parser.add_option('--type', action='store',
- dest='content-type', default=False,
+ parser.add_option('--content-type', action='store',
+ dest='content_type', default=None,
help='create object with specific content type')
parser.add_option('--sharing', action='store',
- dest='sharing', default=None,
+ dest='x_object_sharing', default=None,
help='define sharing object policy')
parser.add_option('-f', action='store',
dest='srcpath', default=None,
help='file descriptor to read from (pass - for standard input)')
- parser.add_option('--public', action='store',
- dest='public', default=None,
- help='make object publicly accessible (\'True\'/\'False\')')
+ parser.add_option('--public', action='store_true',
+ dest='x_object_public', default=False,
+ help='make object publicly accessible')
def execute(self, path, *args):
if path.find('=') != -1:
key, sep, val = arg.partition('=')
meta[key] = val
- headers = {}
- manifest = getattr(self, 'manifest')
- if manifest:
- # if it's manifestation file
- # send zero-byte data with X-Object-Manifest header
- self.touch = True
- headers['X_OBJECT_MANIFEST'] = manifest
- if self.sharing:
- headers['X_OBJECT_SHARING'] = self.sharing
-
- attrs = ['etag', 'content-encoding', 'content-disposition',
- 'content-type']
- attrs = [a for a in attrs if getattr(self, a)]
- for a in attrs:
- headers[a.replace('-', '_').upper()] = getattr(self, a)
+ attrs = ['etag', 'content_encoding', 'content_disposition',
+ 'content_type', 'x_object_sharing', 'x_object_public']
+ args = self._build_args(attrs)
container, sep, object = path.partition('/')
if self.use_hashes and not f:
raise Fault('Illegal option combination')
- if self.public not in ['True', 'False', None]:
- raise Fault('Not acceptable value for public')
- public = eval(self.public) if self.public else None
- self.client.create_object(container, object, f, chunked=self.chunked,
- headers=headers, use_hashes=self.use_hashes,
- public=public, **meta)
+
+ if self.chunked:
+ self.client.create_object_using_chunks(container, object, f,
+ meta=meta, **args)
+ elif self.use_hashes:
+ data = f.read()
+ if data is object:
+ hashmap = json.loads()
+ self.client.create_object_by_hashmap(container, object, hashmap,
+ meta=meta, **args)
+ else:
+ print "Expected object"
+ elif self.x_object_manifest:
+ self.client.create_manifestation(container, object, self.x_object_manifest)
+ elif not f:
+ self.client.create_zero_length_object(container, object, meta=meta, **args)
+ else:
+ self.client.create_object(container, object, f, meta=meta, **args)
if f:
f.close()
@cli_command('copy', 'cp')
class CopyObject(Command):
- syntax = '<src container>/<src object> [<dst container>/]<dst object>'
+ syntax = '<src container>/<src object> [<dst container>/]<dst object> [key=val] [...]'
description = 'copy an object to a different location'
def add_options(self, parser):
parser.add_option('--version', action='store',
dest='version', default=False,
help='copy specific version')
- parser.add_option('--public', action='store',
- dest='public', default=None,
- help='publish/unpublish object (\'True\'/\'False\')')
-
- def execute(self, src, dst):
+ parser.add_option('--public', action='store_true',
+ dest='public', default=False,
+ help='make object publicly accessible')
+ parser.add_option('--content-type', action='store',
+ dest='content_type', default=None,
+ help='change object\'s content type')
+
+ def execute(self, src, dst, *args):
src_container, sep, src_object = src.partition('/')
dst_container, sep, dst_object = dst.partition('/')
+
+ #prepare user defined meta
+ meta = {}
+ for arg in args:
+ key, sep, val = arg.partition('=')
+ meta[key] = val
+
if not sep:
dst_container = src_container
dst_object = dst
- version = getattr(self, 'version')
- if version:
- headers = {}
- headers['X_SOURCE_VERSION'] = version
- if self.public and self.nopublic:
- raise Fault('Conflicting options')
- if self.public not in ['True', 'False', None]:
- raise Fault('Not acceptable value for public')
- public = eval(self.public) if self.public else None
+
+ args = {'content_type':self.content_type} if self.content_type else {}
self.client.copy_object(src_container, src_object, dst_container,
- dst_object, public, headers)
+ dst_object, meta, self.public, self.version,
+ **args)
@cli_command('set')
class SetMeta(Command):
syntax = '[<container>[/<object>]] key=val [key=val] [...]'
- description = 'set metadata'
+ description = 'set account/container/object metadata'
def execute(self, path, *args):
#in case of account fix the args
parser.add_option('--chunked', action='store_true', dest='chunked',
default=False, help='set chunked transfer mode')
parser.add_option('--content-encoding', action='store',
- dest='content-encoding', default=None,
+ dest='content_encoding', default=None,
help='provide the object MIME content type')
parser.add_option('--content-disposition', action='store', type='str',
- dest='content-disposition', default=None,
+ dest='content_disposition', default=None,
help='provide the presentation style of the object')
parser.add_option('--manifest', action='store', type='str',
- dest='manifest', default=None,
+ dest='x_object_manifest', default=None,
help='use for large file support')
parser.add_option('--sharing', action='store',
- dest='sharing', default=None,
+ dest='x_object_sharing', default=None,
help='define sharing object policy')
parser.add_option('--nosharing', action='store_true',
dest='no_sharing', default=None,
parser.add_option('-f', action='store',
dest='srcpath', default=None,
help='file descriptor to read from: pass - for standard input')
- parser.add_option('--public', action='store',
- dest='public', default=None,
- help='publish/unpublish object (\'True\'/\'False\')')
+ parser.add_option('--public', action='store_true',
+ dest='x_object_public', default=False,
+ help='make object publicly accessible')
+ parser.add_option('--replace', action='store_true',
+ dest='replace', default=False,
+ help='override metadata')
def execute(self, path, *args):
if path.find('=') != -1:
raise Fault('Missing path argument')
- headers = {}
- if self.manifest:
- headers['X_OBJECT_MANIFEST'] = self.manifest
- if self.sharing:
- headers['X_OBJECT_SHARING'] = self.sharing
-
- if self.no_sharing:
- headers['X_OBJECT_SHARING'] = ''
-
- attrs = ['content-encoding', 'content-disposition']
- attrs = [a for a in attrs if getattr(self, a)]
- for a in attrs:
- headers[a.replace('-', '_').upper()] = getattr(self, a)
-
#prepare user defined meta
meta = {}
for arg in args:
key, sep, val = arg.partition('=')
meta[key] = val
+ if self.no_sharing:
+ self.x_object_sharing = ''
+
+ attrs = ['content_encoding', 'content_disposition', 'x_object_sharing',
+ 'x_object_public', 'replace']
+ args = self._build_args(attrs)
+
container, sep, object = path.partition('/')
f = None
- chunked = False
if self.srcpath:
- f = self.srcpath != '-' and open(self.srcpath) or stdin
- if f:
- chunked = True if (self.chunked or f == stdin) else False
- if self.public not in ['True', 'False', None]:
- raise Fault('Not acceptable value for public')
- public = eval(self.public) if self.public else None
- self.client.update_object(container, object, f, chunked=chunked,
- headers=headers, offset=self.offset,
- public=public, **meta)
+ f = open(self.srcpath) if self.srcpath != '-' else stdin
+
+ if self.chunked:
+ self.client.update_object_using_chunks(container, object, f,
+ meta=meta, **args)
+ else:
+ self.client.update_object(container, object, f, meta=meta, **args)
if f:
f.close()
description = 'move an object to a different location'
def add_options(self, parser):
- parser.add_option('--public', action='store',
- dest='public', default=None,
- help='publish/unpublish object (\'True\'/\'False\')')
-
- def execute(self, src, dst):
+ parser.add_option('--version', action='store',
+ dest='version', default=None,
+ help='move a specific object version')
+ parser.add_option('--public', action='store_true',
+ dest='public', default=False,
+ help='make object publicly accessible')
+ parser.add_option('--content-type', action='store',
+ dest='content_type', default=None,
+ help='change object\'s content type')
+
+ def execute(self, src, dst, *args):
src_container, sep, src_object = src.partition('/')
dst_container, sep, dst_object = dst.partition('/')
if not sep:
dst_container = src_container
dst_object = dst
- if self.public not in ['True', 'False', None]:
- raise Fault('Not acceptable value for public')
- public = eval(self.public) if self.public else None
- self.client.move_object(src_container, src_object, dst_container,
- dst_object, public, headers)
-
-@cli_command('remove')
-class TrashObject(Command):
- syntax = '<container>/<object>'
- description = 'trash an object'
-
- def execute(self, src):
- src_container, sep, src_object = src.partition('/')
- self.client.trash_object(src_container, src_object)
-
-@cli_command('restore')
-class RestoreObject(Command):
- syntax = '<container>/<object>'
- description = 'restore a trashed object'
-
- def execute(self, src):
- src_container, sep, src_object = src.partition('/')
+ #prepare user defined meta
+ meta = {}
+ for arg in args:
+ key, sep, val = arg.partition('=')
+ meta[key] = val
- self.client.restore_object(src_container, src_object)
+ args = {'content_type':self.content_type} if self.content_type else {}
+ self.client.move_object(src_container, src_object, dst_container,
+ dst_object, meta, self.public, self.version, **args)
@cli_command('unset')
class UnsetObject(Command):
self.client.delete_account_metadata(meta)
@cli_command('group')
-class SetGroup(Command):
+class CreateGroup(Command):
syntax = 'key=val [key=val] [...]'
- description = 'set group account info'
+ description = 'create account groups'
def execute(self, *args):
groups = {}
groups[key] = val
self.client.set_account_groups(**groups)
+@cli_command('ungroup')
+class DeleteGroup(Command):
+ syntax = 'key [key] [...]'
+ description = 'delete account groups'
+
+ def execute(self, *args):
+ groups = []
+ for arg in args:
+ groups.append(arg)
+ self.client.unset_account_groups(groups)
+
@cli_command('policy')
class SetPolicy(Command):
syntax = 'container key=val [key=val] [...]'
self.client.unpublish_object(src_container, src_object)
+@cli_command('sharing')
+class SharingObject(Command):
+ syntax = 'list users sharing objects with the user'
+ description = 'list user accounts sharing objects with the user'
+
+ def add_options(self, parser):
+ parser.add_option('-l', action='store_true', dest='detail',
+ default=False, help='show detailed output')
+ parser.add_option('-n', action='store', type='int', dest='limit',
+ default=10000, help='show limited output')
+ parser.add_option('--marker', action='store', type='str',
+ dest='marker', default=None,
+ help='show output greater then marker')
+
+
+ def execute(self):
+ attrs = ['limit', 'marker']
+ args = self._build_args(attrs)
+ args['format'] = 'json' if self.detail else 'text'
+
+ print_list(self.client.list_shared_by_others(**args))
+
def print_usage():
cmd = Command('', [])
parser = cmd.parser
print '\nCommands:\n' + '\n'.join(sorted(commands))
def print_dict(d, header='name', f=stdout, detail=True):
- header = header in d and header or 'subdir'
+ header = header if header in d else 'subdir'
if header and header in d:
- f.write('%s\n' %d.pop(header))
+ f.write('%s\n' %d.pop(header).encode('utf8'))
if detail:
patterns = ['^x_(account|container|object)_meta_(\w+)$']
patterns.append(patterns[0].replace('_', '-'))
for key, val in sorted(d.items()):
- for p in patterns:
- p = re.compile(p)
- m = p.match(key)
- if m:
- key = m.group(2)
f.write('%s: %s\n' % (key.rjust(30), val))
def print_list(l, verbose=False, f=stdout, detail=True):
for id, t in data['versions']:
f.write('%s @ %s\n' % (str(id).rjust(30), datetime.fromtimestamp(t)))
-def _get_user():
- try:
- return os.environ['PITHOS_USER']
- except KeyError:
- return getuser()
-
-def _get_auth():
- try:
- return os.environ['PITHOS_AUTH']
- except KeyError:
- return '0000'
-
-
def main():
try:
name = argv[1]