3 from getpass import getuser
4 from optparse import OptionParser
5 from os.path import basename
6 from sys import argv, exit, stdin
7 from pithos.lib.client import Client, Fault
12 DEFAULT_HOST = '127.0.0.1:8000'
17 def cli_command(*args):
21 _cli_commands[name] = cls
25 def class_for_cli_command(name):
26 return _cli_commands[name]
28 def print_dict(d, header='name'):
31 for key, val in sorted(d.items()):
32 print '%s: %s' % (key.rjust(15), val)
35 class Command(object):
36 def __init__(self, argv):
37 parser = OptionParser()
38 parser.add_option('--host', dest='host', metavar='HOST', default=DEFAULT_HOST,
39 help='use server HOST')
40 parser.add_option('--user', dest='user', metavar='USERNAME', default=getuser(),
41 help='use account USERNAME')
42 parser.add_option('--api', dest='api', metavar='API', default=DEFAULT_API,
44 parser.add_option('-v', action='store_true', dest='verbose', default=False,
45 help='use verbose output')
46 parser.add_option('-d', action='store_true', dest='debug', default=False,
47 help='use debug output')
48 self.add_options(parser)
49 options, args = parser.parse_args(argv)
52 for opt in parser.option_list:
55 val = getattr(options, key)
56 setattr(self, key, val)
58 self.client = Client(self.host, self.user, self.api, self.verbose, self.debug)
63 def add_options(self, parser):
66 def execute(self, *args):
70 @cli_command('list', 'ls')
72 syntax = '[container]'
73 description = 'list containers or objects'
75 def add_options(self, parser):
76 parser.add_option('-l', action='store_true', dest='detail',
77 default=False, help='show detailed output')
78 parser.add_option('-n', action='store', type='int', dest='limit',
79 default=1000, help='show limited output')
80 parser.add_option('--marker', action='store', type='str',
81 dest='marker', default=None,
82 help='show output greater then marker')
83 parser.add_option('--prefix', action='store', type='str',
84 dest='prefix', default=None,
85 help='show output starting with prefix')
86 parser.add_option('--delimiter', action='store', type='str',
87 dest='delimiter', default=None,
88 help='show output up to the delimiter')
89 parser.add_option('--path', action='store', type='str',
90 dest='path', default=None,
91 help='show output starting with prefix up to /')
92 parser.add_option('--meta', action='store', type='str',
93 dest='meta', default=None,
94 help='show output having the specified meta keys')
95 parser.add_option('--if-modified-since', action='store', type='str',
96 dest='if_modified_since', default=None,
97 help='show output if modified since then')
98 parser.add_option('--if-unmodified-since', action='store', type='str',
99 dest='if_unmodified_since', default=None,
100 help='show output if not modified since then')
102 def execute(self, container=None):
104 self.list_objects(container)
106 self.list_containers()
108 def list_containers(self):
109 l = self.client.list_containers(self.detail, self.limit, self.marker,
110 self.if_modified_since,
111 self.if_unmodified_since)
114 print_dict(container)
120 print l.split('Traceback')[0]
122 def list_objects(self, container):
123 l = self.client.list_objects(container, self.detail, self.limit,
124 self.marker, self.prefix,
125 self.delimiter, self.path,
127 self.if_modified_since,
128 self.if_unmodified_since)
137 print l.split('Traceback')[0]
141 syntax = '[<container>[/<object>]]'
142 description = 'get the metadata of an account, a container or an object'
144 def execute(self, path=''):
145 container, sep, object = path.partition('/')
147 meta = self.client.retrieve_object_metadata(container, object)
149 meta = self.client.retrieve_container_metadata(container)
151 meta = self.client.account_metadata()
152 print_dict(meta, header=None)
155 @cli_command('create')
156 class CreateContainer(Command):
157 syntax = '<container>'
158 description = 'create a container'
160 def execute(self, container):
161 ret = self.client.create_container(container)
163 print 'Container already exists'
166 @cli_command('delete', 'rm')
167 class Delete(Command):
168 syntax = '<container>[/<object>]'
169 description = 'delete a container or an object'
171 def execute(self, path):
172 container, sep, object = path.partition('/')
174 self.client.delete_object(container, object)
176 self.client.delete_container(container)
180 class GetObject(Command):
181 syntax = '<container>/<object>'
182 description = 'get the data of an object'
184 def execute(self, path):
185 container, sep, object = path.partition('/')
186 print self.client.retrieve_object(container, object)
190 class PutObject(Command):
191 syntax = '<container>/<object> <path>'
192 description = 'create or update an object with contents of path'
194 def execute(self, path, srcpath):
195 container, sep, object = path.partition('/')
196 f = open(srcpath) if srcpath != '-' else stdin
198 self.client.create_object(container, object, data)
202 @cli_command('copy', 'cp')
203 class CopyObject(Command):
204 syntax = '<src container>/<src object> [<dst container>/]<dst object>'
205 description = 'copies an object to a different location'
207 def execute(self, src, dst):
208 src_container, sep, src_object = src.partition('/')
209 dst_container, sep, dst_object = dst.partition('/')
211 dst_container = src_container
213 self.client.copy_object(src_container, src_object, dst_container, dst_object)
216 class SetMeta(Command):
217 syntax = '[<container>[/<object>]] key=val [key=val] [...]'
218 description = 'set metadata'
220 def execute(self, path, *args):
221 #in case of account fix the args
230 key, sep, val = arg.partition('=')
231 meta[key.strip()] = val.strip()
232 container, sep, object = path.partition('/')
234 self.client.update_object_metadata(container, object, **meta)
236 self.client.update_container_metadata(container, **meta)
238 self.client.update_account_metadata(**meta)
243 parser.usage = '%prog <command> [options]'
247 for cls in set(_cli_commands.values()):
248 name = ', '.join(cls.commands)
249 description = getattr(cls, 'description', '')
250 commands.append(' %s %s' % (name.ljust(12), description))
251 print '\nCommands:\n' + '\n'.join(sorted(commands))
258 cls = class_for_cli_command(name)
259 except (IndexError, KeyError):
266 cmd.execute(*cmd.args)
268 cmd.parser.usage = '%%prog %s [options] %s' % (name, cmd.syntax)
269 cmd.parser.print_help()
275 if __name__ == '__main__':