Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / pithos.py @ a339a3ee

History | View | Annotate | Download (77.3 kB)

1
# Copyright 2011-2013 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 sys import stdout
35
from time import localtime, strftime
36
from os import path, makedirs, walk
37

    
38
from kamaki.cli import command
39
from kamaki.cli.command_tree import CommandTree
40
from kamaki.cli.errors import raiseCLIError, CLISyntaxError, CLIBaseUrlError
41
from kamaki.cli.utils import (
42
    format_size, to_bytes, print_dict, print_items, pretty_keys, pretty_dict,
43
    page_hold, bold, ask_user, get_path_size, print_json)
44
from kamaki.cli.argument import FlagArgument, ValueArgument, IntArgument
45
from kamaki.cli.argument import KeyValueArgument, DateArgument
46
from kamaki.cli.argument import ProgressBarArgument
47
from kamaki.cli.commands import _command_init, errors
48
from kamaki.cli.commands import addLogSettings, DontRaiseKeyError
49
from kamaki.cli.commands import _optional_output_cmd, _optional_json
50
from kamaki.clients.pithos import PithosClient, ClientError
51
from kamaki.clients.astakos import AstakosClient
52

    
53
pithos_cmds = CommandTree('file', 'Pithos+/Storage API commands')
54
_commands = [pithos_cmds]
55

    
56

    
57
# Argument functionality
58

    
59
class DelimiterArgument(ValueArgument):
60
    """
61
    :value type: string
62
    :value returns: given string or /
63
    """
64

    
65
    def __init__(self, caller_obj, help='', parsed_name=None, default=None):
66
        super(DelimiterArgument, self).__init__(help, parsed_name, default)
67
        self.caller_obj = caller_obj
68

    
69
    @property
70
    def value(self):
71
        if self.caller_obj['recursive']:
72
            return '/'
73
        return getattr(self, '_value', self.default)
74

    
75
    @value.setter
76
    def value(self, newvalue):
77
        self._value = newvalue
78

    
79

    
80
class SharingArgument(ValueArgument):
81
    """Set sharing (read and/or write) groups
82
    .
83
    :value type: "read=term1,term2,... write=term1,term2,..."
84
    .
85
    :value returns: {'read':['term1', 'term2', ...],
86
    .   'write':['term1', 'term2', ...]}
87
    """
88

    
89
    @property
90
    def value(self):
91
        return getattr(self, '_value', self.default)
92

    
93
    @value.setter
94
    def value(self, newvalue):
95
        perms = {}
96
        try:
97
            permlist = newvalue.split(' ')
98
        except AttributeError:
99
            return
100
        for p in permlist:
101
            try:
102
                (key, val) = p.split('=')
103
            except ValueError as err:
104
                raiseCLIError(
105
                    err,
106
                    'Error in --sharing',
107
                    details='Incorrect format',
108
                    importance=1)
109
            if key.lower() not in ('read', 'write'):
110
                msg = 'Error in --sharing'
111
                raiseCLIError(err, msg, importance=1, details=[
112
                    'Invalid permission key %s' % key])
113
            val_list = val.split(',')
114
            if not key in perms:
115
                perms[key] = []
116
            for item in val_list:
117
                if item not in perms[key]:
118
                    perms[key].append(item)
119
        self._value = perms
120

    
121

    
122
class RangeArgument(ValueArgument):
123
    """
124
    :value type: string of the form <start>-<end> where <start> and <end> are
125
        integers
126
    :value returns: the input string, after type checking <start> and <end>
127
    """
128

    
129
    @property
130
    def value(self):
131
        return getattr(self, '_value', self.default)
132

    
133
    @value.setter
134
    def value(self, newvalue):
135
        if newvalue is None:
136
            self._value = self.default
137
            return
138
        (start, end) = newvalue.split('-')
139
        (start, end) = (int(start), int(end))
140
        self._value = '%s-%s' % (start, end)
141

    
142

    
143
# Command specs
144

    
145

    
146
class _pithos_init(_command_init):
147
    """Initialize a pithos+ kamaki client"""
148

    
149
    @staticmethod
150
    def _is_dir(remote_dict):
151
        return 'application/directory' == remote_dict.get(
152
            'content_type', remote_dict.get('content-type', ''))
153

    
154
    @DontRaiseKeyError
155
    def _custom_container(self):
156
        return self.config.get_cloud(self.cloud, 'pithos_container')
157

    
158
    @DontRaiseKeyError
159
    def _custom_uuid(self):
160
        return self.config.get_cloud(self.cloud, 'pithos_uuid')
161

    
162
    def _set_account(self):
163
        self.account = self._custom_uuid()
164
        if self.account:
165
            return
166
        if getattr(self, 'auth_base', False):
167
            self.account = self.auth_base.user_term('id', self.token)
168
        else:
169
            astakos_url = self._custom_url('astakos')
170
            astakos_token = self._custom_token('astakos') or self.token
171
            if not astakos_url:
172
                raise CLIBaseUrlError(service='astakos')
173
            astakos = AstakosClient(astakos_url, astakos_token)
174
            self.account = astakos.user_term('id')
175

    
176
    @errors.generic.all
177
    @addLogSettings
178
    def _run(self):
179
        self.base_url = None
180
        if getattr(self, 'cloud', None):
181
            self.base_url = self._custom_url('pithos')
182
        else:
183
            self.cloud = 'default'
184
        self.token = self._custom_token('pithos')
185
        self.container = self._custom_container()
186

    
187
        if getattr(self, 'auth_base', False):
188
            self.token = self.token or self.auth_base.token
189
            if not self.base_url:
190
                pithos_endpoints = self.auth_base.get_service_endpoints(
191
                    self._custom_type('pithos') or 'object-store',
192
                    self._custom_version('pithos') or '')
193
                self.base_url = pithos_endpoints['publicURL']
194
        elif not self.base_url:
195
            raise CLIBaseUrlError(service='pithos')
196

    
197
        self._set_account()
198
        self.client = PithosClient(
199
            base_url=self.base_url,
200
            token=self.token,
201
            account=self.account,
202
            container=self.container)
203

    
204
    def main(self):
205
        self._run()
206

    
207

    
208
class _file_account_command(_pithos_init):
209
    """Base class for account level storage commands"""
210

    
211
    def __init__(self, arguments={}, auth_base=None, cloud=None):
212
        super(_file_account_command, self).__init__(
213
            arguments, auth_base, cloud)
214
        self['account'] = ValueArgument(
215
            'Set user account (not permanent)', ('-A', '--account'))
216

    
217
    def _run(self, custom_account=None):
218
        super(_file_account_command, self)._run()
219
        if custom_account:
220
            self.client.account = custom_account
221
        elif self['account']:
222
            self.client.account = self['account']
223

    
224
    @errors.generic.all
225
    def main(self):
226
        self._run()
227

    
228

    
229
class _file_container_command(_file_account_command):
230
    """Base class for container level storage commands"""
231

    
232
    container = None
233
    path = None
234

    
235
    def __init__(self, arguments={}, auth_base=None, cloud=None):
236
        super(_file_container_command, self).__init__(
237
            arguments, auth_base, cloud)
238
        self['container'] = ValueArgument(
239
            'Set container to work with (temporary)', ('-C', '--container'))
240

    
241
    def extract_container_and_path(
242
            self,
243
            container_with_path,
244
            path_is_optional=True):
245
        """Contains all heuristics for deciding what should be used as
246
        container or path. Options are:
247
        * user string of the form container:path
248
        * self.container, self.path variables set by super constructor, or
249
        explicitly by the caller application
250
        Error handling is explicit as these error cases happen only here
251
        """
252
        try:
253
            assert isinstance(container_with_path, str)
254
        except AssertionError as err:
255
            if self['container'] and path_is_optional:
256
                self.container = self['container']
257
                self.client.container = self['container']
258
                return
259
            raiseCLIError(err)
260

    
261
        user_cont, sep, userpath = container_with_path.partition(':')
262

    
263
        if sep:
264
            if not user_cont:
265
                raiseCLIError(CLISyntaxError(
266
                    'Container is missing\n',
267
                    details=errors.pithos.container_howto))
268
            alt_cont = self['container']
269
            if alt_cont and user_cont != alt_cont:
270
                raiseCLIError(CLISyntaxError(
271
                    'Conflict: 2 containers (%s, %s)' % (user_cont, alt_cont),
272
                    details=errors.pithos.container_howto)
273
                )
274
            self.container = user_cont
275
            if not userpath:
276
                raiseCLIError(CLISyntaxError(
277
                    'Path is missing for object in container %s' % user_cont,
278
                    details=errors.pithos.container_howto)
279
                )
280
            self.path = userpath
281
        else:
282
            alt_cont = self['container'] or self.client.container
283
            if alt_cont:
284
                self.container = alt_cont
285
                self.path = user_cont
286
            elif path_is_optional:
287
                self.container = user_cont
288
                self.path = None
289
            else:
290
                self.container = user_cont
291
                raiseCLIError(CLISyntaxError(
292
                    'Both container and path are required',
293
                    details=errors.pithos.container_howto)
294
                )
295

    
296
    @errors.generic.all
297
    def _run(self, container_with_path=None, path_is_optional=True):
298
        super(_file_container_command, self)._run()
299
        if self['container']:
300
            self.client.container = self['container']
301
            if container_with_path:
302
                self.path = container_with_path
303
            elif not path_is_optional:
304
                raise CLISyntaxError(
305
                    'Both container and path are required',
306
                    details=errors.pithos.container_howto)
307
        elif container_with_path:
308
            self.extract_container_and_path(
309
                container_with_path,
310
                path_is_optional)
311
            self.client.container = self.container
312
        self.container = self.client.container
313

    
314
    def main(self, container_with_path=None, path_is_optional=True):
315
        self._run(container_with_path, path_is_optional)
316

    
317

    
318
@command(pithos_cmds)
319
class file_list(_file_container_command, _optional_json):
320
    """List containers, object trees or objects in a directory
321
    Use with:
322
    1 no parameters : containers in current account
323
    2. one parameter (container) or --container : contents of container
324
    3. <container>:<prefix> or --container=<container> <prefix>: objects in
325
    .   container starting with prefix
326
    """
327

    
328
    arguments = dict(
329
        detail=FlagArgument('detailed output', ('-l', '--list')),
330
        limit=IntArgument('limit number of listed items', ('-n', '--number')),
331
        marker=ValueArgument('output greater that marker', '--marker'),
332
        prefix=ValueArgument('output starting with prefix', '--prefix'),
333
        delimiter=ValueArgument('show output up to delimiter', '--delimiter'),
334
        path=ValueArgument(
335
            'show output starting with prefix up to /', '--path'),
336
        meta=ValueArgument(
337
            'show output with specified meta keys', '--meta',
338
            default=[]),
339
        if_modified_since=ValueArgument(
340
            'show output modified since then', '--if-modified-since'),
341
        if_unmodified_since=ValueArgument(
342
            'show output not modified since then', '--if-unmodified-since'),
343
        until=DateArgument('show metadata until then', '--until'),
344
        format=ValueArgument(
345
            'format to parse until data (default: d/m/Y H:M:S )', '--format'),
346
        shared=FlagArgument('show only shared', '--shared'),
347
        more=FlagArgument(
348
            'output results in pages (-n to set items per page, default 10)',
349
            '--more'),
350
        exact_match=FlagArgument(
351
            'Show only objects that match exactly with path',
352
            '--exact-match'),
353
        enum=FlagArgument('Enumerate results', '--enumerate')
354
    )
355

    
356
    def print_objects(self, object_list):
357
        if self['json_output']:
358
            print_json(object_list)
359
            return
360
        limit = int(self['limit']) if self['limit'] > 0 else len(object_list)
361
        for index, obj in enumerate(object_list):
362
            if self['exact_match'] and self.path and not (
363
                    obj['name'] == self.path or 'content_type' in obj):
364
                continue
365
            pretty_obj = obj.copy()
366
            index += 1
367
            empty_space = ' ' * (len(str(len(object_list))) - len(str(index)))
368
            if 'subdir' in obj:
369
                continue
370
            if obj['content_type'] == 'application/directory':
371
                isDir = True
372
                size = 'D'
373
            else:
374
                isDir = False
375
                size = format_size(obj['bytes'])
376
                pretty_obj['bytes'] = '%s (%s)' % (obj['bytes'], size)
377
            oname = bold(obj['name'])
378
            prfx = ('%s%s. ' % (empty_space, index)) if self['enum'] else ''
379
            if self['detail']:
380
                print('%s%s' % (prfx, oname))
381
                print_dict(pretty_keys(pretty_obj), exclude=('name'))
382
                print
383
            else:
384
                oname = '%s%9s %s' % (prfx, size, oname)
385
                oname += '/' if isDir else ''
386
                print(oname)
387
            if self['more']:
388
                page_hold(index, limit, len(object_list))
389

    
390
    def print_containers(self, container_list):
391
        if self['json_output']:
392
            print_json(container_list)
393
            return
394
        limit = int(self['limit']) if self['limit'] > 0\
395
            else len(container_list)
396
        for index, container in enumerate(container_list):
397
            if 'bytes' in container:
398
                size = format_size(container['bytes'])
399
            prfx = ('%s. ' % (index + 1)) if self['enum'] else ''
400
            cname = '%s%s' % (prfx, bold(container['name']))
401
            if self['detail']:
402
                print(cname)
403
                pretty_c = container.copy()
404
                if 'bytes' in container:
405
                    pretty_c['bytes'] = '%s (%s)' % (container['bytes'], size)
406
                print_dict(pretty_keys(pretty_c), exclude=('name'))
407
                print
408
            else:
409
                if 'count' in container and 'bytes' in container:
410
                    print('%s (%s, %s objects)' % (
411
                        cname,
412
                        size,
413
                        container['count']))
414
                else:
415
                    print(cname)
416
            if self['more']:
417
                page_hold(index + 1, limit, len(container_list))
418

    
419
    @errors.generic.all
420
    @errors.pithos.connection
421
    @errors.pithos.object_path
422
    @errors.pithos.container
423
    def _run(self):
424
        if self.container is None:
425
            r = self.client.account_get(
426
                limit=False if self['more'] else self['limit'],
427
                marker=self['marker'],
428
                if_modified_since=self['if_modified_since'],
429
                if_unmodified_since=self['if_unmodified_since'],
430
                until=self['until'],
431
                show_only_shared=self['shared'])
432
            self._print(r.json, self.print_containers)
433
        else:
434
            prefix = self.path or self['prefix']
435
            r = self.client.container_get(
436
                limit=False if self['more'] else self['limit'],
437
                marker=self['marker'],
438
                prefix=prefix,
439
                delimiter=self['delimiter'],
440
                path=self['path'],
441
                if_modified_since=self['if_modified_since'],
442
                if_unmodified_since=self['if_unmodified_since'],
443
                until=self['until'],
444
                meta=self['meta'],
445
                show_only_shared=self['shared'])
446
            self._print(r.json, self.print_objects)
447

    
448
    def main(self, container____path__=None):
449
        super(self.__class__, self)._run(container____path__)
450
        self._run()
451

    
452

    
453
@command(pithos_cmds)
454
class file_mkdir(_file_container_command, _optional_output_cmd):
455
    """Create a directory
456
    Kamaki hanldes directories the same way as OOS Storage and Pithos+:
457
    A directory  is   an  object  with  type  "application/directory"
458
    An object with path  dir/name can exist even if  dir does not exist
459
    or even if dir  is  a non  directory  object.  Users can modify dir '
460
    without affecting the dir/name object in any way.
461
    """
462

    
463
    @errors.generic.all
464
    @errors.pithos.connection
465
    @errors.pithos.container
466
    def _run(self):
467
        self._optional_output(self.client.create_directory(self.path))
468

    
469
    def main(self, container___directory):
470
        super(self.__class__, self)._run(
471
            container___directory,
472
            path_is_optional=False)
473
        self._run()
474

    
475

    
476
@command(pithos_cmds)
477
class file_touch(_file_container_command, _optional_output_cmd):
478
    """Create an empty object (file)
479
    If object exists, this command will reset it to 0 length
480
    """
481

    
482
    arguments = dict(
483
        content_type=ValueArgument(
484
            'Set content type (default: application/octet-stream)',
485
            '--content-type',
486
            default='application/octet-stream')
487
    )
488

    
489
    @errors.generic.all
490
    @errors.pithos.connection
491
    @errors.pithos.container
492
    def _run(self):
493
        self._optional_output(
494
            self.client.create_object(self.path, self['content_type']))
495

    
496
    def main(self, container___path):
497
        super(file_touch, self)._run(
498
            container___path,
499
            path_is_optional=False)
500
        self._run()
501

    
502

    
503
@command(pithos_cmds)
504
class file_create(_file_container_command, _optional_output_cmd):
505
    """Create a container"""
506

    
507
    arguments = dict(
508
        versioning=ValueArgument(
509
            'set container versioning (auto/none)', '--versioning'),
510
        limit=IntArgument('set default container limit', '--limit'),
511
        meta=KeyValueArgument(
512
            'set container metadata (can be repeated)', '--meta')
513
    )
514

    
515
    @errors.generic.all
516
    @errors.pithos.connection
517
    @errors.pithos.container
518
    def _run(self, container):
519
        self._optional_output(self.client.create_container(
520
            container=container,
521
            sizelimit=self['limit'],
522
            versioning=self['versioning'],
523
            metadata=self['meta']))
524

    
525
    def main(self, container=None):
526
        super(self.__class__, self)._run(container)
527
        if container and self.container != container:
528
            raiseCLIError('Invalid container name %s' % container, details=[
529
                'Did you mean "%s" ?' % self.container,
530
                'Use --container for names containing :'])
531
        self._run(container)
532

    
533

    
534
class _source_destination_command(_file_container_command):
535

    
536
    arguments = dict(
537
        destination_account=ValueArgument('', ('-a', '--dst-account')),
538
        recursive=FlagArgument('', ('-R', '--recursive')),
539
        prefix=FlagArgument('', '--with-prefix', default=''),
540
        suffix=ValueArgument('', '--with-suffix', default=''),
541
        add_prefix=ValueArgument('', '--add-prefix', default=''),
542
        add_suffix=ValueArgument('', '--add-suffix', default=''),
543
        prefix_replace=ValueArgument('', '--prefix-to-replace', default=''),
544
        suffix_replace=ValueArgument('', '--suffix-to-replace', default=''),
545
    )
546

    
547
    def __init__(self, arguments={}, auth_base=None, cloud=None):
548
        self.arguments.update(arguments)
549
        super(_source_destination_command, self).__init__(
550
            self.arguments, auth_base, cloud)
551

    
552
    def _run(self, source_container___path, path_is_optional=False):
553
        super(_source_destination_command, self)._run(
554
            source_container___path,
555
            path_is_optional)
556
        self.dst_client = PithosClient(
557
            base_url=self.client.base_url,
558
            token=self.client.token,
559
            account=self['destination_account'] or self.client.account)
560

    
561
    @errors.generic.all
562
    @errors.pithos.account
563
    def _dest_container_path(self, dest_container_path):
564
        if self['destination_container']:
565
            self.dst_client.container = self['destination_container']
566
            return (self['destination_container'], dest_container_path)
567
        if dest_container_path:
568
            dst = dest_container_path.split(':')
569
            if len(dst) > 1:
570
                try:
571
                    self.dst_client.container = dst[0]
572
                    self.dst_client.get_container_info(dst[0])
573
                except ClientError as err:
574
                    if err.status in (404, 204):
575
                        raiseCLIError(
576
                            'Destination container %s not found' % dst[0])
577
                    raise
578
                else:
579
                    self.dst_client.container = dst[0]
580
                return (dst[0], dst[1])
581
            return(None, dst[0])
582
        raiseCLIError('No destination container:path provided')
583

    
584
    def _get_all(self, prefix):
585
        return self.client.container_get(prefix=prefix).json
586

    
587
    def _get_src_objects(self, src_path, source_version=None):
588
        """Get a list of the source objects to be called
589

590
        :param src_path: (str) source path
591

592
        :returns: (method, params) a method that returns a list when called
593
        or (object) if it is a single object
594
        """
595
        if src_path and src_path[-1] == '/':
596
            src_path = src_path[:-1]
597

    
598
        if self['prefix']:
599
            return (self._get_all, dict(prefix=src_path))
600
        try:
601
            srcobj = self.client.get_object_info(
602
                src_path, version=source_version)
603
        except ClientError as srcerr:
604
            if srcerr.status == 404:
605
                raiseCLIError(
606
                    'Source object %s not in source container %s' % (
607
                        src_path, self.client.container),
608
                    details=['Hint: --with-prefix to match multiple objects'])
609
            elif srcerr.status not in (204,):
610
                raise
611
            return (self.client.list_objects, {})
612

    
613
        if self._is_dir(srcobj):
614
            if not self['recursive']:
615
                raiseCLIError(
616
                    'Object %s of cont. %s is a dir' % (
617
                        src_path, self.client.container),
618
                    details=['Use --recursive to access directories'])
619
            return (self._get_all, dict(prefix=src_path))
620
        srcobj['name'] = src_path
621
        return srcobj
622

    
623
    def src_dst_pairs(self, dst_path, source_version=None):
624
        src_iter = self._get_src_objects(self.path, source_version)
625
        src_N = isinstance(src_iter, tuple)
626
        add_prefix = self['add_prefix'].strip('/')
627

    
628
        if dst_path and dst_path.endswith('/'):
629
            dst_path = dst_path[:-1]
630

    
631
        try:
632
            dstobj = self.dst_client.get_object_info(dst_path)
633
        except ClientError as trgerr:
634
            if trgerr.status in (404,):
635
                if src_N:
636
                    raiseCLIError(
637
                        'Cannot merge multiple paths to path %s' % dst_path,
638
                        details=[
639
                            'Try to use / or a directory as destination',
640
                            'or create the destination dir (/file mkdir)',
641
                            'or use a single object as source'])
642
            elif trgerr.status not in (204,):
643
                raise
644
        else:
645
            if self._is_dir(dstobj):
646
                add_prefix = '%s/%s' % (dst_path.strip('/'), add_prefix)
647
            elif src_N:
648
                raiseCLIError(
649
                    'Cannot merge multiple paths to path' % dst_path,
650
                    details=[
651
                        'Try to use / or a directory as destination',
652
                        'or create the destination dir (/file mkdir)',
653
                        'or use a single object as source'])
654

    
655
        if src_N:
656
            (method, kwargs) = src_iter
657
            for obj in method(**kwargs):
658
                name = obj['name']
659
                if name.endswith(self['suffix']):
660
                    yield (name, self._get_new_object(name, add_prefix))
661
        elif src_iter['name'].endswith(self['suffix']):
662
            name = src_iter['name']
663
            yield (name, self._get_new_object(dst_path or name, add_prefix))
664
        else:
665
            raiseCLIError('Source path %s conflicts with suffix %s' % (
666
                src_iter['name'], self['suffix']))
667

    
668
    def _get_new_object(self, obj, add_prefix):
669
        if self['prefix_replace'] and obj.startswith(self['prefix_replace']):
670
            obj = obj[len(self['prefix_replace']):]
671
        if self['suffix_replace'] and obj.endswith(self['suffix_replace']):
672
            obj = obj[:-len(self['suffix_replace'])]
673
        return add_prefix + obj + self['add_suffix']
674

    
675

    
676
@command(pithos_cmds)
677
class file_copy(_source_destination_command, _optional_output_cmd):
678
    """Copy objects from container to (another) container
679
    Semantics:
680
    copy cont:path dir
681
    .   transfer path as dir/path
682
    copy cont:path cont2:
683
    .   trasnfer all <obj> prefixed with path to container cont2
684
    copy cont:path [cont2:]path2
685
    .   transfer path to path2
686
    Use options:
687
    1. <container1>:<path1> [container2:]<path2> : if container2 is not given,
688
    destination is container1:path2
689
    2. <container>:<path1> <path2> : make a copy in the same container
690
    3. Can use --container= instead of <container1>
691
    """
692

    
693
    arguments = dict(
694
        destination_account=ValueArgument(
695
            'Account to copy to', ('-a', '--dst-account')),
696
        destination_container=ValueArgument(
697
            'use it if destination container name contains a : character',
698
            ('-D', '--dst-container')),
699
        public=ValueArgument('make object publicly accessible', '--public'),
700
        content_type=ValueArgument(
701
            'change object\'s content type', '--content-type'),
702
        recursive=FlagArgument(
703
            'copy directory and contents', ('-R', '--recursive')),
704
        prefix=FlagArgument(
705
            'Match objects prefixed with src path (feels like src_path*)',
706
            '--with-prefix',
707
            default=''),
708
        suffix=ValueArgument(
709
            'Suffix of source objects (feels like *suffix)', '--with-suffix',
710
            default=''),
711
        add_prefix=ValueArgument('Prefix targets', '--add-prefix', default=''),
712
        add_suffix=ValueArgument('Suffix targets', '--add-suffix', default=''),
713
        prefix_replace=ValueArgument(
714
            'Prefix of src to replace with dst path + add_prefix, if matched',
715
            '--prefix-to-replace',
716
            default=''),
717
        suffix_replace=ValueArgument(
718
            'Suffix of src to replace with add_suffix, if matched',
719
            '--suffix-to-replace',
720
            default=''),
721
        source_version=ValueArgument(
722
            'copy specific version', ('-S', '--source-version'))
723
    )
724

    
725
    @errors.generic.all
726
    @errors.pithos.connection
727
    @errors.pithos.container
728
    @errors.pithos.account
729
    def _run(self, dst_path):
730
        no_source_object = True
731
        src_account = self.client.account if (
732
            self['destination_account']) else None
733
        for src_obj, dst_obj in self.src_dst_pairs(
734
                dst_path, self['source_version']):
735
            no_source_object = False
736
            r = self.dst_client.copy_object(
737
                src_container=self.client.container,
738
                src_object=src_obj,
739
                dst_container=self.dst_client.container,
740
                dst_object=dst_obj,
741
                source_account=src_account,
742
                source_version=self['source_version'],
743
                public=self['public'],
744
                content_type=self['content_type'])
745
        if no_source_object:
746
            raiseCLIError('No object %s in container %s' % (
747
                self.path, self.container))
748
        self._optional_output(r)
749

    
750
    def main(
751
            self, source_container___path,
752
            destination_container___path=None):
753
        super(file_copy, self)._run(
754
            source_container___path,
755
            path_is_optional=False)
756
        (dst_cont, dst_path) = self._dest_container_path(
757
            destination_container___path)
758
        self.dst_client.container = dst_cont or self.container
759
        self._run(dst_path=dst_path or '')
760

    
761

    
762
@command(pithos_cmds)
763
class file_move(_source_destination_command, _optional_output_cmd):
764
    """Move/rename objects from container to (another) container
765
    Semantics:
766
    move cont:path dir
767
    .   rename path as dir/path
768
    move cont:path cont2:
769
    .   trasnfer all <obj> prefixed with path to container cont2
770
    move cont:path [cont2:]path2
771
    .   transfer path to path2
772
    Use options:
773
    1. <container1>:<path1> [container2:]<path2> : if container2 is not given,
774
    destination is container1:path2
775
    2. <container>:<path1> <path2> : move in the same container
776
    3. Can use --container= instead of <container1>
777
    """
778

    
779
    arguments = dict(
780
        destination_account=ValueArgument(
781
            'Account to move to', ('-a', '--dst-account')),
782
        destination_container=ValueArgument(
783
            'use it if destination container name contains a : character',
784
            ('-D', '--dst-container')),
785
        public=ValueArgument('make object publicly accessible', '--public'),
786
        content_type=ValueArgument(
787
            'change object\'s content type', '--content-type'),
788
        recursive=FlagArgument(
789
            'copy directory and contents', ('-R', '--recursive')),
790
        prefix=FlagArgument(
791
            'Match objects prefixed with src path (feels like src_path*)',
792
            '--with-prefix',
793
            default=''),
794
        suffix=ValueArgument(
795
            'Suffix of source objects (feels like *suffix)', '--with-suffix',
796
            default=''),
797
        add_prefix=ValueArgument('Prefix targets', '--add-prefix', default=''),
798
        add_suffix=ValueArgument('Suffix targets', '--add-suffix', default=''),
799
        prefix_replace=ValueArgument(
800
            'Prefix of src to replace with dst path + add_prefix, if matched',
801
            '--prefix-to-replace',
802
            default=''),
803
        suffix_replace=ValueArgument(
804
            'Suffix of src to replace with add_suffix, if matched',
805
            '--suffix-to-replace',
806
            default='')
807
    )
808

    
809
    @errors.generic.all
810
    @errors.pithos.connection
811
    @errors.pithos.container
812
    def _run(self, dst_path):
813
        no_source_object = True
814
        src_account = self.client.account if (
815
            self['destination_account']) else None
816
        for src_obj, dst_obj in self.src_dst_pairs(dst_path):
817
            no_source_object = False
818
            r = self.dst_client.move_object(
819
                src_container=self.container,
820
                src_object=src_obj,
821
                dst_container=self.dst_client.container,
822
                dst_object=dst_obj,
823
                source_account=src_account,
824
                public=self['public'],
825
                content_type=self['content_type'])
826
        if no_source_object:
827
            raiseCLIError('No object %s in container %s' % (
828
                self.path,
829
                self.container))
830
        self._optional_output(r)
831

    
832
    def main(
833
            self, source_container___path,
834
            destination_container___path=None):
835
        super(self.__class__, self)._run(
836
            source_container___path,
837
            path_is_optional=False)
838
        (dst_cont, dst_path) = self._dest_container_path(
839
            destination_container___path)
840
        (dst_cont, dst_path) = self._dest_container_path(
841
            destination_container___path)
842
        self.dst_client.container = dst_cont or self.container
843
        self._run(dst_path=dst_path or '')
844

    
845

    
846
@command(pithos_cmds)
847
class file_append(_file_container_command, _optional_output_cmd):
848
    """Append local file to (existing) remote object
849
    The remote object should exist.
850
    If the remote object is a directory, it is transformed into a file.
851
    In the later case, objects under the directory remain intact.
852
    """
853

    
854
    arguments = dict(
855
        progress_bar=ProgressBarArgument(
856
            'do not show progress bar',
857
            ('-N', '--no-progress-bar'),
858
            default=False)
859
    )
860

    
861
    @errors.generic.all
862
    @errors.pithos.connection
863
    @errors.pithos.container
864
    @errors.pithos.object_path
865
    def _run(self, local_path):
866
        (progress_bar, upload_cb) = self._safe_progress_bar('Appending')
867
        try:
868
            f = open(local_path, 'rb')
869
            self._optional_output(
870
                self.client.append_object(self.path, f, upload_cb))
871
        except Exception:
872
            self._safe_progress_bar_finish(progress_bar)
873
            raise
874
        finally:
875
            self._safe_progress_bar_finish(progress_bar)
876

    
877
    def main(self, local_path, container___path):
878
        super(self.__class__, self)._run(
879
            container___path, path_is_optional=False)
880
        self._run(local_path)
881

    
882

    
883
@command(pithos_cmds)
884
class file_truncate(_file_container_command, _optional_output_cmd):
885
    """Truncate remote file up to a size (default is 0)"""
886

    
887
    @errors.generic.all
888
    @errors.pithos.connection
889
    @errors.pithos.container
890
    @errors.pithos.object_path
891
    @errors.pithos.object_size
892
    def _run(self, size=0):
893
        self._optional_output(self.client.truncate_object(self.path, size))
894

    
895
    def main(self, container___path, size=0):
896
        super(self.__class__, self)._run(container___path)
897
        self._run(size=size)
898

    
899

    
900
@command(pithos_cmds)
901
class file_overwrite(_file_container_command, _optional_output_cmd):
902
    """Overwrite part (from start to end) of a remote file
903
    overwrite local-path container 10 20
904
    .   will overwrite bytes from 10 to 20 of a remote file with the same name
905
    .   as local-path basename
906
    overwrite local-path container:path 10 20
907
    .   will overwrite as above, but the remote file is named path
908
    """
909

    
910
    arguments = dict(
911
        progress_bar=ProgressBarArgument(
912
            'do not show progress bar',
913
            ('-N', '--no-progress-bar'),
914
            default=False)
915
    )
916

    
917
    def _open_file(self, local_path, start):
918
        f = open(path.abspath(local_path), 'rb')
919
        f.seek(0, 2)
920
        f_size = f.tell()
921
        f.seek(start, 0)
922
        return (f, f_size)
923

    
924
    @errors.generic.all
925
    @errors.pithos.connection
926
    @errors.pithos.container
927
    @errors.pithos.object_path
928
    @errors.pithos.object_size
929
    def _run(self, local_path, start, end):
930
        (start, end) = (int(start), int(end))
931
        (f, f_size) = self._open_file(local_path, start)
932
        (progress_bar, upload_cb) = self._safe_progress_bar(
933
            'Overwrite %s bytes' % (end - start))
934
        try:
935
            self._optional_output(self.client.overwrite_object(
936
                obj=self.path,
937
                start=start,
938
                end=end,
939
                source_file=f,
940
                upload_cb=upload_cb))
941
        finally:
942
            self._safe_progress_bar_finish(progress_bar)
943

    
944
    def main(self, local_path, container___path, start, end):
945
        super(self.__class__, self)._run(
946
            container___path, path_is_optional=None)
947
        self.path = self.path or path.basename(local_path)
948
        self._run(local_path=local_path, start=start, end=end)
949

    
950

    
951
@command(pithos_cmds)
952
class file_manifest(_file_container_command, _optional_output_cmd):
953
    """Create a remote file of uploaded parts by manifestation
954
    Remains functional for compatibility with OOS Storage. Users are advised
955
    to use the upload command instead.
956
    Manifestation is a compliant process for uploading large files. The files
957
    have to be chunked in smalled files and uploaded as <prefix><increment>
958
    where increment is 1, 2, ...
959
    Finally, the manifest command glues partial files together in one file
960
    named <prefix>
961
    The upload command is faster, easier and more intuitive than manifest
962
    """
963

    
964
    arguments = dict(
965
        etag=ValueArgument('check written data', '--etag'),
966
        content_encoding=ValueArgument(
967
            'set MIME content type', '--content-encoding'),
968
        content_disposition=ValueArgument(
969
            'the presentation style of the object', '--content-disposition'),
970
        content_type=ValueArgument(
971
            'specify content type', '--content-type',
972
            default='application/octet-stream'),
973
        sharing=SharingArgument(
974
            '\n'.join([
975
                'define object sharing policy',
976
                '    ( "read=user1,grp1,user2,... write=user1,grp2,..." )']),
977
            '--sharing'),
978
        public=FlagArgument('make object publicly accessible', '--public')
979
    )
980

    
981
    @errors.generic.all
982
    @errors.pithos.connection
983
    @errors.pithos.container
984
    @errors.pithos.object_path
985
    def _run(self):
986
        self._optional_output(self.client.create_object_by_manifestation(
987
            self.path,
988
            content_encoding=self['content_encoding'],
989
            content_disposition=self['content_disposition'],
990
            content_type=self['content_type'],
991
            sharing=self['sharing'],
992
            public=self['public']))
993

    
994
    def main(self, container___path):
995
        super(self.__class__, self)._run(
996
            container___path, path_is_optional=False)
997
        self.run()
998

    
999

    
1000
@command(pithos_cmds)
1001
class file_upload(_file_container_command, _optional_output_cmd):
1002
    """Upload a file"""
1003

    
1004
    arguments = dict(
1005
        use_hashes=FlagArgument(
1006
            'provide hashmap file instead of data', '--use-hashes'),
1007
        etag=ValueArgument('check written data', '--etag'),
1008
        unchunked=FlagArgument('avoid chunked transfer mode', '--unchunked'),
1009
        content_encoding=ValueArgument(
1010
            'set MIME content type', '--content-encoding'),
1011
        content_disposition=ValueArgument(
1012
            'specify objects presentation style', '--content-disposition'),
1013
        content_type=ValueArgument('specify content type', '--content-type'),
1014
        sharing=SharingArgument(
1015
            help='\n'.join([
1016
                'define sharing object policy',
1017
                '( "read=user1,grp1,user2,... write=user1,grp2,... )']),
1018
            parsed_name='--sharing'),
1019
        public=FlagArgument('make object publicly accessible', '--public'),
1020
        poolsize=IntArgument('set pool size', '--with-pool-size'),
1021
        progress_bar=ProgressBarArgument(
1022
            'do not show progress bar',
1023
            ('-N', '--no-progress-bar'),
1024
            default=False),
1025
        overwrite=FlagArgument('Force (over)write', ('-f', '--force')),
1026
        recursive=FlagArgument(
1027
            'Recursively upload directory *contents* + subdirectories',
1028
            ('-R', '--recursive'))
1029
    )
1030

    
1031
    def _check_container_limit(self, path):
1032
        cl_dict = self.client.get_container_limit()
1033
        container_limit = int(cl_dict['x-container-policy-quota'])
1034
        r = self.client.container_get()
1035
        used_bytes = sum(int(o['bytes']) for o in r.json)
1036
        path_size = get_path_size(path)
1037
        if container_limit and path_size > (container_limit - used_bytes):
1038
            raiseCLIError(
1039
                'Container(%s) (limit(%s) - used(%s)) < size(%s) of %s' % (
1040
                    self.client.container,
1041
                    format_size(container_limit),
1042
                    format_size(used_bytes),
1043
                    format_size(path_size),
1044
                    path),
1045
                importance=1, details=[
1046
                    'Check accound limit: /file quota',
1047
                    'Check container limit:',
1048
                    '\t/file containerlimit get %s' % self.client.container,
1049
                    'Increase container limit:',
1050
                    '\t/file containerlimit set <new limit> %s' % (
1051
                        self.client.container)])
1052

    
1053
    def _path_pairs(self, local_path, remote_path):
1054
        """Get pairs of local and remote paths"""
1055
        lpath = path.abspath(local_path)
1056
        short_path = lpath.split(path.sep)[-1]
1057
        rpath = remote_path or short_path
1058
        if path.isdir(lpath):
1059
            if not self['recursive']:
1060
                raiseCLIError('%s is a directory' % lpath, details=[
1061
                    'Use -R to upload directory contents'])
1062
            robj = self.client.container_get(path=rpath)
1063
            if robj.json and not self['overwrite']:
1064
                raiseCLIError(
1065
                    'Objects prefixed with %s already exist' % rpath,
1066
                    importance=1,
1067
                    details=['Existing objects:'] + ['\t%s:\t%s' % (
1068
                        o['content_type'][12:],
1069
                        o['name']) for o in robj.json] + [
1070
                        'Use -f to add, overwrite or resume'])
1071
            if not self['overwrite']:
1072
                try:
1073
                    topobj = self.client.get_object_info(rpath)
1074
                    if not self._is_dir(topobj):
1075
                        raiseCLIError(
1076
                            'Object %s exists but it is not a dir' % rpath,
1077
                            importance=1, details=['Use -f to overwrite'])
1078
                except ClientError as ce:
1079
                    if ce.status != 404:
1080
                        raise
1081
            self._check_container_limit(lpath)
1082
            prev = ''
1083
            for top, subdirs, files in walk(lpath):
1084
                if top != prev:
1085
                    prev = top
1086
                    try:
1087
                        rel_path = rpath + top.split(lpath)[1]
1088
                    except IndexError:
1089
                        rel_path = rpath
1090
                    print('mkdir %s:%s' % (self.client.container, rel_path))
1091
                    self.client.create_directory(rel_path)
1092
                for f in files:
1093
                    fpath = path.join(top, f)
1094
                    if path.isfile(fpath):
1095
                        rel_path = rel_path.replace(path.sep, '/')
1096
                        pathfix = f.replace(path.sep, '/')
1097
                        yield open(fpath, 'rb'), '%s/%s' % (rel_path, pathfix)
1098
                    else:
1099
                        print('%s is not a regular file' % fpath)
1100
        else:
1101
            if not path.isfile(lpath):
1102
                raiseCLIError('%s is not a regular file' % lpath)
1103
            try:
1104
                robj = self.client.get_object_info(rpath)
1105
                if remote_path and self._is_dir(robj):
1106
                    rpath += '/%s' % (short_path.replace(path.sep, '/'))
1107
                    self.client.get_object_info(rpath)
1108
                if not self['overwrite']:
1109
                    raiseCLIError(
1110
                        'Object %s already exists' % rpath,
1111
                        importance=1,
1112
                        details=['use -f to overwrite or resume'])
1113
            except ClientError as ce:
1114
                if ce.status != 404:
1115
                    raise
1116
            self._check_container_limit(lpath)
1117
            yield open(lpath, 'rb'), rpath
1118

    
1119
    @errors.generic.all
1120
    @errors.pithos.connection
1121
    @errors.pithos.container
1122
    @errors.pithos.object_path
1123
    @errors.pithos.local_path
1124
    def _run(self, local_path, remote_path):
1125
        poolsize = self['poolsize']
1126
        if poolsize > 0:
1127
            self.client.MAX_THREADS = int(poolsize)
1128
        params = dict(
1129
            content_encoding=self['content_encoding'],
1130
            content_type=self['content_type'],
1131
            content_disposition=self['content_disposition'],
1132
            sharing=self['sharing'],
1133
            public=self['public'])
1134
        uploaded = []
1135
        container_info_cache = dict()
1136
        for f, rpath in self._path_pairs(local_path, remote_path):
1137
            print('%s --> %s:%s' % (f.name, self.client.container, rpath))
1138
            if self['unchunked']:
1139
                r = self.client.upload_object_unchunked(
1140
                    rpath, f,
1141
                    etag=self['etag'], withHashFile=self['use_hashes'],
1142
                    **params)
1143
                if self['with_output'] or self['json_output']:
1144
                    r['name'] = '%s: %s' % (self.client.container, rpath)
1145
                    uploaded.append(r)
1146
            else:
1147
                try:
1148
                    (progress_bar, upload_cb) = self._safe_progress_bar(
1149
                        'Uploading %s' % f.name.split(path.sep)[-1])
1150
                    if progress_bar:
1151
                        hash_bar = progress_bar.clone()
1152
                        hash_cb = hash_bar.get_generator(
1153
                            'Calculating block hashes')
1154
                    else:
1155
                        hash_cb = None
1156
                    r = self.client.upload_object(
1157
                        rpath, f,
1158
                        hash_cb=hash_cb,
1159
                        upload_cb=upload_cb,
1160
                        container_info_cache=container_info_cache,
1161
                        **params)
1162
                    if self['with_output'] or self['json_output']:
1163
                        r['name'] = '%s: %s' % (self.client.container, rpath)
1164
                        uploaded.append(r)
1165
                except Exception:
1166
                    self._safe_progress_bar_finish(progress_bar)
1167
                    raise
1168
                finally:
1169
                    self._safe_progress_bar_finish(progress_bar)
1170
        self._optional_output(uploaded)
1171
        print('Upload completed')
1172

    
1173
    def main(self, local_path, container____path__=None):
1174
        super(self.__class__, self)._run(container____path__)
1175
        remote_path = self.path or path.basename(path.abspath(local_path))
1176
        self._run(local_path=local_path, remote_path=remote_path)
1177

    
1178

    
1179
@command(pithos_cmds)
1180
class file_cat(_file_container_command):
1181
    """Print remote file contents to console"""
1182

    
1183
    arguments = dict(
1184
        range=RangeArgument('show range of data', '--range'),
1185
        if_match=ValueArgument('show output if ETags match', '--if-match'),
1186
        if_none_match=ValueArgument(
1187
            'show output if ETags match', '--if-none-match'),
1188
        if_modified_since=DateArgument(
1189
            'show output modified since then', '--if-modified-since'),
1190
        if_unmodified_since=DateArgument(
1191
            'show output unmodified since then', '--if-unmodified-since'),
1192
        object_version=ValueArgument(
1193
            'get the specific version', ('-O', '--object-version'))
1194
    )
1195

    
1196
    @errors.generic.all
1197
    @errors.pithos.connection
1198
    @errors.pithos.container
1199
    @errors.pithos.object_path
1200
    def _run(self):
1201
        self.client.download_object(
1202
            self.path,
1203
            stdout,
1204
            range_str=self['range'],
1205
            version=self['object_version'],
1206
            if_match=self['if_match'],
1207
            if_none_match=self['if_none_match'],
1208
            if_modified_since=self['if_modified_since'],
1209
            if_unmodified_since=self['if_unmodified_since'])
1210

    
1211
    def main(self, container___path):
1212
        super(self.__class__, self)._run(
1213
            container___path, path_is_optional=False)
1214
        self._run()
1215

    
1216

    
1217
@command(pithos_cmds)
1218
class file_download(_file_container_command):
1219
    """Download remote object as local file
1220
    If local destination is a directory:
1221
    *   download <container>:<path> <local dir> -R
1222
    will download all files on <container> prefixed as <path>,
1223
    to <local dir>/<full path> (or <local dir>\<full path> in windows)
1224
    *   download <container>:<path> <local dir> --exact-match
1225
    will download only one file, exactly matching <path>
1226
    ATTENTION: to download cont:dir1/dir2/file there must exist objects
1227
    cont:dir1 and cont:dir1/dir2 of type application/directory
1228
    To create directory objects, use /file mkdir
1229
    """
1230

    
1231
    arguments = dict(
1232
        resume=FlagArgument('Resume instead of overwrite', ('-r', '--resume')),
1233
        range=RangeArgument('show range of data', '--range'),
1234
        if_match=ValueArgument('show output if ETags match', '--if-match'),
1235
        if_none_match=ValueArgument(
1236
            'show output if ETags match', '--if-none-match'),
1237
        if_modified_since=DateArgument(
1238
            'show output modified since then', '--if-modified-since'),
1239
        if_unmodified_since=DateArgument(
1240
            'show output unmodified since then', '--if-unmodified-since'),
1241
        object_version=ValueArgument(
1242
            'get the specific version', ('-O', '--object-version')),
1243
        poolsize=IntArgument('set pool size', '--with-pool-size'),
1244
        progress_bar=ProgressBarArgument(
1245
            'do not show progress bar',
1246
            ('-N', '--no-progress-bar'),
1247
            default=False),
1248
        recursive=FlagArgument(
1249
            'Download a remote path and all its contents',
1250
            ('-R', '--recursive'))
1251
    )
1252

    
1253
    def _outputs(self, local_path):
1254
        """:returns: (local_file, remote_path)"""
1255
        remotes = []
1256
        if self['recursive']:
1257
            r = self.client.container_get(
1258
                prefix=self.path or '/',
1259
                if_modified_since=self['if_modified_since'],
1260
                if_unmodified_since=self['if_unmodified_since'])
1261
            dirlist = dict()
1262
            for remote in r.json:
1263
                rname = remote['name'].strip('/')
1264
                tmppath = ''
1265
                for newdir in rname.strip('/').split('/')[:-1]:
1266
                    tmppath = '/'.join([tmppath, newdir])
1267
                    dirlist.update({tmppath.strip('/'): True})
1268
                remotes.append((rname, file_download._is_dir(remote)))
1269
            dir_remotes = [r[0] for r in remotes if r[1]]
1270
            if not set(dirlist).issubset(dir_remotes):
1271
                badguys = [bg.strip('/') for bg in set(
1272
                    dirlist).difference(dir_remotes)]
1273
                raiseCLIError(
1274
                    'Some remote paths contain non existing directories',
1275
                    details=['Missing remote directories:'] + badguys)
1276
        elif self.path:
1277
            r = self.client.get_object_info(
1278
                self.path,
1279
                version=self['object_version'])
1280
            if file_download._is_dir(r):
1281
                raiseCLIError(
1282
                    'Illegal download: Remote object %s is a directory' % (
1283
                        self.path),
1284
                    details=['To download a directory, try --recursive or -R'])
1285
            if '/' in self.path.strip('/') and not local_path:
1286
                raiseCLIError(
1287
                    'Illegal download: remote object %s contains "/"' % (
1288
                        self.path),
1289
                    details=[
1290
                        'To download an object containing "/" characters',
1291
                        'either create the remote directories or',
1292
                        'specify a non-directory local path for this object'])
1293
            remotes = [(self.path, False)]
1294
        if not remotes:
1295
            if self.path:
1296
                raiseCLIError(
1297
                    'No matching path %s on container %s' % (
1298
                        self.path, self.container),
1299
                    details=[
1300
                        'To list the contents of %s, try:' % self.container,
1301
                        '   /file list %s' % self.container])
1302
            raiseCLIError(
1303
                'Illegal download of container %s' % self.container,
1304
                details=[
1305
                    'To download a whole container, try:',
1306
                    '   /file download --recursive <container>'])
1307

    
1308
        lprefix = path.abspath(local_path or path.curdir)
1309
        if path.isdir(lprefix):
1310
            for rpath, remote_is_dir in remotes:
1311
                lpath = path.sep.join([
1312
                    lprefix[:-1] if lprefix.endswith(path.sep) else lprefix,
1313
                    rpath.strip('/').replace('/', path.sep)])
1314
                if remote_is_dir:
1315
                    if path.exists(lpath) and path.isdir(lpath):
1316
                        continue
1317
                    makedirs(lpath)
1318
                elif path.exists(lpath):
1319
                    if not self['resume']:
1320
                        print('File %s exists, aborting...' % lpath)
1321
                        continue
1322
                    with open(lpath, 'rwb+') as f:
1323
                        yield (f, rpath)
1324
                else:
1325
                    with open(lpath, 'wb+') as f:
1326
                        yield (f, rpath)
1327
        elif path.exists(lprefix):
1328
            if len(remotes) > 1:
1329
                raiseCLIError(
1330
                    '%s remote objects cannot be merged in local file %s' % (
1331
                        len(remotes),
1332
                        local_path),
1333
                    details=[
1334
                        'To download multiple objects, local path should be',
1335
                        'a directory, or use download without a local path'])
1336
            (rpath, remote_is_dir) = remotes[0]
1337
            if remote_is_dir:
1338
                raiseCLIError(
1339
                    'Remote directory %s should not replace local file %s' % (
1340
                        rpath,
1341
                        local_path))
1342
            if self['resume']:
1343
                with open(lprefix, 'rwb+') as f:
1344
                    yield (f, rpath)
1345
            else:
1346
                raiseCLIError(
1347
                    'Local file %s already exist' % local_path,
1348
                    details=['Try --resume to overwrite it'])
1349
        else:
1350
            if len(remotes) > 1 or remotes[0][1]:
1351
                raiseCLIError(
1352
                    'Local directory %s does not exist' % local_path)
1353
            with open(lprefix, 'wb+') as f:
1354
                yield (f, remotes[0][0])
1355

    
1356
    @errors.generic.all
1357
    @errors.pithos.connection
1358
    @errors.pithos.container
1359
    @errors.pithos.object_path
1360
    @errors.pithos.local_path
1361
    def _run(self, local_path):
1362
        poolsize = self['poolsize']
1363
        if poolsize:
1364
            self.client.MAX_THREADS = int(poolsize)
1365
        progress_bar = None
1366
        try:
1367
            for f, rpath in self._outputs(local_path):
1368
                (
1369
                    progress_bar,
1370
                    download_cb) = self._safe_progress_bar(
1371
                        'Download %s' % rpath)
1372
                self.client.download_object(
1373
                    rpath, f,
1374
                    download_cb=download_cb,
1375
                    range_str=self['range'],
1376
                    version=self['object_version'],
1377
                    if_match=self['if_match'],
1378
                    resume=self['resume'],
1379
                    if_none_match=self['if_none_match'],
1380
                    if_modified_since=self['if_modified_since'],
1381
                    if_unmodified_since=self['if_unmodified_since'])
1382
        except KeyboardInterrupt:
1383
            from threading import activeCount, enumerate as activethreads
1384
            timeout = 0.5
1385
            while activeCount() > 1:
1386
                stdout.write('\nCancel %s threads: ' % (activeCount() - 1))
1387
                stdout.flush()
1388
                for thread in activethreads():
1389
                    try:
1390
                        thread.join(timeout)
1391
                        stdout.write('.' if thread.isAlive() else '*')
1392
                    except RuntimeError:
1393
                        continue
1394
                    finally:
1395
                        stdout.flush()
1396
                        timeout += 0.1
1397
            print('\nDownload canceled by user')
1398
            if local_path is not None:
1399
                print('to resume, re-run with --resume')
1400
        except Exception:
1401
            self._safe_progress_bar_finish(progress_bar)
1402
            raise
1403
        finally:
1404
            self._safe_progress_bar_finish(progress_bar)
1405

    
1406
    def main(self, container___path, local_path=None):
1407
        super(self.__class__, self)._run(container___path)
1408
        self._run(local_path=local_path)
1409

    
1410

    
1411
@command(pithos_cmds)
1412
class file_hashmap(_file_container_command, _optional_json):
1413
    """Get the hash-map of an object"""
1414

    
1415
    arguments = dict(
1416
        if_match=ValueArgument('show output if ETags match', '--if-match'),
1417
        if_none_match=ValueArgument(
1418
            'show output if ETags match', '--if-none-match'),
1419
        if_modified_since=DateArgument(
1420
            'show output modified since then', '--if-modified-since'),
1421
        if_unmodified_since=DateArgument(
1422
            'show output unmodified since then', '--if-unmodified-since'),
1423
        object_version=ValueArgument(
1424
            'get the specific version', ('-O', '--object-version'))
1425
    )
1426

    
1427
    @errors.generic.all
1428
    @errors.pithos.connection
1429
    @errors.pithos.container
1430
    @errors.pithos.object_path
1431
    def _run(self):
1432
        self._print(self.client.get_object_hashmap(
1433
            self.path,
1434
            version=self['object_version'],
1435
            if_match=self['if_match'],
1436
            if_none_match=self['if_none_match'],
1437
            if_modified_since=self['if_modified_since'],
1438
            if_unmodified_since=self['if_unmodified_since']), print_dict)
1439

    
1440
    def main(self, container___path):
1441
        super(self.__class__, self)._run(
1442
            container___path,
1443
            path_is_optional=False)
1444
        self._run()
1445

    
1446

    
1447
@command(pithos_cmds)
1448
class file_delete(_file_container_command, _optional_output_cmd):
1449
    """Delete a container [or an object]
1450
    How to delete a non-empty container:
1451
    - empty the container:  /file delete -R <container>
1452
    - delete it:            /file delete <container>
1453
    .
1454
    Semantics of directory deletion:
1455
    .a preserve the contents: /file delete <container>:<directory>
1456
    .    objects of the form dir/filename can exist with a dir object
1457
    .b delete contents:       /file delete -R <container>:<directory>
1458
    .    all dir/* objects are affected, even if dir does not exist
1459
    .
1460
    To restore a deleted object OBJ in a container CONT:
1461
    - get object versions: /file versions CONT:OBJ
1462
    .   and choose the version to be restored
1463
    - restore the object:  /file copy --source-version=<version> CONT:OBJ OBJ
1464
    """
1465

    
1466
    arguments = dict(
1467
        until=DateArgument('remove history until that date', '--until'),
1468
        yes=FlagArgument('Do not prompt for permission', '--yes'),
1469
        recursive=FlagArgument(
1470
            'empty dir or container and delete (if dir)',
1471
            ('-R', '--recursive'))
1472
    )
1473

    
1474
    def __init__(self, arguments={}, auth_base=None, cloud=None):
1475
        super(self.__class__, self).__init__(arguments,  auth_base, cloud)
1476
        self['delimiter'] = DelimiterArgument(
1477
            self,
1478
            parsed_name='--delimiter',
1479
            help='delete objects prefixed with <object><delimiter>')
1480

    
1481
    @errors.generic.all
1482
    @errors.pithos.connection
1483
    @errors.pithos.container
1484
    @errors.pithos.object_path
1485
    def _run(self):
1486
        if self.path:
1487
            if self['yes'] or ask_user(
1488
                    'Delete %s:%s ?' % (self.container, self.path)):
1489
                self._optional_output(self.client.del_object(
1490
                    self.path,
1491
                    until=self['until'], delimiter=self['delimiter']))
1492
            else:
1493
                print('Aborted')
1494
        else:
1495
            if self['recursive']:
1496
                ask_msg = 'Delete container contents'
1497
            else:
1498
                ask_msg = 'Delete container'
1499
            if self['yes'] or ask_user('%s %s ?' % (ask_msg, self.container)):
1500
                self._optional_output(self.client.del_container(
1501
                    until=self['until'], delimiter=self['delimiter']))
1502
            else:
1503
                print('Aborted')
1504

    
1505
    def main(self, container____path__=None):
1506
        super(self.__class__, self)._run(container____path__)
1507
        self._run()
1508

    
1509

    
1510
@command(pithos_cmds)
1511
class file_purge(_file_container_command, _optional_output_cmd):
1512
    """Delete a container and release related data blocks
1513
    Non-empty containers can not purged.
1514
    To purge a container with content:
1515
    .   /file delete -R <container>
1516
    .      objects are deleted, but data blocks remain on server
1517
    .   /file purge <container>
1518
    .      container and data blocks are released and deleted
1519
    """
1520

    
1521
    arguments = dict(
1522
        yes=FlagArgument('Do not prompt for permission', '--yes'),
1523
        force=FlagArgument('purge even if not empty', ('-F', '--force'))
1524
    )
1525

    
1526
    @errors.generic.all
1527
    @errors.pithos.connection
1528
    @errors.pithos.container
1529
    def _run(self):
1530
        if self['yes'] or ask_user('Purge container %s?' % self.container):
1531
            try:
1532
                r = self.client.purge_container()
1533
            except ClientError as ce:
1534
                if ce.status in (409,):
1535
                    if self['force']:
1536
                        self.client.del_container(delimiter='/')
1537
                        r = self.client.purge_container()
1538
                    else:
1539
                        raiseCLIError(ce, details=['Try -F to force-purge'])
1540
                else:
1541
                    raise
1542
            self._optional_output(r)
1543
        else:
1544
            print('Aborted')
1545

    
1546
    def main(self, container=None):
1547
        super(self.__class__, self)._run(container)
1548
        if container and self.container != container:
1549
            raiseCLIError('Invalid container name %s' % container, details=[
1550
                'Did you mean "%s" ?' % self.container,
1551
                'Use --container for names containing :'])
1552
        self._run()
1553

    
1554

    
1555
@command(pithos_cmds)
1556
class file_publish(_file_container_command):
1557
    """Publish the object and print the public url"""
1558

    
1559
    @errors.generic.all
1560
    @errors.pithos.connection
1561
    @errors.pithos.container
1562
    @errors.pithos.object_path
1563
    def _run(self):
1564
        print self.client.publish_object(self.path)
1565

    
1566
    def main(self, container___path):
1567
        super(self.__class__, self)._run(
1568
            container___path, path_is_optional=False)
1569
        self._run()
1570

    
1571

    
1572
@command(pithos_cmds)
1573
class file_unpublish(_file_container_command, _optional_output_cmd):
1574
    """Unpublish an object"""
1575

    
1576
    @errors.generic.all
1577
    @errors.pithos.connection
1578
    @errors.pithos.container
1579
    @errors.pithos.object_path
1580
    def _run(self):
1581
            self._optional_output(self.client.unpublish_object(self.path))
1582

    
1583
    def main(self, container___path):
1584
        super(self.__class__, self)._run(
1585
            container___path, path_is_optional=False)
1586
        self._run()
1587

    
1588

    
1589
@command(pithos_cmds)
1590
class file_permissions(_pithos_init):
1591
    """Manage user and group accessibility for objects
1592
    Permissions are lists of users and user groups. There are read and write
1593
    permissions. Users and groups with write permission have also read
1594
    permission.
1595
    """
1596

    
1597

    
1598
def print_permissions(permissions_dict):
1599
    expected_keys = ('read', 'write')
1600
    if set(permissions_dict).issubset(expected_keys):
1601
        print_dict(permissions_dict)
1602
    else:
1603
        invalid_keys = set(permissions_dict.keys()).difference(expected_keys)
1604
        raiseCLIError(
1605
            'Illegal permission keys: %s' % ', '.join(invalid_keys),
1606
            importance=1, details=[
1607
                'Valid permission types: %s' % ' '.join(expected_keys)])
1608

    
1609

    
1610
@command(pithos_cmds)
1611
class file_permissions_get(_file_container_command, _optional_json):
1612
    """Get read and write permissions of an object"""
1613

    
1614
    @errors.generic.all
1615
    @errors.pithos.connection
1616
    @errors.pithos.container
1617
    @errors.pithos.object_path
1618
    def _run(self):
1619
        self._print(
1620
            self.client.get_object_sharing(self.path), print_permissions)
1621

    
1622
    def main(self, container___path):
1623
        super(self.__class__, self)._run(
1624
            container___path, path_is_optional=False)
1625
        self._run()
1626

    
1627

    
1628
@command(pithos_cmds)
1629
class file_permissions_set(_file_container_command, _optional_output_cmd):
1630
    """Set permissions for an object
1631
    New permissions overwrite existing permissions.
1632
    Permission format:
1633
    -   read=<username>[,usergroup[,...]]
1634
    -   write=<username>[,usegroup[,...]]
1635
    E.g. to give read permissions for file F to users A and B and write for C:
1636
    .       /file permissions set F read=A,B write=C
1637
    """
1638

    
1639
    @errors.generic.all
1640
    def format_permission_dict(self, permissions):
1641
        read = False
1642
        write = False
1643
        for perms in permissions:
1644
            splstr = perms.split('=')
1645
            if 'read' == splstr[0]:
1646
                read = [ug.strip() for ug in splstr[1].split(',')]
1647
            elif 'write' == splstr[0]:
1648
                write = [ug.strip() for ug in splstr[1].split(',')]
1649
            else:
1650
                msg = 'Usage:\tread=<groups,users> write=<groups,users>'
1651
                raiseCLIError(None, msg)
1652
        return (read, write)
1653

    
1654
    @errors.generic.all
1655
    @errors.pithos.connection
1656
    @errors.pithos.container
1657
    @errors.pithos.object_path
1658
    def _run(self, read, write):
1659
        self._optional_output(self.client.set_object_sharing(
1660
            self.path, read_permission=read, write_permission=write))
1661

    
1662
    def main(self, container___path, *permissions):
1663
        super(self.__class__, self)._run(
1664
            container___path, path_is_optional=False)
1665
        read, write = self.format_permission_dict(permissions)
1666
        self._run(read, write)
1667

    
1668

    
1669
@command(pithos_cmds)
1670
class file_permissions_delete(_file_container_command, _optional_output_cmd):
1671
    """Delete all permissions set on object
1672
    To modify permissions, use /file permissions set
1673
    """
1674

    
1675
    @errors.generic.all
1676
    @errors.pithos.connection
1677
    @errors.pithos.container
1678
    @errors.pithos.object_path
1679
    def _run(self):
1680
        self._optional_output(self.client.del_object_sharing(self.path))
1681

    
1682
    def main(self, container___path):
1683
        super(self.__class__, self)._run(
1684
            container___path, path_is_optional=False)
1685
        self._run()
1686

    
1687

    
1688
@command(pithos_cmds)
1689
class file_info(_file_container_command, _optional_json):
1690
    """Get detailed information for user account, containers or objects
1691
    to get account info:    /file info
1692
    to get container info:  /file info <container>
1693
    to get object info:     /file info <container>:<path>
1694
    """
1695

    
1696
    arguments = dict(
1697
        object_version=ValueArgument(
1698
            'show specific version \ (applies only for objects)',
1699
            ('-O', '--object-version'))
1700
    )
1701

    
1702
    @errors.generic.all
1703
    @errors.pithos.connection
1704
    @errors.pithos.container
1705
    @errors.pithos.object_path
1706
    def _run(self):
1707
        if self.container is None:
1708
            r = self.client.get_account_info()
1709
        elif self.path is None:
1710
            r = self.client.get_container_info(self.container)
1711
        else:
1712
            r = self.client.get_object_info(
1713
                self.path, version=self['object_version'])
1714
        self._print(r, print_dict)
1715

    
1716
    def main(self, container____path__=None):
1717
        super(self.__class__, self)._run(container____path__)
1718
        self._run()
1719

    
1720

    
1721
@command(pithos_cmds)
1722
class file_metadata(_pithos_init):
1723
    """Metadata are attached on objects. They are formed as key:value pairs.
1724
    They can have arbitary values.
1725
    """
1726

    
1727

    
1728
@command(pithos_cmds)
1729
class file_metadata_get(_file_container_command, _optional_json):
1730
    """Get metadata for account, containers or objects"""
1731

    
1732
    arguments = dict(
1733
        detail=FlagArgument('show detailed output', ('-l', '--details')),
1734
        until=DateArgument('show metadata until then', '--until'),
1735
        object_version=ValueArgument(
1736
            'show specific version (applies only for objects)',
1737
            ('-O', '--object-version'))
1738
    )
1739

    
1740
    @errors.generic.all
1741
    @errors.pithos.connection
1742
    @errors.pithos.container
1743
    @errors.pithos.object_path
1744
    def _run(self):
1745
        until = self['until']
1746
        r = None
1747
        if self.container is None:
1748
            if self['detail']:
1749
                r = self.client.get_account_info(until=until)
1750
            else:
1751
                r = self.client.get_account_meta(until=until)
1752
                r = pretty_keys(r, '-')
1753
        elif self.path is None:
1754
            if self['detail']:
1755
                r = self.client.get_container_info(until=until)
1756
            else:
1757
                cmeta = self.client.get_container_meta(until=until)
1758
                ometa = self.client.get_container_object_meta(until=until)
1759
                r = {}
1760
                if cmeta:
1761
                    r['container-meta'] = pretty_keys(cmeta, '-')
1762
                if ometa:
1763
                    r['object-meta'] = pretty_keys(ometa, '-')
1764
        else:
1765
            if self['detail']:
1766
                r = self.client.get_object_info(
1767
                    self.path,
1768
                    version=self['object_version'])
1769
            else:
1770
                r = self.client.get_object_meta(
1771
                    self.path,
1772
                    version=self['object_version'])
1773
                r = pretty_keys(pretty_keys(r, '-'))
1774
        if r:
1775
            self._print(r, print_dict)
1776

    
1777
    def main(self, container____path__=None):
1778
        super(self.__class__, self)._run(container____path__)
1779
        self._run()
1780

    
1781

    
1782
@command(pithos_cmds)
1783
class file_metadata_set(_file_container_command, _optional_output_cmd):
1784
    """Set a piece of metadata for account, container or object"""
1785

    
1786
    @errors.generic.all
1787
    @errors.pithos.connection
1788
    @errors.pithos.container
1789
    @errors.pithos.object_path
1790
    def _run(self, metakey, metaval):
1791
        if not self.container:
1792
            r = self.client.set_account_meta({metakey: metaval})
1793
        elif not self.path:
1794
            r = self.client.set_container_meta({metakey: metaval})
1795
        else:
1796
            r = self.client.set_object_meta(self.path, {metakey: metaval})
1797
        self._optional_output(r)
1798

    
1799
    def main(self, metakey, metaval, container____path__=None):
1800
        super(self.__class__, self)._run(container____path__)
1801
        self._run(metakey=metakey, metaval=metaval)
1802

    
1803

    
1804
@command(pithos_cmds)
1805
class file_metadata_delete(_file_container_command, _optional_output_cmd):
1806
    """Delete metadata with given key from account, container or object
1807
    - to get metadata of current account: /file metadata get
1808
    - to get metadata of a container:     /file metadata get <container>
1809
    - to get metadata of an object:       /file metadata get <container>:<path>
1810
    """
1811

    
1812
    @errors.generic.all
1813
    @errors.pithos.connection
1814
    @errors.pithos.container
1815
    @errors.pithos.object_path
1816
    def _run(self, metakey):
1817
        if self.container is None:
1818
            r = self.client.del_account_meta(metakey)
1819
        elif self.path is None:
1820
            r = self.client.del_container_meta(metakey)
1821
        else:
1822
            r = self.client.del_object_meta(self.path, metakey)
1823
        self._optional_output(r)
1824

    
1825
    def main(self, metakey, container____path__=None):
1826
        super(self.__class__, self)._run(container____path__)
1827
        self._run(metakey)
1828

    
1829

    
1830
@command(pithos_cmds)
1831
class file_quota(_file_account_command, _optional_json):
1832
    """Get account quota"""
1833

    
1834
    arguments = dict(
1835
        in_bytes=FlagArgument('Show result in bytes', ('-b', '--bytes'))
1836
    )
1837

    
1838
    @errors.generic.all
1839
    @errors.pithos.connection
1840
    def _run(self):
1841

    
1842
        def pretty_print(output):
1843
            if not self['in_bytes']:
1844
                for k in output:
1845
                    output[k] = format_size(output[k])
1846
            pretty_dict(output, '-')
1847

    
1848
        self._print(self.client.get_account_quota(), pretty_print)
1849

    
1850
    def main(self, custom_uuid=None):
1851
        super(self.__class__, self)._run(custom_account=custom_uuid)
1852
        self._run()
1853

    
1854

    
1855
@command(pithos_cmds)
1856
class file_containerlimit(_pithos_init):
1857
    """Container size limit commands"""
1858

    
1859

    
1860
@command(pithos_cmds)
1861
class file_containerlimit_get(_file_container_command, _optional_json):
1862
    """Get container size limit"""
1863

    
1864
    arguments = dict(
1865
        in_bytes=FlagArgument('Show result in bytes', ('-b', '--bytes'))
1866
    )
1867

    
1868
    @errors.generic.all
1869
    @errors.pithos.container
1870
    def _run(self):
1871

    
1872
        def pretty_print(output):
1873
            if not self['in_bytes']:
1874
                for k, v in output.items():
1875
                    output[k] = 'unlimited' if '0' == v else format_size(v)
1876
            pretty_dict(output, '-')
1877

    
1878
        self._print(
1879
            self.client.get_container_limit(self.container), pretty_print)
1880

    
1881
    def main(self, container=None):
1882
        super(self.__class__, self)._run()
1883
        self.container = container
1884
        self._run()
1885

    
1886

    
1887
@command(pithos_cmds)
1888
class file_containerlimit_set(_file_account_command, _optional_output_cmd):
1889
    """Set new storage limit for a container
1890
    By default, the limit is set in bytes
1891
    Users may specify a different unit, e.g:
1892
    /file containerlimit set 2.3GB mycontainer
1893
    Valid units: B, KiB (1024 B), KB (1000 B), MiB, MB, GiB, GB, TiB, TB
1894
    To set container limit to "unlimited", use 0
1895
    """
1896

    
1897
    @errors.generic.all
1898
    def _calculate_limit(self, user_input):
1899
        limit = 0
1900
        try:
1901
            limit = int(user_input)
1902
        except ValueError:
1903
            index = 0
1904
            digits = [str(num) for num in range(0, 10)] + ['.']
1905
            while user_input[index] in digits:
1906
                index += 1
1907
            limit = user_input[:index]
1908
            format = user_input[index:]
1909
            try:
1910
                return to_bytes(limit, format)
1911
            except Exception as qe:
1912
                msg = 'Failed to convert %s to bytes' % user_input,
1913
                raiseCLIError(qe, msg, details=[
1914
                    'Syntax: containerlimit set <limit>[format] [container]',
1915
                    'e.g.: containerlimit set 2.3GB mycontainer',
1916
                    'Valid formats:',
1917
                    '(*1024): B, KiB, MiB, GiB, TiB',
1918
                    '(*1000): B, KB, MB, GB, TB'])
1919
        return limit
1920

    
1921
    @errors.generic.all
1922
    @errors.pithos.connection
1923
    @errors.pithos.container
1924
    def _run(self, limit):
1925
        if self.container:
1926
            self.client.container = self.container
1927
        self._optional_output(self.client.set_container_limit(limit))
1928

    
1929
    def main(self, limit, container=None):
1930
        super(self.__class__, self)._run()
1931
        limit = self._calculate_limit(limit)
1932
        self.container = container
1933
        self._run(limit)
1934

    
1935

    
1936
@command(pithos_cmds)
1937
class file_versioning(_pithos_init):
1938
    """Manage the versioning scheme of current pithos user account"""
1939

    
1940

    
1941
@command(pithos_cmds)
1942
class file_versioning_get(_file_account_command, _optional_json):
1943
    """Get  versioning for account or container"""
1944

    
1945
    @errors.generic.all
1946
    @errors.pithos.connection
1947
    @errors.pithos.container
1948
    def _run(self):
1949
        self._print(
1950
            self.client.get_container_versioning(self.container), print_dict)
1951

    
1952
    def main(self, container):
1953
        super(self.__class__, self)._run()
1954
        self.container = container
1955
        self._run()
1956

    
1957

    
1958
@command(pithos_cmds)
1959
class file_versioning_set(_file_account_command, _optional_output_cmd):
1960
    """Set versioning mode (auto, none) for account or container"""
1961

    
1962
    def _check_versioning(self, versioning):
1963
        if versioning and versioning.lower() in ('auto', 'none'):
1964
            return versioning.lower()
1965
        raiseCLIError('Invalid versioning %s' % versioning, details=[
1966
            'Versioning can be auto or none'])
1967

    
1968
    @errors.generic.all
1969
    @errors.pithos.connection
1970
    @errors.pithos.container
1971
    def _run(self, versioning):
1972
        self.client.container = self.container
1973
        r = self.client.set_container_versioning(versioning)
1974
        self._optional_output(r)
1975

    
1976
    def main(self, versioning, container):
1977
        super(self.__class__, self)._run()
1978
        self._run(self._check_versioning(versioning))
1979

    
1980

    
1981
@command(pithos_cmds)
1982
class file_group(_pithos_init):
1983
    """Manage access groups and group members"""
1984

    
1985

    
1986
@command(pithos_cmds)
1987
class file_group_list(_file_account_command, _optional_json):
1988
    """list all groups and group members"""
1989

    
1990
    @errors.generic.all
1991
    @errors.pithos.connection
1992
    def _run(self):
1993
        self._print(self.client.get_account_group(), pretty_dict, delim='-')
1994

    
1995
    def main(self):
1996
        super(self.__class__, self)._run()
1997
        self._run()
1998

    
1999

    
2000
@command(pithos_cmds)
2001
class file_group_set(_file_account_command, _optional_output_cmd):
2002
    """Set a user group"""
2003

    
2004
    @errors.generic.all
2005
    @errors.pithos.connection
2006
    def _run(self, groupname, *users):
2007
        self._optional_output(self.client.set_account_group(groupname, users))
2008

    
2009
    def main(self, groupname, *users):
2010
        super(self.__class__, self)._run()
2011
        if users:
2012
            self._run(groupname, *users)
2013
        else:
2014
            raiseCLIError('No users to add in group %s' % groupname)
2015

    
2016

    
2017
@command(pithos_cmds)
2018
class file_group_delete(_file_account_command, _optional_output_cmd):
2019
    """Delete a user group"""
2020

    
2021
    @errors.generic.all
2022
    @errors.pithos.connection
2023
    def _run(self, groupname):
2024
        self._optional_output(self.client.del_account_group(groupname))
2025

    
2026
    def main(self, groupname):
2027
        super(self.__class__, self)._run()
2028
        self._run(groupname)
2029

    
2030

    
2031
@command(pithos_cmds)
2032
class file_sharers(_file_account_command, _optional_json):
2033
    """List the accounts that share objects with current user"""
2034

    
2035
    arguments = dict(
2036
        detail=FlagArgument('show detailed output', ('-l', '--details')),
2037
        marker=ValueArgument('show output greater then marker', '--marker')
2038
    )
2039

    
2040
    @errors.generic.all
2041
    @errors.pithos.connection
2042
    def _run(self):
2043
        accounts = self.client.get_sharing_accounts(marker=self['marker'])
2044
        if self['json_output'] or self['detail']:
2045
            self._print(accounts)
2046
        else:
2047
            self._print([acc['name'] for acc in accounts])
2048

    
2049
    def main(self):
2050
        super(self.__class__, self)._run()
2051
        self._run()
2052

    
2053

    
2054
def version_print(versions):
2055
    print_items([dict(id=vitem[0], created=strftime(
2056
        '%d-%m-%Y %H:%M:%S',
2057
        localtime(float(vitem[1])))) for vitem in versions])
2058

    
2059

    
2060
@command(pithos_cmds)
2061
class file_versions(_file_container_command, _optional_json):
2062
    """Get the list of object versions
2063
    Deleted objects may still have versions that can be used to restore it and
2064
    get information about its previous state.
2065
    The version number can be used in a number of other commands, like info,
2066
    copy, move, meta. See these commands for more information, e.g.
2067
    /file info -h
2068
    """
2069

    
2070
    @errors.generic.all
2071
    @errors.pithos.connection
2072
    @errors.pithos.container
2073
    @errors.pithos.object_path
2074
    def _run(self):
2075
        self._print(
2076
            self.client.get_object_versionlist(self.path), version_print)
2077

    
2078
    def main(self, container___path):
2079
        super(file_versions, self)._run(
2080
            container___path,
2081
            path_is_optional=False)
2082
        self._run()