root / kamaki / cli / commands / pithos_cli.py @ 7493ccb6
History | View | Annotate | Download (42.3 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.command
|
33 |
|
34 |
from kamaki.cli import command, set_api_description, CLIError |
35 |
from kamaki.clients.utils import filter_in |
36 |
from kamaki.cli.utils import format_size, raiseCLIError, print_dict, pretty_keys, print_list |
37 |
set_api_description('store', 'Pithos+ storage commands') |
38 |
from kamaki.clients.pithos import PithosClient, ClientError |
39 |
from colors import bold |
40 |
from sys import stdout, exit |
41 |
import signal |
42 |
|
43 |
from progress.bar import IncrementalBar |
44 |
|
45 |
|
46 |
class ProgressBar(IncrementalBar): |
47 |
#suffix = '%(percent)d%% - %(eta)ds'
|
48 |
suffix = '%(percent)d%%'
|
49 |
|
50 |
class _pithos_init(object): |
51 |
def main(self): |
52 |
self.token = self.config.get('store', 'token') or self.config.get('global', 'token') |
53 |
self.base_url = self.config.get('store', 'url') or self.config.get('global', 'url') |
54 |
self.account = self.config.get('store', 'account') or self.config.get('global', 'account') |
55 |
self.container = self.config.get('store', 'container') or self.config.get('global', 'container') |
56 |
self.client = PithosClient(base_url=self.base_url, token=self.token, account=self.account, |
57 |
container=self.container)
|
58 |
|
59 |
class _store_account_command(_pithos_init): |
60 |
"""Base class for account level storage commands"""
|
61 |
|
62 |
def update_parser(self, parser): |
63 |
parser.add_argument('--account', dest='account', metavar='NAME', |
64 |
help="Specify an account to use")
|
65 |
|
66 |
def progress(self, message): |
67 |
"""Return a generator function to be used for progress tracking"""
|
68 |
|
69 |
MESSAGE_LENGTH = 25
|
70 |
|
71 |
def progress_gen(n): |
72 |
msg = message.ljust(MESSAGE_LENGTH) |
73 |
for i in ProgressBar(msg).iter(range(n)): |
74 |
yield
|
75 |
yield
|
76 |
|
77 |
return progress_gen
|
78 |
|
79 |
def main(self): |
80 |
super(_store_account_command, self).main() |
81 |
if hasattr(self.args, 'account') and self.args.account is not None: |
82 |
self.client.account = self.args.account |
83 |
|
84 |
class _store_container_command(_store_account_command): |
85 |
"""Base class for container level storage commands"""
|
86 |
|
87 |
def __init__(self): |
88 |
self.container = None |
89 |
self.path = None |
90 |
|
91 |
def update_parser(self, parser): |
92 |
super(_store_container_command, self).update_parser(parser) |
93 |
parser.add_argument('--container', dest='container', metavar='NAME', default=None, |
94 |
help="Specify a container to use")
|
95 |
|
96 |
def extract_container_and_path(self, container_with_path, path_is_optional=True): |
97 |
assert isinstance(container_with_path, str) |
98 |
if ':' not in container_with_path: |
99 |
if hasattr(self.args, 'container'): |
100 |
self.container = getattr(self.args, 'container') |
101 |
else:
|
102 |
self.container = self.client.container |
103 |
if self.container is None: |
104 |
self.container = container_with_path
|
105 |
else:
|
106 |
self.path = container_with_path
|
107 |
if not path_is_optional and self.path is None: |
108 |
raise CLIError(message="Object path is missing", status=11) |
109 |
return
|
110 |
cnp = container_with_path.split(':')
|
111 |
self.container = cnp[0] |
112 |
try:
|
113 |
self.path = cnp[1] |
114 |
except IndexError: |
115 |
if path_is_optional:
|
116 |
self.path = None |
117 |
else:
|
118 |
raise CLIError(message="Object path is missing", status=11) |
119 |
|
120 |
def main(self, container_with_path=None, path_is_optional=True): |
121 |
super(_store_container_command, self).main() |
122 |
if container_with_path is not None: |
123 |
self.extract_container_and_path(container_with_path, path_is_optional)
|
124 |
self.client.container = self.container |
125 |
elif hasattr(self.args, 'container'): |
126 |
self.client.container = getattr(self.args,'container') |
127 |
self.container = self.client.container |
128 |
|
129 |
"""
|
130 |
@command()
|
131 |
class store_test(_store_container_command):
|
132 |
""Test various stuff""
|
133 |
|
134 |
def main(self):
|
135 |
super(self.__class__, self).main('pithos')
|
136 |
r = self.client.list_containers()
|
137 |
for item in r:
|
138 |
if item['name'].startswith('c1_') or item['name'].startswith('c2_') \
|
139 |
or item['name'].startswith('c3_'):
|
140 |
self.client.container = item['name']
|
141 |
self.client.del_container(delimiter='/')
|
142 |
self.client.del_container()
|
143 |
"""
|
144 |
|
145 |
@command()
|
146 |
class store_list(_store_container_command): |
147 |
"""List containers, object trees or objects in a directory
|
148 |
"""
|
149 |
|
150 |
def update_parser(self, parser): |
151 |
super(self.__class__, self).update_parser(parser) |
152 |
parser.add_argument('-l', action='store_true', dest='detail', default=False, |
153 |
help='show detailed output')
|
154 |
parser.add_argument('-N', action='store', dest='show_size', default=1000, |
155 |
help='print output in chunks of size N')
|
156 |
parser.add_argument('-n', action='store', dest='limit', default=None, |
157 |
help='show limited output')
|
158 |
parser.add_argument('--marker', action='store', dest='marker', default=None, |
159 |
help='show output greater then marker')
|
160 |
parser.add_argument('--prefix', action='store', dest='prefix', default=None, |
161 |
help='show output starting with prefix')
|
162 |
parser.add_argument('--delimiter', action='store', dest='delimiter', default=None, |
163 |
help='show output up to the delimiter')
|
164 |
parser.add_argument('--path', action='store', dest='path', default=None, |
165 |
help='show output starting with prefix up to /')
|
166 |
parser.add_argument('--meta', action='store', dest='meta', default=None, |
167 |
help='show output having the specified meta keys (e.g. --meta "meta1 meta2 ..."')
|
168 |
parser.add_argument('--if-modified-since', action='store', dest='if_modified_since', |
169 |
default=None, help='show output if modified since then') |
170 |
parser.add_argument('--if-unmodified-since', action='store', dest='if_unmodified_since', |
171 |
default=None, help='show output if not modified since then') |
172 |
parser.add_argument('--until', action='store', dest='until', default=None, |
173 |
help='show metadata until that date')
|
174 |
dateformat = '%d/%m/%Y %H:%M:%S'
|
175 |
parser.add_argument('--format', action='store', dest='format', default=dateformat, |
176 |
help='format to parse until date (default: d/m/Y H:M:S)')
|
177 |
parser.add_argument('--shared', action='store_true', dest='shared', default=False, |
178 |
help='show only shared')
|
179 |
parser.add_argument('--public', action='store_true', dest='public', default=False, |
180 |
help='show only public')
|
181 |
|
182 |
def print_objects(self, object_list): |
183 |
import sys |
184 |
try:
|
185 |
limit = getattr(self.args, 'show_size') |
186 |
limit = int(limit)
|
187 |
except AttributeError: |
188 |
pass
|
189 |
#index = 0
|
190 |
for index,obj in enumerate(object_list): |
191 |
if not obj.has_key('content_type'): |
192 |
continue
|
193 |
pretty_obj = obj.copy() |
194 |
index += 1
|
195 |
empty_space = ' '*(len(str(len(object_list))) - len(str(index))) |
196 |
if obj['content_type'] == 'application/directory': |
197 |
isDir = True
|
198 |
size = 'D'
|
199 |
else:
|
200 |
isDir = False
|
201 |
size = format_size(obj['bytes'])
|
202 |
pretty_obj['bytes'] = '%s (%s)'%(obj['bytes'],size) |
203 |
oname = bold(obj['name'])
|
204 |
if getattr(self.args, 'detail'): |
205 |
print('%s%s. %s'%(empty_space, index, oname))
|
206 |
print_dict(pretty_keys(pretty_obj), exclude=('name'))
|
207 |
print
|
208 |
else:
|
209 |
oname = '%s%s. %6s %s'%(empty_space, index, size, oname)
|
210 |
oname += '/' if isDir else '' |
211 |
print(oname) |
212 |
if limit <= index < len(object_list) and index%limit == 0: |
213 |
print('(press "enter" to continue)')
|
214 |
sys.stdin.read(1)
|
215 |
|
216 |
def print_containers(self, container_list): |
217 |
import sys |
218 |
try:
|
219 |
limit = getattr(self.args, 'show_size') |
220 |
limit = int(limit)
|
221 |
except AttributeError: |
222 |
pass
|
223 |
for index,container in enumerate(container_list): |
224 |
if container.has_key('bytes'): |
225 |
size = format_size(container['bytes'])
|
226 |
cname = '%s. %s'%(index+1, bold(container['name'])) |
227 |
if getattr(self.args, 'detail'): |
228 |
print(cname) |
229 |
pretty_c = container.copy() |
230 |
if container.has_key('bytes'): |
231 |
pretty_c['bytes'] = '%s (%s)'%(container['bytes'], size) |
232 |
print_dict(pretty_keys(pretty_c), exclude=('name'))
|
233 |
print
|
234 |
else:
|
235 |
if container.has_key('count') and container.has_key('bytes'): |
236 |
print('%s (%s, %s objects)' % (cname, size, container['count'])) |
237 |
else:
|
238 |
print(cname) |
239 |
if limit <= index < len(container_list) and index%limit == 0: |
240 |
print('(press "enter" to continue)')
|
241 |
sys.stdin.read(1)
|
242 |
|
243 |
def getuntil(self, orelse=None): |
244 |
if hasattr(self.args, 'until'): |
245 |
import time |
246 |
until = getattr(self.args, 'until') |
247 |
if until is None: |
248 |
return None |
249 |
format = getattr(self.args, 'format') |
250 |
#except TypeError:
|
251 |
try:
|
252 |
t = time.strptime(until, format) |
253 |
except ValueError as err: |
254 |
raise CLIError(message='in --until: '+unicode(err), importance=1) |
255 |
return int(time.mktime(t)) |
256 |
return orelse
|
257 |
|
258 |
def getmeta(self, orelse=[]): |
259 |
if hasattr(self.args, 'meta'): |
260 |
meta = getattr(self.args, 'meta') |
261 |
if meta is None: |
262 |
return []
|
263 |
return meta.split(' ') |
264 |
return orelse
|
265 |
|
266 |
def getpath(self, orelse=None): |
267 |
if self.path is not None: |
268 |
return self.path |
269 |
if hasattr(self.args, 'path'): |
270 |
return getattr(self.args, 'path') |
271 |
return orelse
|
272 |
|
273 |
def main(self, container____path__=None): |
274 |
super(self.__class__, self).main(container____path__) |
275 |
try:
|
276 |
if self.container is None: |
277 |
r = self.client.account_get(limit=getattr(self.args, 'limit', None), |
278 |
marker=getattr(self.args, 'marker', None), |
279 |
if_modified_since=getattr(self.args, 'if_modified_since', None), |
280 |
if_unmodified_since=getattr(self.args, 'if_unmodified_since', None), |
281 |
until=self.getuntil(),
|
282 |
show_only_shared=getattr(self.args, 'shared', False)) |
283 |
self.print_containers(r.json)
|
284 |
else:
|
285 |
r = self.client.container_get(limit=getattr(self.args, 'limit', None), |
286 |
marker=getattr(self.args, 'marker', None), |
287 |
prefix=getattr(self.args, 'prefix', None), |
288 |
delimiter=getattr(self.args, 'delimiter', None), path=self.getpath(orelse=None), |
289 |
if_modified_since=getattr(self.args, 'if_modified_since', None), |
290 |
if_unmodified_since=getattr(self.args, 'if_unmodified_since', None), |
291 |
until=self.getuntil(),
|
292 |
meta=self.getmeta(),
|
293 |
show_only_shared=getattr(self.args, 'shared', False)) |
294 |
self.print_objects(r.json)
|
295 |
except ClientError as err: |
296 |
raiseCLIError(err) |
297 |
|
298 |
@command()
|
299 |
class store_mkdir(_store_container_command): |
300 |
"""Create a directory"""
|
301 |
|
302 |
def main(self, container___directory): |
303 |
super(self.__class__, self).main(container___directory, path_is_optional=False) |
304 |
try:
|
305 |
self.client.create_directory(self.path) |
306 |
except ClientError as err: |
307 |
raiseCLIError(err) |
308 |
|
309 |
@command()
|
310 |
class store_create(_store_container_command): |
311 |
"""Create a container or a directory object"""
|
312 |
|
313 |
def update_parser(self, parser): |
314 |
super(self.__class__, self).update_parser(parser) |
315 |
parser.add_argument('--versioning', action='store', dest='versioning', default=None, |
316 |
help='set container versioning (auto/none)')
|
317 |
parser.add_argument('--quota', action='store', dest='quota', default=None, |
318 |
help='set default container quota')
|
319 |
parser.add_argument('--meta', action='store', dest='meta', default=None, |
320 |
help='set container metadata ("key1:val1 key2:val2 ...")')
|
321 |
|
322 |
def getmeta(self, orelse=None): |
323 |
try:
|
324 |
meta = getattr(self.args,'meta') |
325 |
metalist = meta.split(' ')
|
326 |
except AttributeError: |
327 |
return orelse
|
328 |
metadict = {} |
329 |
for metastr in metalist: |
330 |
(key,val) = metastr.split(':')
|
331 |
metadict[key] = val |
332 |
return metadict
|
333 |
|
334 |
def main(self, container____directory__): |
335 |
super(self.__class__, self).main(container____directory__) |
336 |
try:
|
337 |
if self.path is None: |
338 |
self.client.container_put(quota=getattr(self.args, 'quota'), |
339 |
versioning=getattr(self.args, 'versioning'), metadata=self.getmeta()) |
340 |
else:
|
341 |
self.client.create_directory(self.path) |
342 |
except ClientError as err: |
343 |
raiseCLIError(err) |
344 |
|
345 |
@command()
|
346 |
class store_copy(_store_container_command): |
347 |
"""Copy an object"""
|
348 |
|
349 |
def update_parser(self, parser): |
350 |
super(store_copy, self).update_parser(parser) |
351 |
parser.add_argument('--source-version', action='store', dest='source_version', default=None, |
352 |
help='copy specific version')
|
353 |
parser.add_argument('--public', action='store_true', dest='public', default=False, |
354 |
help='make object publicly accessible')
|
355 |
parser.add_argument('--content-type', action='store', dest='content_type', default=None, |
356 |
help='change object\'s content type')
|
357 |
parser.add_argument('--delimiter', action='store', dest='delimiter', default=None, |
358 |
help=u'mass copy objects with path staring with src_object + delimiter')
|
359 |
parser.add_argument('-r', action='store_true', dest='recursive', default=False, |
360 |
help='mass copy with delimiter /')
|
361 |
|
362 |
def getdelimiter(self): |
363 |
if getattr(self.args, 'recursive'): |
364 |
return '/' |
365 |
return getattr(self.args, 'delimiter') |
366 |
|
367 |
def main(self, source_container___path, destination_container____path__): |
368 |
super(self.__class__, self).main(source_container___path, path_is_optional=False) |
369 |
try:
|
370 |
dst = destination_container____path__.split(':')
|
371 |
dst_cont = dst[0]
|
372 |
dst_path = dst[1] if len(dst) > 1 else False |
373 |
self.client.copy_object(src_container = self.container, src_object = self.path, |
374 |
dst_container = dst_cont, dst_object = dst_path, |
375 |
source_version=getattr(self.args, 'source_version'), |
376 |
public=getattr(self.args, 'public'), |
377 |
content_type=getattr(self.args,'content_type'), delimiter=self.getdelimiter()) |
378 |
except ClientError as err: |
379 |
raiseCLIError(err) |
380 |
|
381 |
@command()
|
382 |
class store_move(_store_container_command): |
383 |
"""Copy an object"""
|
384 |
|
385 |
def update_parser(self, parser): |
386 |
super(store_move, self).update_parser(parser) |
387 |
parser.add_argument('--source-version', action='store', dest='source_version', default=None, |
388 |
help='copy specific version')
|
389 |
parser.add_argument('--public', action='store_true', dest='public', default=False, |
390 |
help='make object publicly accessible')
|
391 |
parser.add_argument('--content-type', action='store', dest='content_type', default=None, |
392 |
help='change object\'s content type')
|
393 |
parser.add_argument('--delimiter', action='store', dest='delimiter', default=None, |
394 |
help=u'mass copy objects with path staring with src_object + delimiter')
|
395 |
parser.add_argument('-r', action='store_true', dest='recursive', default=False, |
396 |
help='mass copy with delimiter /')
|
397 |
|
398 |
def getdelimiter(self): |
399 |
if getattr(self.args, 'recursive'): |
400 |
return '/' |
401 |
return getattr(self.args, 'delimiter') |
402 |
|
403 |
def main(self, source_container___path, destination_container____path__): |
404 |
super(self.__class__, self).main(source_container___path, path_is_optional=False) |
405 |
try:
|
406 |
dst = destination_container____path__.split(':')
|
407 |
dst_cont = dst[0]
|
408 |
dst_path = dst[1] if len(dst) > 1 else False |
409 |
self.client.move_object(src_container = self.container, src_object = self.path, |
410 |
dst_container = dst_cont, dst_object = dst_path, |
411 |
source_version=getattr(self.args, 'source_version'), |
412 |
public=getattr(self.args, 'public'), |
413 |
content_type=getattr(self.args,'content_type'), delimiter=self.getdelimiter()) |
414 |
except ClientError as err: |
415 |
raiseCLIError(err) |
416 |
|
417 |
@command()
|
418 |
class store_append(_store_container_command): |
419 |
"""Append local file to (existing) remote object"""
|
420 |
|
421 |
def main(self, local_path, container___path): |
422 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
423 |
try:
|
424 |
f = open(local_path, 'r') |
425 |
upload_cb = self.progress('Appending blocks') |
426 |
self.client.append_object(object=self.path, source_file = f, upload_cb = upload_cb) |
427 |
except ClientError as err: |
428 |
raiseCLIError(err) |
429 |
|
430 |
@command()
|
431 |
class store_truncate(_store_container_command): |
432 |
"""Truncate remote file up to a size"""
|
433 |
|
434 |
|
435 |
def main(self, container___path, size=0): |
436 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
437 |
try:
|
438 |
self.client.truncate_object(self.path, size) |
439 |
except ClientError as err: |
440 |
raiseCLIError(err) |
441 |
|
442 |
@command()
|
443 |
class store_overwrite(_store_container_command): |
444 |
"""Overwrite part (from start to end) of a remote file"""
|
445 |
|
446 |
def main(self, local_path, container___path, start, end): |
447 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
448 |
try:
|
449 |
f = open(local_path, 'r') |
450 |
upload_cb = self.progress('Overwritting blocks') |
451 |
self.client.overwrite_object(object=self.path, start=start, end=end, |
452 |
source_file=f, upload_cb = upload_cb) |
453 |
except ClientError as err: |
454 |
raiseCLIError(err) |
455 |
|
456 |
@command()
|
457 |
class store_manifest(_store_container_command): |
458 |
"""Create a remote file with uploaded parts by manifestation"""
|
459 |
|
460 |
def update_parser(self, parser): |
461 |
super(self.__class__, self).update_parser(parser) |
462 |
parser.add_argument('--etag', action='store', dest='etag', default=None, |
463 |
help='check written data')
|
464 |
parser.add_argument('--content-encoding', action='store', dest='content_encoding', |
465 |
default=None, help='provide the object MIME content type') |
466 |
parser.add_argument('--content-disposition', action='store', dest='content_disposition', |
467 |
default=None, help='provide the presentation style of the object') |
468 |
parser.add_argument('--content-type', action='store', dest='content_type', default=None, |
469 |
help='create object with specific content type')
|
470 |
parser.add_argument('--sharing', action='store', dest='sharing', default=None, |
471 |
help='define sharing object policy ( "read=user1,grp1,user2,... write=user1,grp2,...')
|
472 |
parser.add_argument('--public', action='store_true', dest='public', default=False, |
473 |
help='make object publicly accessible')
|
474 |
|
475 |
def getsharing(self, orelse={}): |
476 |
permstr = getattr(self.args, 'sharing') |
477 |
if permstr is None: |
478 |
return orelse
|
479 |
perms = {} |
480 |
for p in permstr.split(' '): |
481 |
(key, val) = p.split('=')
|
482 |
if key.lower() not in ('read', 'write'): |
483 |
raise CLIError(message='in --sharing: Invalid permition key', importance=1) |
484 |
val_list = val.split(',')
|
485 |
if not perms.has_key(key): |
486 |
perms[key]=[] |
487 |
for item in val_list: |
488 |
if item not in perms[key]: |
489 |
perms[key].append(item) |
490 |
return perms
|
491 |
|
492 |
def main(self, container___path): |
493 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
494 |
try:
|
495 |
self.client.create_object_by_manifestation(self.path, |
496 |
content_encoding=getattr(self.args, 'content_encoding'), |
497 |
content_disposition=getattr(self.args, 'content_disposition'), |
498 |
content_type=getattr(self.args, 'content_type'), sharing=self.getsharing(), |
499 |
public=getattr(self.args, 'public')) |
500 |
except ClientError as err: |
501 |
raiseCLIError(err) |
502 |
|
503 |
@command()
|
504 |
class store_upload(_store_container_command): |
505 |
"""Upload a file"""
|
506 |
|
507 |
def update_parser(self, parser): |
508 |
super(self.__class__, self).update_parser(parser) |
509 |
parser.add_argument('--use_hashes', action='store_true', dest='use_hashes', default=False, |
510 |
help='provide hashmap file instead of data')
|
511 |
parser.add_argument('--unchunked', action='store_true', dest='unchunked', default=False, |
512 |
help='avoid chunked transfer mode')
|
513 |
parser.add_argument('--etag', action='store', dest='etag', default=None, |
514 |
help='check written data')
|
515 |
parser.add_argument('--content-encoding', action='store', dest='content_encoding', |
516 |
default=None, help='provide the object MIME content type') |
517 |
parser.add_argument('--content-disposition', action='store', dest='content_disposition', |
518 |
default=None, help='provide the presentation style of the object') |
519 |
parser.add_argument('--content-type', action='store', dest='content_type', default=None, |
520 |
help='create object with specific content type')
|
521 |
parser.add_argument('--sharing', action='store', dest='sharing', default=None, |
522 |
help='define sharing object policy ( "read=user1,grp1,user2,... write=user1,grp2,...')
|
523 |
parser.add_argument('--public', action='store_true', dest='public', default=False, |
524 |
help='make object publicly accessible')
|
525 |
|
526 |
def getsharing(self, orelse={}): |
527 |
permstr = getattr(self.args, 'sharing') |
528 |
if permstr is None: |
529 |
return orelse
|
530 |
perms = {} |
531 |
for p in permstr.split(' '): |
532 |
(key, val) = p.split('=')
|
533 |
if key.lower() not in ('read', 'write'): |
534 |
raise CLIError(message='in --sharing: Invalid permition key', importance=1) |
535 |
val_list = val.split(',')
|
536 |
if not perms.has_key(key): |
537 |
perms[key]=[] |
538 |
for item in val_list: |
539 |
if item not in perms[key]: |
540 |
perms[key].append(item) |
541 |
return perms
|
542 |
|
543 |
def main(self, local_path, container____path__): |
544 |
super(self.__class__, self).main(container____path__) |
545 |
remote_path = local_path if self.path is None else self.path |
546 |
try:
|
547 |
with open(local_path) as f: |
548 |
if getattr(self.args, 'unchunked'): |
549 |
self.client.upload_object_unchunked(remote_path, f,
|
550 |
etag=getattr(self.args, 'etag'), withHashFile=getattr(self.args, 'use_hashes'), |
551 |
content_encoding=getattr(self.args, 'content_encoding'), |
552 |
content_disposition=getattr(self.args, 'content_disposition'), |
553 |
content_type=getattr(self.args, 'content_type'), sharing=self.getsharing(), |
554 |
public=getattr(self.args, 'public')) |
555 |
else:
|
556 |
hash_cb = self.progress('Calculating block hashes') |
557 |
upload_cb = self.progress('Uploading blocks') |
558 |
self.client.upload_object(remote_path, f, hash_cb=hash_cb, upload_cb=upload_cb,
|
559 |
content_encoding=getattr(self.args, 'content_encoding'), |
560 |
content_disposition=getattr(self.args, 'content_disposition'), |
561 |
content_type=getattr(self.args, 'content_type'), sharing=self.getsharing(), |
562 |
public=getattr(self.args, 'public')) |
563 |
except ClientError as err: |
564 |
raiseCLIError(err) |
565 |
|
566 |
@command()
|
567 |
class store_download(_store_container_command): |
568 |
"""Download a file"""
|
569 |
|
570 |
def update_parser(self, parser): |
571 |
super(self.__class__, self).update_parser(parser) |
572 |
parser.add_argument('--no-progress-bar', action='store_true', dest='no_progress_bar', |
573 |
default=False, help='Dont display progress bars') |
574 |
parser.add_argument('--overide', action='store_true', dest='overide', default=False, |
575 |
help='Force download to overide an existing file')
|
576 |
parser.add_argument('--range', action='store', dest='range', default=None, |
577 |
help='show range of data')
|
578 |
parser.add_argument('--if-match', action='store', dest='if_match', default=None, |
579 |
help='show output if ETags match')
|
580 |
parser.add_argument('--if-none-match', action='store', dest='if_none_match', default=None, |
581 |
help='show output if ETags don\'t match')
|
582 |
parser.add_argument('--if-modified-since', action='store', dest='if_modified_since', |
583 |
default=None, help='show output if modified since then') |
584 |
parser.add_argument('--if-unmodified-since', action='store', dest='if_unmodified_since', |
585 |
default=None, help='show output if not modified since then') |
586 |
parser.add_argument('--object-version', action='store', dest='object_version', default=None, |
587 |
help='get the specific version')
|
588 |
|
589 |
def main(self, container___path, local_path=None): |
590 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
591 |
|
592 |
#setup output stream
|
593 |
if local_path is None: |
594 |
out = stdout |
595 |
else:
|
596 |
try:
|
597 |
if getattr(self.args, 'overide'): |
598 |
out = open(local_path, 'wb+') |
599 |
else:
|
600 |
out = open(local_path, 'ab+') |
601 |
except IOError as err: |
602 |
raise CLIError(message='Cannot write to file %s - %s'%(local_path,unicode(err)), |
603 |
importance=1)
|
604 |
download_cb = None if getattr(self.args, 'no_progress_bar') \ |
605 |
else self.progress('Downloading') |
606 |
|
607 |
try:
|
608 |
self.client.download_object(self.path, out, download_cb, |
609 |
range=getattr(self.args, 'range'), version=getattr(self.args,'object_version'), |
610 |
if_match=getattr(self.args, 'if_match'), overide=getattr(self.args, 'overide'), |
611 |
if_none_match=getattr(self.args, 'if_none_match'), |
612 |
if_modified_since=getattr(self.args, 'if_modified_since'), |
613 |
if_unmodified_since=getattr(self.args, 'if_unmodified_since')) |
614 |
except ClientError as err: |
615 |
raiseCLIError(err) |
616 |
except KeyboardInterrupt: |
617 |
print('\ndownload canceled by user')
|
618 |
if local_path is not None: |
619 |
print('re-run command to resume')
|
620 |
print
|
621 |
|
622 |
@command()
|
623 |
class store_hashmap(_store_container_command): |
624 |
"""Get the hashmap of an object"""
|
625 |
|
626 |
def update_parser(self, parser): |
627 |
super(self.__class__, self).update_parser(parser) |
628 |
parser.add_argument('--if-match', action='store', dest='if_match', default=None, |
629 |
help='show output if ETags match')
|
630 |
parser.add_argument('--if-none-match', action='store', dest='if_none_match', default=None, |
631 |
help='show output if ETags dont match')
|
632 |
parser.add_argument('--if-modified-since', action='store', dest='if_modified_since', |
633 |
default=None, help='show output if modified since then') |
634 |
parser.add_argument('--if-unmodified-since', action='store', dest='if_unmodified_since', |
635 |
default=None, help='show output if not modified since then') |
636 |
parser.add_argument('--object-version', action='store', dest='object_version', default=None, |
637 |
help='get the specific version')
|
638 |
|
639 |
def main(self, container___path): |
640 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
641 |
try:
|
642 |
data = self.client.get_object_hashmap(self.path, |
643 |
version=getattr(self.args, 'object_version'), |
644 |
if_match=getattr(self.args, 'if_match'), |
645 |
if_none_match=getattr(self.args, 'if_none_match'), |
646 |
if_modified_since=getattr(self.args, 'if_modified_since'), |
647 |
if_unmodified_since=getattr(self.args, 'if_unmodified_since')) |
648 |
except ClientError as err: |
649 |
raiseCLIError(err) |
650 |
print_dict(data) |
651 |
|
652 |
@command()
|
653 |
class store_delete(_store_container_command): |
654 |
"""Delete a container [or an object]"""
|
655 |
|
656 |
def update_parser(self, parser): |
657 |
super(self.__class__, self).update_parser(parser) |
658 |
parser.add_argument('--until', action='store', dest='until', default=None, |
659 |
help='remove history until that date')
|
660 |
parser.add_argument('--format', action='store', dest='format', default='%d/%m/%Y %H:%M:%S', |
661 |
help='format to parse until date (default: d/m/Y H:M:S)')
|
662 |
parser.add_argument('--delimiter', action='store', dest='delimiter', |
663 |
default=None,
|
664 |
help='mass delete objects with path staring with <object><delimiter>')
|
665 |
parser.add_argument('-r', action='store_true', dest='recursive', default=False, |
666 |
help='empty dir or container and delete (if dir)')
|
667 |
|
668 |
def getuntil(self, orelse=None): |
669 |
if hasattr(self.args, 'until'): |
670 |
import time |
671 |
until = getattr(self.args, 'until') |
672 |
if until is None: |
673 |
return None |
674 |
format = getattr(self.args, 'format') |
675 |
try:
|
676 |
t = time.strptime(until, format) |
677 |
except ValueError as err: |
678 |
raise CLIError(message='in --until: '+unicode(err), importance=1) |
679 |
return int(time.mktime(t)) |
680 |
return orelse
|
681 |
|
682 |
def getdelimiter(self, orelse=None): |
683 |
try:
|
684 |
dlm = getattr(self.args, 'delimiter') |
685 |
if dlm is None: |
686 |
return '/' if getattr(self.args, 'recursive') else orelse |
687 |
except AttributeError: |
688 |
return orelse
|
689 |
return dlm
|
690 |
|
691 |
def main(self, container____path__): |
692 |
super(self.__class__, self).main(container____path__) |
693 |
try:
|
694 |
if self.path is None: |
695 |
self.client.del_container(until=self.getuntil(), delimiter=self.getdelimiter()) |
696 |
else:
|
697 |
#self.client.delete_object(self.path)
|
698 |
self.client.del_object(self.path, until=self.getuntil(), |
699 |
delimiter=self.getdelimiter())
|
700 |
except ClientError as err: |
701 |
raiseCLIError(err) |
702 |
|
703 |
@command()
|
704 |
class store_purge(_store_container_command): |
705 |
"""Purge a container"""
|
706 |
|
707 |
def main(self, container): |
708 |
super(self.__class__, self).main() |
709 |
try:
|
710 |
self.client.purge_container()
|
711 |
except ClientError as err: |
712 |
raiseCLIError(err) |
713 |
|
714 |
@command()
|
715 |
class store_publish(_store_container_command): |
716 |
"""Publish an object"""
|
717 |
|
718 |
def main(self, container___path): |
719 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
720 |
try:
|
721 |
self.client.publish_object(self.path) |
722 |
except ClientError as err: |
723 |
raiseCLIError(err) |
724 |
|
725 |
@command()
|
726 |
class store_unpublish(_store_container_command): |
727 |
"""Unpublish an object"""
|
728 |
|
729 |
def main(self, container___path): |
730 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
731 |
try:
|
732 |
self.client.unpublish_object(self.path) |
733 |
except ClientError as err: |
734 |
raiseCLIError(err) |
735 |
|
736 |
@command()
|
737 |
class store_permitions(_store_container_command): |
738 |
"""Get object read/write permitions"""
|
739 |
|
740 |
def main(self, container___path): |
741 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
742 |
try:
|
743 |
reply = self.client.get_object_sharing(self.path) |
744 |
print_dict(reply) |
745 |
except ClientError as err: |
746 |
raiseCLIError(err) |
747 |
|
748 |
@command()
|
749 |
class store_setpermitions(_store_container_command): |
750 |
"""Set sharing permitions"""
|
751 |
|
752 |
def format_permition_dict(self,permitions): |
753 |
read = False
|
754 |
write = False
|
755 |
for perms in permitions: |
756 |
splstr = perms.split('=')
|
757 |
if 'read' == splstr[0]: |
758 |
read = [user_or_group.strip() \ |
759 |
for user_or_group in splstr[1].split(',')] |
760 |
elif 'write' == splstr[0]: |
761 |
write = [user_or_group.strip() \ |
762 |
for user_or_group in splstr[1].split(',')] |
763 |
else:
|
764 |
read = False
|
765 |
write = False
|
766 |
if not read and not write: |
767 |
raise CLIError(message='Usage:\tread=<groups,users> write=<groups,users>', |
768 |
importance=0)
|
769 |
return (read,write)
|
770 |
|
771 |
def main(self, container___path, *permitions): |
772 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
773 |
(read, write) = self.format_permition_dict(permitions)
|
774 |
try:
|
775 |
self.client.set_object_sharing(self.path, |
776 |
read_permition=read, write_permition=write) |
777 |
except ClientError as err: |
778 |
raiseCLIError(err) |
779 |
|
780 |
@command()
|
781 |
class store_delpermitions(_store_container_command): |
782 |
"""Delete all sharing permitions"""
|
783 |
|
784 |
def main(self, container___path): |
785 |
super(self.__class__, self).main(container___path, path_is_optional=False) |
786 |
try:
|
787 |
self.client.del_object_sharing(self.path) |
788 |
except ClientError as err: |
789 |
raiseCLIError(err) |
790 |
|
791 |
@command()
|
792 |
class store_info(_store_container_command): |
793 |
"""Get information for account [, container [or object]]"""
|
794 |
|
795 |
|
796 |
def main(self, container____path__=None): |
797 |
super(self.__class__, self).main(container____path__) |
798 |
try:
|
799 |
if self.container is None: |
800 |
reply = self.client.get_account_info()
|
801 |
elif self.path is None: |
802 |
reply = self.client.get_container_info(self.container) |
803 |
else:
|
804 |
reply = self.client.get_object_info(self.path) |
805 |
except ClientError as err: |
806 |
raiseCLIError(err) |
807 |
print_dict(reply) |
808 |
|
809 |
@command()
|
810 |
class store_meta(_store_container_command): |
811 |
"""Get custom meta-content for account [, container [or object]]"""
|
812 |
|
813 |
def update_parser(self, parser): |
814 |
super(self.__class__, self).update_parser(parser) |
815 |
parser.add_argument('-l', action='store_true', dest='detail', default=False, |
816 |
help='show detailed output')
|
817 |
parser.add_argument('--until', action='store', dest='until', default=None, |
818 |
help='show metadata until that date')
|
819 |
dateformat='%d/%m/%Y %H:%M:%S'
|
820 |
parser.add_argument('--format', action='store', dest='format', default=dateformat, |
821 |
help='format to parse until date (default: "d/m/Y H:M:S")')
|
822 |
parser.add_argument('--object_version', action='store', dest='object_version', default=None, |
823 |
help='show specific version \ (applies only for objects)')
|
824 |
|
825 |
def getuntil(self, orelse=None): |
826 |
if hasattr(self.args, 'until'): |
827 |
import time |
828 |
until = getattr(self.args, 'until') |
829 |
if until is None: |
830 |
return None |
831 |
format = getattr(self.args, 'format') |
832 |
#except TypeError:
|
833 |
try:
|
834 |
t = time.strptime(until, format) |
835 |
except ValueError as err: |
836 |
raise CLIError(message='in --until: '+unicode(err), importance=1) |
837 |
return int(time.mktime(t)) |
838 |
return orelse
|
839 |
|
840 |
def main(self, container____path__ = None): |
841 |
super(self.__class__, self).main(container____path__) |
842 |
|
843 |
detail = getattr(self.args, 'detail') |
844 |
try:
|
845 |
if self.container is None: |
846 |
print(bold(self.client.account))
|
847 |
if detail:
|
848 |
reply = self.client.get_account_info(until=self.getuntil()) |
849 |
else:
|
850 |
reply = self.client.get_account_meta(until=self.getuntil()) |
851 |
reply = pretty_keys(reply, '-')
|
852 |
elif self.path is None: |
853 |
print(bold(self.client.account+': '+self.container)) |
854 |
if detail:
|
855 |
reply = self.client.get_container_info(until = self.getuntil()) |
856 |
else:
|
857 |
cmeta = self.client.get_container_meta(until=self.getuntil()) |
858 |
ometa = self.client.get_container_object_meta(until=self.getuntil()) |
859 |
reply = {'container-meta':pretty_keys(cmeta, '-'), |
860 |
'object-meta':pretty_keys(ometa, '-')} |
861 |
else:
|
862 |
print(bold(self.client.account+': '+self.container+':'+self.path)) |
863 |
version=getattr(self.args, 'object_version') |
864 |
if detail:
|
865 |
reply = self.client.get_object_info(self.path, version = version) |
866 |
else:
|
867 |
reply = self.client.get_object_meta(self.path, version=version) |
868 |
reply = pretty_keys(pretty_keys(reply, '-'))
|
869 |
except ClientError as err: |
870 |
raiseCLIError(err) |
871 |
print_dict(reply) |
872 |
|
873 |
@command()
|
874 |
class store_setmeta(_store_container_command): |
875 |
"""Set a new metadatum for account [, container [or object]]"""
|
876 |
|
877 |
def main(self, metakey___metaval, container____path__=None): |
878 |
super(self.__class__, self).main(container____path__) |
879 |
try:
|
880 |
metakey, metavalue = metakey___metaval.split(':')
|
881 |
except ValueError: |
882 |
raise CLIError(message='Meta variables should be formated as metakey:metavalue', |
883 |
importance=1)
|
884 |
try:
|
885 |
if self.container is None: |
886 |
self.client.set_account_meta({metakey:metavalue})
|
887 |
elif self.path is None: |
888 |
self.client.set_container_meta({metakey:metavalue})
|
889 |
else:
|
890 |
self.client.set_object_meta(self.path, {metakey:metavalue}) |
891 |
except ClientError as err: |
892 |
raiseCLIError(err) |
893 |
|
894 |
@command()
|
895 |
class store_delmeta(_store_container_command): |
896 |
"""Delete an existing metadatum of account [, container [or object]]"""
|
897 |
|
898 |
def main(self, metakey, container____path__=None): |
899 |
super(self.__class__, self).main(container____path__) |
900 |
try:
|
901 |
if self.container is None: |
902 |
self.client.del_account_meta(metakey)
|
903 |
elif self.path is None: |
904 |
self.client.del_container_meta(metakey)
|
905 |
else:
|
906 |
self.client.del_object_meta(metakey, self.path) |
907 |
except ClientError as err: |
908 |
raiseCLIError(err) |
909 |
|
910 |
@command()
|
911 |
class store_quota(_store_account_command): |
912 |
"""Get quota for account [or container]"""
|
913 |
|
914 |
def main(self, container = None): |
915 |
super(self.__class__, self).main() |
916 |
try:
|
917 |
if container is None: |
918 |
reply = self.client.get_account_quota()
|
919 |
else:
|
920 |
reply = self.client.get_container_quota(container)
|
921 |
except ClientError as err: |
922 |
raiseCLIError(err) |
923 |
print_dict(reply) |
924 |
|
925 |
@command()
|
926 |
class store_setquota(_store_account_command): |
927 |
"""Set new quota (in KB) for account [or container]"""
|
928 |
|
929 |
def main(self, quota, container = None): |
930 |
super(self.__class__, self).main() |
931 |
try:
|
932 |
if container is None: |
933 |
self.client.set_account_quota(quota)
|
934 |
else:
|
935 |
self.client.container = container
|
936 |
self.client.set_container_quota(quota)
|
937 |
except ClientError as err: |
938 |
raiseCLIError(err) |
939 |
|
940 |
@command()
|
941 |
class store_versioning(_store_account_command): |
942 |
"""Get versioning for account [or container ]"""
|
943 |
|
944 |
def main(self, container = None): |
945 |
super(self.__class__, self).main() |
946 |
try:
|
947 |
if container is None: |
948 |
reply = self.client.get_account_versioning()
|
949 |
else:
|
950 |
reply = self.client.get_container_versioning(container)
|
951 |
except ClientError as err: |
952 |
raiseCLIError(err) |
953 |
print_dict(reply) |
954 |
|
955 |
@command()
|
956 |
class store_setversioning(_store_account_command): |
957 |
"""Set new versioning (auto, none) for account [or container]"""
|
958 |
|
959 |
def main(self, versioning, container = None): |
960 |
super(self.__class__, self).main() |
961 |
try:
|
962 |
if container is None: |
963 |
self.client.set_account_versioning(versioning)
|
964 |
else:
|
965 |
self.client.container = container
|
966 |
self.client.set_container_versioning(versioning)
|
967 |
except ClientError as err: |
968 |
raiseCLIError(err) |
969 |
|
970 |
@command()
|
971 |
class store_group(_store_account_command): |
972 |
"""Get user groups details for account"""
|
973 |
|
974 |
def main(self): |
975 |
super(self.__class__, self).main() |
976 |
try:
|
977 |
reply = self.client.get_account_group()
|
978 |
except ClientError as err: |
979 |
raiseCLIError(err) |
980 |
print_dict(reply) |
981 |
|
982 |
@command()
|
983 |
class store_setgroup(_store_account_command): |
984 |
"""Create/update a new user group on account"""
|
985 |
|
986 |
def main(self, groupname, *users): |
987 |
super(self.__class__, self).main() |
988 |
try:
|
989 |
self.client.set_account_group(groupname, users)
|
990 |
except ClientError as err: |
991 |
raiseCLIError(err) |
992 |
|
993 |
@command()
|
994 |
class store_delgroup(_store_account_command): |
995 |
"""Delete a user group on an account"""
|
996 |
|
997 |
def main(self, groupname): |
998 |
super(self.__class__, self).main() |
999 |
try:
|
1000 |
self.client.del_account_group(groupname)
|
1001 |
except ClientError as err: |
1002 |
raiseCLIError(err) |