Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (77.3 kB)

1 e3f01d64 Stavros Sachtouris
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 7493ccb6 Stavros Sachtouris
#
3 7493ccb6 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 7493ccb6 Stavros Sachtouris
# without modification, are permitted provided that the following
5 7493ccb6 Stavros Sachtouris
# conditions are met:
6 7493ccb6 Stavros Sachtouris
#
7 7493ccb6 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 7493ccb6 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 7493ccb6 Stavros Sachtouris
#      disclaimer.
10 7493ccb6 Stavros Sachtouris
#
11 7493ccb6 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 7493ccb6 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 7493ccb6 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 7493ccb6 Stavros Sachtouris
#      provided with the distribution.
15 7493ccb6 Stavros Sachtouris
#
16 7493ccb6 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 7493ccb6 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 7493ccb6 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 7493ccb6 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 7493ccb6 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 7493ccb6 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 7493ccb6 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 7493ccb6 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 7493ccb6 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 7493ccb6 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 7493ccb6 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 7493ccb6 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 7493ccb6 Stavros Sachtouris
#
29 7493ccb6 Stavros Sachtouris
# The views and conclusions contained in the software and
30 7493ccb6 Stavros Sachtouris
# documentation are those of the authors and should not be
31 7493ccb6 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 7493ccb6 Stavros Sachtouris
# or implied, of GRNET S.A.command
33 7493ccb6 Stavros Sachtouris
34 1395c40e Stavros Sachtouris
from sys import stdout
35 1395c40e Stavros Sachtouris
from time import localtime, strftime
36 b666ef82 Stavros Sachtouris
from os import path, makedirs, walk
37 1395c40e Stavros Sachtouris
38 234954d1 Stavros Sachtouris
from kamaki.cli import command
39 d486baec Stavros Sachtouris
from kamaki.cli.command_tree import CommandTree
40 8cec3671 Stavros Sachtouris
from kamaki.cli.errors import raiseCLIError, CLISyntaxError, CLIBaseUrlError
41 7147e1ca Stavros Sachtouris
from kamaki.cli.utils import (
42 545c6c29 Stavros Sachtouris
    format_size, to_bytes, print_dict, print_items, pretty_keys, pretty_dict,
43 0399ac7e Stavros Sachtouris
    page_hold, bold, ask_user, get_path_size, print_json)
44 e3d4d442 Stavros Sachtouris
from kamaki.cli.argument import FlagArgument, ValueArgument, IntArgument
45 04d01cd4 Stavros Sachtouris
from kamaki.cli.argument import KeyValueArgument, DateArgument
46 fd1f1d96 Stavros Sachtouris
from kamaki.cli.argument import ProgressBarArgument
47 545c6c29 Stavros Sachtouris
from kamaki.cli.commands import _command_init, errors
48 b4f69041 Stavros Sachtouris
from kamaki.cli.commands import addLogSettings, DontRaiseKeyError
49 545c6c29 Stavros Sachtouris
from kamaki.cli.commands import _optional_output_cmd, _optional_json
50 7493ccb6 Stavros Sachtouris
from kamaki.clients.pithos import PithosClient, ClientError
51 1f5debf7 Stavros Sachtouris
from kamaki.clients.astakos import AstakosClient
52 1395c40e Stavros Sachtouris
53 a29d2f88 Stavros Sachtouris
pithos_cmds = CommandTree('file', 'Pithos+/Storage API commands')
54 d486baec Stavros Sachtouris
_commands = [pithos_cmds]
55 234954d1 Stavros Sachtouris
56 7493ccb6 Stavros Sachtouris
57 234954d1 Stavros Sachtouris
# Argument functionality
58 234954d1 Stavros Sachtouris
59 efdee310 Stavros Sachtouris
class DelimiterArgument(ValueArgument):
60 befed235 Stavros Sachtouris
    """
61 befed235 Stavros Sachtouris
    :value type: string
62 befed235 Stavros Sachtouris
    :value returns: given string or /
63 03d661d8 Stavros Sachtouris
    """
64 03d661d8 Stavros Sachtouris
65 efdee310 Stavros Sachtouris
    def __init__(self, caller_obj, help='', parsed_name=None, default=None):
66 efdee310 Stavros Sachtouris
        super(DelimiterArgument, self).__init__(help, parsed_name, default)
67 efdee310 Stavros Sachtouris
        self.caller_obj = caller_obj
68 efdee310 Stavros Sachtouris
69 234954d1 Stavros Sachtouris
    @property
70 efdee310 Stavros Sachtouris
    def value(self):
71 2fe2672e Stavros Sachtouris
        if self.caller_obj['recursive']:
72 efdee310 Stavros Sachtouris
            return '/'
73 efdee310 Stavros Sachtouris
        return getattr(self, '_value', self.default)
74 234954d1 Stavros Sachtouris
75 234954d1 Stavros Sachtouris
    @value.setter
76 efdee310 Stavros Sachtouris
    def value(self, newvalue):
77 efdee310 Stavros Sachtouris
        self._value = newvalue
78 efdee310 Stavros Sachtouris
79 234954d1 Stavros Sachtouris
80 befed235 Stavros Sachtouris
class SharingArgument(ValueArgument):
81 befed235 Stavros Sachtouris
    """Set sharing (read and/or write) groups
82 439826ec Stavros Sachtouris
    .
83 befed235 Stavros Sachtouris
    :value type: "read=term1,term2,... write=term1,term2,..."
84 439826ec Stavros Sachtouris
    .
85 befed235 Stavros Sachtouris
    :value returns: {'read':['term1', 'term2', ...],
86 439826ec Stavros Sachtouris
    .   'write':['term1', 'term2', ...]}
87 befed235 Stavros Sachtouris
    """
88 234954d1 Stavros Sachtouris
89 234954d1 Stavros Sachtouris
    @property
90 e3d4d442 Stavros Sachtouris
    def value(self):
91 e3d4d442 Stavros Sachtouris
        return getattr(self, '_value', self.default)
92 234954d1 Stavros Sachtouris
93 e3d4d442 Stavros Sachtouris
    @value.setter
94 e3d4d442 Stavros Sachtouris
    def value(self, newvalue):
95 e3d4d442 Stavros Sachtouris
        perms = {}
96 e3d4d442 Stavros Sachtouris
        try:
97 e3d4d442 Stavros Sachtouris
            permlist = newvalue.split(' ')
98 e3d4d442 Stavros Sachtouris
        except AttributeError:
99 e3d4d442 Stavros Sachtouris
            return
100 e3d4d442 Stavros Sachtouris
        for p in permlist:
101 e3d4d442 Stavros Sachtouris
            try:
102 234954d1 Stavros Sachtouris
                (key, val) = p.split('=')
103 83ba5545 Stavros Sachtouris
            except ValueError as err:
104 24ff0a35 Stavros Sachtouris
                raiseCLIError(
105 24ff0a35 Stavros Sachtouris
                    err,
106 24ff0a35 Stavros Sachtouris
                    'Error in --sharing',
107 234954d1 Stavros Sachtouris
                    details='Incorrect format',
108 234954d1 Stavros Sachtouris
                    importance=1)
109 e3d4d442 Stavros Sachtouris
            if key.lower() not in ('read', 'write'):
110 de73876b Stavros Sachtouris
                msg = 'Error in --sharing'
111 de73876b Stavros Sachtouris
                raiseCLIError(err, msg, importance=1, details=[
112 de73876b Stavros Sachtouris
                    'Invalid permission key %s' % key])
113 e3d4d442 Stavros Sachtouris
            val_list = val.split(',')
114 234954d1 Stavros Sachtouris
            if not key in perms:
115 234954d1 Stavros Sachtouris
                perms[key] = []
116 e3d4d442 Stavros Sachtouris
            for item in val_list:
117 e3d4d442 Stavros Sachtouris
                if item not in perms[key]:
118 e3d4d442 Stavros Sachtouris
                    perms[key].append(item)
119 e3d4d442 Stavros Sachtouris
        self._value = perms
120 e3d4d442 Stavros Sachtouris
121 234954d1 Stavros Sachtouris
122 e3d4d442 Stavros Sachtouris
class RangeArgument(ValueArgument):
123 befed235 Stavros Sachtouris
    """
124 439826ec Stavros Sachtouris
    :value type: string of the form <start>-<end> where <start> and <end> are
125 439826ec Stavros Sachtouris
        integers
126 befed235 Stavros Sachtouris
    :value returns: the input string, after type checking <start> and <end>
127 befed235 Stavros Sachtouris
    """
128 befed235 Stavros Sachtouris
129 234954d1 Stavros Sachtouris
    @property
130 e3d4d442 Stavros Sachtouris
    def value(self):
131 e3d4d442 Stavros Sachtouris
        return getattr(self, '_value', self.default)
132 234954d1 Stavros Sachtouris
133 e3d4d442 Stavros Sachtouris
    @value.setter
134 e3d4d442 Stavros Sachtouris
    def value(self, newvalue):
135 e3d4d442 Stavros Sachtouris
        if newvalue is None:
136 e3d4d442 Stavros Sachtouris
            self._value = self.default
137 e3d4d442 Stavros Sachtouris
            return
138 9ceec15a Stavros Sachtouris
        (start, end) = newvalue.split('-')
139 e3d4d442 Stavros Sachtouris
        (start, end) = (int(start), int(end))
140 234954d1 Stavros Sachtouris
        self._value = '%s-%s' % (start, end)
141 234954d1 Stavros Sachtouris
142 201baa17 Stavros Sachtouris
143 234954d1 Stavros Sachtouris
# Command specs
144 234954d1 Stavros Sachtouris
145 e3d4d442 Stavros Sachtouris
146 5eae854d Stavros Sachtouris
class _pithos_init(_command_init):
147 befed235 Stavros Sachtouris
    """Initialize a pithos+ kamaki client"""
148 befed235 Stavros Sachtouris
149 ece4ae4b Stavros Sachtouris
    @staticmethod
150 ece4ae4b Stavros Sachtouris
    def _is_dir(remote_dict):
151 ece4ae4b Stavros Sachtouris
        return 'application/directory' == remote_dict.get(
152 8cec3671 Stavros Sachtouris
            'content_type', remote_dict.get('content-type', ''))
153 ece4ae4b Stavros Sachtouris
154 b4f69041 Stavros Sachtouris
    @DontRaiseKeyError
155 b4f69041 Stavros Sachtouris
    def _custom_container(self):
156 144b3551 Stavros Sachtouris
        return self.config.get_cloud(self.cloud, 'pithos_container')
157 b4f69041 Stavros Sachtouris
158 b4f69041 Stavros Sachtouris
    @DontRaiseKeyError
159 b4f69041 Stavros Sachtouris
    def _custom_uuid(self):
160 144b3551 Stavros Sachtouris
        return self.config.get_cloud(self.cloud, 'pithos_uuid')
161 b4f69041 Stavros Sachtouris
162 b4f69041 Stavros Sachtouris
    def _set_account(self):
163 b4f69041 Stavros Sachtouris
        self.account = self._custom_uuid()
164 b4f69041 Stavros Sachtouris
        if self.account:
165 b4f69041 Stavros Sachtouris
            return
166 b4f69041 Stavros Sachtouris
        if getattr(self, 'auth_base', False):
167 b4f69041 Stavros Sachtouris
            self.account = self.auth_base.user_term('id', self.token)
168 b4f69041 Stavros Sachtouris
        else:
169 b4f69041 Stavros Sachtouris
            astakos_url = self._custom_url('astakos')
170 b4f69041 Stavros Sachtouris
            astakos_token = self._custom_token('astakos') or self.token
171 b4f69041 Stavros Sachtouris
            if not astakos_url:
172 b4f69041 Stavros Sachtouris
                raise CLIBaseUrlError(service='astakos')
173 b4f69041 Stavros Sachtouris
            astakos = AstakosClient(astakos_url, astakos_token)
174 b4f69041 Stavros Sachtouris
            self.account = astakos.user_term('id')
175 b4f69041 Stavros Sachtouris
176 1395c40e Stavros Sachtouris
    @errors.generic.all
177 b4f69041 Stavros Sachtouris
    @addLogSettings
178 1395c40e Stavros Sachtouris
    def _run(self):
179 b4f69041 Stavros Sachtouris
        self.base_url = None
180 b4f69041 Stavros Sachtouris
        if getattr(self, 'cloud', None):
181 b4f69041 Stavros Sachtouris
            self.base_url = self._custom_url('pithos')
182 b4f69041 Stavros Sachtouris
        else:
183 b4f69041 Stavros Sachtouris
            self.cloud = 'default'
184 b4f69041 Stavros Sachtouris
        self.token = self._custom_token('pithos')
185 b4f69041 Stavros Sachtouris
        self.container = self._custom_container()
186 8cec3671 Stavros Sachtouris
187 8cec3671 Stavros Sachtouris
        if getattr(self, 'auth_base', False):
188 b4f69041 Stavros Sachtouris
            self.token = self.token or self.auth_base.token
189 b4f69041 Stavros Sachtouris
            if not self.base_url:
190 b4f69041 Stavros Sachtouris
                pithos_endpoints = self.auth_base.get_service_endpoints(
191 b4f69041 Stavros Sachtouris
                    self._custom_type('pithos') or 'object-store',
192 b4f69041 Stavros Sachtouris
                    self._custom_version('pithos') or '')
193 b4f69041 Stavros Sachtouris
                self.base_url = pithos_endpoints['publicURL']
194 b4f69041 Stavros Sachtouris
        elif not self.base_url:
195 8cec3671 Stavros Sachtouris
            raise CLIBaseUrlError(service='pithos')
196 8cec3671 Stavros Sachtouris
197 1f5debf7 Stavros Sachtouris
        self._set_account()
198 de73876b Stavros Sachtouris
        self.client = PithosClient(
199 de73876b Stavros Sachtouris
            base_url=self.base_url,
200 234954d1 Stavros Sachtouris
            token=self.token,
201 234954d1 Stavros Sachtouris
            account=self.account,
202 7493ccb6 Stavros Sachtouris
            container=self.container)
203 7493ccb6 Stavros Sachtouris
204 1395c40e Stavros Sachtouris
    def main(self):
205 1395c40e Stavros Sachtouris
        self._run()
206 1395c40e Stavros Sachtouris
207 234954d1 Stavros Sachtouris
208 3ae60112 Stavros Sachtouris
class _file_account_command(_pithos_init):
209 7493ccb6 Stavros Sachtouris
    """Base class for account level storage commands"""
210 7493ccb6 Stavros Sachtouris
211 b4f69041 Stavros Sachtouris
    def __init__(self, arguments={}, auth_base=None, cloud=None):
212 b4f69041 Stavros Sachtouris
        super(_file_account_command, self).__init__(
213 b4f69041 Stavros Sachtouris
            arguments, auth_base, cloud)
214 439826ec Stavros Sachtouris
        self['account'] = ValueArgument(
215 201baa17 Stavros Sachtouris
            'Set user account (not permanent)', ('-A', '--account'))
216 7493ccb6 Stavros Sachtouris
217 3ed6dbde Stavros Sachtouris
    def _run(self, custom_account=None):
218 3ae60112 Stavros Sachtouris
        super(_file_account_command, self)._run()
219 3ed6dbde Stavros Sachtouris
        if custom_account:
220 3ed6dbde Stavros Sachtouris
            self.client.account = custom_account
221 3ed6dbde Stavros Sachtouris
        elif self['account']:
222 47ae7577 Stavros Sachtouris
            self.client.account = self['account']
223 7493ccb6 Stavros Sachtouris
224 1395c40e Stavros Sachtouris
    @errors.generic.all
225 1395c40e Stavros Sachtouris
    def main(self):
226 1395c40e Stavros Sachtouris
        self._run()
227 1395c40e Stavros Sachtouris
228 234954d1 Stavros Sachtouris
229 3ae60112 Stavros Sachtouris
class _file_container_command(_file_account_command):
230 7493ccb6 Stavros Sachtouris
    """Base class for container level storage commands"""
231 7493ccb6 Stavros Sachtouris
232 47ae7577 Stavros Sachtouris
    container = None
233 47ae7577 Stavros Sachtouris
    path = None
234 2d7ce81e Stavros Sachtouris
235 b4f69041 Stavros Sachtouris
    def __init__(self, arguments={}, auth_base=None, cloud=None):
236 b4f69041 Stavros Sachtouris
        super(_file_container_command, self).__init__(
237 b4f69041 Stavros Sachtouris
            arguments, auth_base, cloud)
238 439826ec Stavros Sachtouris
        self['container'] = ValueArgument(
239 201baa17 Stavros Sachtouris
            'Set container to work with (temporary)', ('-C', '--container'))
240 7493ccb6 Stavros Sachtouris
241 de73876b Stavros Sachtouris
    def extract_container_and_path(
242 24ff0a35 Stavros Sachtouris
            self,
243 24ff0a35 Stavros Sachtouris
            container_with_path,
244 24ff0a35 Stavros Sachtouris
            path_is_optional=True):
245 1395c40e Stavros Sachtouris
        """Contains all heuristics for deciding what should be used as
246 1395c40e Stavros Sachtouris
        container or path. Options are:
247 1395c40e Stavros Sachtouris
        * user string of the form container:path
248 1395c40e Stavros Sachtouris
        * self.container, self.path variables set by super constructor, or
249 1395c40e Stavros Sachtouris
        explicitly by the caller application
250 1395c40e Stavros Sachtouris
        Error handling is explicit as these error cases happen only here
251 1395c40e Stavros Sachtouris
        """
252 83ba5545 Stavros Sachtouris
        try:
253 83ba5545 Stavros Sachtouris
            assert isinstance(container_with_path, str)
254 83ba5545 Stavros Sachtouris
        except AssertionError as err:
255 edab7ba7 Stavros Sachtouris
            if self['container'] and path_is_optional:
256 edab7ba7 Stavros Sachtouris
                self.container = self['container']
257 edab7ba7 Stavros Sachtouris
                self.client.container = self['container']
258 edab7ba7 Stavros Sachtouris
                return
259 83ba5545 Stavros Sachtouris
            raiseCLIError(err)
260 447c9568 Stavros Sachtouris
261 1395c40e Stavros Sachtouris
        user_cont, sep, userpath = container_with_path.partition(':')
262 447c9568 Stavros Sachtouris
263 447c9568 Stavros Sachtouris
        if sep:
264 1395c40e Stavros Sachtouris
            if not user_cont:
265 2005b18e Stavros Sachtouris
                raiseCLIError(CLISyntaxError(
266 2005b18e Stavros Sachtouris
                    'Container is missing\n',
267 1395c40e Stavros Sachtouris
                    details=errors.pithos.container_howto))
268 47ae7577 Stavros Sachtouris
            alt_cont = self['container']
269 1395c40e Stavros Sachtouris
            if alt_cont and user_cont != alt_cont:
270 c1558584 Stavros Sachtouris
                raiseCLIError(CLISyntaxError(
271 1395c40e Stavros Sachtouris
                    'Conflict: 2 containers (%s, %s)' % (user_cont, alt_cont),
272 1395c40e Stavros Sachtouris
                    details=errors.pithos.container_howto)
273 47ae7577 Stavros Sachtouris
                )
274 1395c40e Stavros Sachtouris
            self.container = user_cont
275 1395c40e Stavros Sachtouris
            if not userpath:
276 c1558584 Stavros Sachtouris
                raiseCLIError(CLISyntaxError(
277 1395c40e Stavros Sachtouris
                    'Path is missing for object in container %s' % user_cont,
278 1395c40e Stavros Sachtouris
                    details=errors.pithos.container_howto)
279 47ae7577 Stavros Sachtouris
                )
280 1395c40e Stavros Sachtouris
            self.path = userpath
281 447c9568 Stavros Sachtouris
        else:
282 47ae7577 Stavros Sachtouris
            alt_cont = self['container'] or self.client.container
283 447c9568 Stavros Sachtouris
            if alt_cont:
284 447c9568 Stavros Sachtouris
                self.container = alt_cont
285 1395c40e Stavros Sachtouris
                self.path = user_cont
286 447c9568 Stavros Sachtouris
            elif path_is_optional:
287 1395c40e Stavros Sachtouris
                self.container = user_cont
288 7493ccb6 Stavros Sachtouris
                self.path = None
289 7493ccb6 Stavros Sachtouris
            else:
290 1395c40e Stavros Sachtouris
                self.container = user_cont
291 447c9568 Stavros Sachtouris
                raiseCLIError(CLISyntaxError(
292 c1558584 Stavros Sachtouris
                    'Both container and path are required',
293 1395c40e Stavros Sachtouris
                    details=errors.pithos.container_howto)
294 47ae7577 Stavros Sachtouris
                )
295 7493ccb6 Stavros Sachtouris
296 1395c40e Stavros Sachtouris
    @errors.generic.all
297 1395c40e Stavros Sachtouris
    def _run(self, container_with_path=None, path_is_optional=True):
298 3ae60112 Stavros Sachtouris
        super(_file_container_command, self)._run()
299 edab7ba7 Stavros Sachtouris
        if self['container']:
300 edab7ba7 Stavros Sachtouris
            self.client.container = self['container']
301 edab7ba7 Stavros Sachtouris
            if container_with_path:
302 edab7ba7 Stavros Sachtouris
                self.path = container_with_path
303 edab7ba7 Stavros Sachtouris
            elif not path_is_optional:
304 edab7ba7 Stavros Sachtouris
                raise CLISyntaxError(
305 edab7ba7 Stavros Sachtouris
                    'Both container and path are required',
306 edab7ba7 Stavros Sachtouris
                    details=errors.pithos.container_howto)
307 edab7ba7 Stavros Sachtouris
        elif container_with_path:
308 47ae7577 Stavros Sachtouris
            self.extract_container_and_path(
309 47ae7577 Stavros Sachtouris
                container_with_path,
310 234954d1 Stavros Sachtouris
                path_is_optional)
311 7493ccb6 Stavros Sachtouris
            self.client.container = self.container
312 7493ccb6 Stavros Sachtouris
        self.container = self.client.container
313 7493ccb6 Stavros Sachtouris
314 1395c40e Stavros Sachtouris
    def main(self, container_with_path=None, path_is_optional=True):
315 1395c40e Stavros Sachtouris
        self._run(container_with_path, path_is_optional)
316 1395c40e Stavros Sachtouris
317 7493ccb6 Stavros Sachtouris
318 d486baec Stavros Sachtouris
@command(pithos_cmds)
319 545c6c29 Stavros Sachtouris
class file_list(_file_container_command, _optional_json):
320 7493ccb6 Stavros Sachtouris
    """List containers, object trees or objects in a directory
321 2703cceb Stavros Sachtouris
    Use with:
322 7ae842c2 Stavros Sachtouris
    1 no parameters : containers in current account
323 2703cceb Stavros Sachtouris
    2. one parameter (container) or --container : contents of container
324 2703cceb Stavros Sachtouris
    3. <container>:<prefix> or --container=<container> <prefix>: objects in
325 439826ec Stavros Sachtouris
    .   container starting with prefix
326 7493ccb6 Stavros Sachtouris
    """
327 7493ccb6 Stavros Sachtouris
328 47ae7577 Stavros Sachtouris
    arguments = dict(
329 f40f0cb7 Stavros Sachtouris
        detail=FlagArgument('detailed output', ('-l', '--list')),
330 f40f0cb7 Stavros Sachtouris
        limit=IntArgument('limit number of listed items', ('-n', '--number')),
331 f40f0cb7 Stavros Sachtouris
        marker=ValueArgument('output greater that marker', '--marker'),
332 f40f0cb7 Stavros Sachtouris
        prefix=ValueArgument('output starting with prefix', '--prefix'),
333 47ae7577 Stavros Sachtouris
        delimiter=ValueArgument('show output up to delimiter', '--delimiter'),
334 47ae7577 Stavros Sachtouris
        path=ValueArgument(
335 201baa17 Stavros Sachtouris
            'show output starting with prefix up to /', '--path'),
336 47ae7577 Stavros Sachtouris
        meta=ValueArgument(
337 201baa17 Stavros Sachtouris
            'show output with specified meta keys', '--meta',
338 47ae7577 Stavros Sachtouris
            default=[]),
339 47ae7577 Stavros Sachtouris
        if_modified_since=ValueArgument(
340 201baa17 Stavros Sachtouris
            'show output modified since then', '--if-modified-since'),
341 47ae7577 Stavros Sachtouris
        if_unmodified_since=ValueArgument(
342 201baa17 Stavros Sachtouris
            'show output not modified since then', '--if-unmodified-since'),
343 47ae7577 Stavros Sachtouris
        until=DateArgument('show metadata until then', '--until'),
344 47ae7577 Stavros Sachtouris
        format=ValueArgument(
345 201baa17 Stavros Sachtouris
            'format to parse until data (default: d/m/Y H:M:S )', '--format'),
346 47ae7577 Stavros Sachtouris
        shared=FlagArgument('show only shared', '--shared'),
347 439826ec Stavros Sachtouris
        more=FlagArgument(
348 439826ec Stavros Sachtouris
            'output results in pages (-n to set items per page, default 10)',
349 eb18b8a7 Stavros Sachtouris
            '--more'),
350 eb18b8a7 Stavros Sachtouris
        exact_match=FlagArgument(
351 eb18b8a7 Stavros Sachtouris
            'Show only objects that match exactly with path',
352 ed9af02c Stavros Sachtouris
            '--exact-match'),
353 545c6c29 Stavros Sachtouris
        enum=FlagArgument('Enumerate results', '--enumerate')
354 47ae7577 Stavros Sachtouris
    )
355 c41a86b2 Stavros Sachtouris
356 7493ccb6 Stavros Sachtouris
    def print_objects(self, object_list):
357 0399ac7e Stavros Sachtouris
        if self['json_output']:
358 0399ac7e Stavros Sachtouris
            print_json(object_list)
359 0399ac7e Stavros Sachtouris
            return
360 439826ec Stavros Sachtouris
        limit = int(self['limit']) if self['limit'] > 0 else len(object_list)
361 234954d1 Stavros Sachtouris
        for index, obj in enumerate(object_list):
362 24ff0a35 Stavros Sachtouris
            if self['exact_match'] and self.path and not (
363 2005b18e Stavros Sachtouris
                    obj['name'] == self.path or 'content_type' in obj):
364 2005b18e Stavros Sachtouris
                continue
365 7493ccb6 Stavros Sachtouris
            pretty_obj = obj.copy()
366 7493ccb6 Stavros Sachtouris
            index += 1
367 234954d1 Stavros Sachtouris
            empty_space = ' ' * (len(str(len(object_list))) - len(str(index)))
368 7493ccb6 Stavros Sachtouris
            if obj['content_type'] == 'application/directory':
369 7493ccb6 Stavros Sachtouris
                isDir = True
370 7493ccb6 Stavros Sachtouris
                size = 'D'
371 7493ccb6 Stavros Sachtouris
            else:
372 7493ccb6 Stavros Sachtouris
                isDir = False
373 7493ccb6 Stavros Sachtouris
                size = format_size(obj['bytes'])
374 234954d1 Stavros Sachtouris
                pretty_obj['bytes'] = '%s (%s)' % (obj['bytes'], size)
375 7493ccb6 Stavros Sachtouris
            oname = bold(obj['name'])
376 ed9af02c Stavros Sachtouris
            prfx = ('%s%s. ' % (empty_space, index)) if self['enum'] else ''
377 47ae7577 Stavros Sachtouris
            if self['detail']:
378 ed9af02c Stavros Sachtouris
                print('%s%s' % (prfx, oname))
379 7493ccb6 Stavros Sachtouris
                print_dict(pretty_keys(pretty_obj), exclude=('name'))
380 7493ccb6 Stavros Sachtouris
                print
381 7493ccb6 Stavros Sachtouris
            else:
382 ed9af02c Stavros Sachtouris
                oname = '%s%9s %s' % (prfx, size, oname)
383 7493ccb6 Stavros Sachtouris
                oname += '/' if isDir else ''
384 7493ccb6 Stavros Sachtouris
                print(oname)
385 439826ec Stavros Sachtouris
            if self['more']:
386 439826ec Stavros Sachtouris
                page_hold(index, limit, len(object_list))
387 7493ccb6 Stavros Sachtouris
388 7493ccb6 Stavros Sachtouris
    def print_containers(self, container_list):
389 0399ac7e Stavros Sachtouris
        if self['json_output']:
390 0399ac7e Stavros Sachtouris
            print_json(container_list)
391 0399ac7e Stavros Sachtouris
            return
392 439826ec Stavros Sachtouris
        limit = int(self['limit']) if self['limit'] > 0\
393 439826ec Stavros Sachtouris
            else len(container_list)
394 234954d1 Stavros Sachtouris
        for index, container in enumerate(container_list):
395 234954d1 Stavros Sachtouris
            if 'bytes' in container:
396 234954d1 Stavros Sachtouris
                size = format_size(container['bytes'])
397 ed9af02c Stavros Sachtouris
            prfx = ('%s. ' % (index + 1)) if self['enum'] else ''
398 ed9af02c Stavros Sachtouris
            cname = '%s%s' % (prfx, bold(container['name']))
399 47ae7577 Stavros Sachtouris
            if self['detail']:
400 7493ccb6 Stavros Sachtouris
                print(cname)
401 7493ccb6 Stavros Sachtouris
                pretty_c = container.copy()
402 234954d1 Stavros Sachtouris
                if 'bytes' in container:
403 234954d1 Stavros Sachtouris
                    pretty_c['bytes'] = '%s (%s)' % (container['bytes'], size)
404 7493ccb6 Stavros Sachtouris
                print_dict(pretty_keys(pretty_c), exclude=('name'))
405 7493ccb6 Stavros Sachtouris
                print
406 7493ccb6 Stavros Sachtouris
            else:
407 234954d1 Stavros Sachtouris
                if 'count' in container and 'bytes' in container:
408 de73876b Stavros Sachtouris
                    print('%s (%s, %s objects)' % (
409 de73876b Stavros Sachtouris
                        cname,
410 de73876b Stavros Sachtouris
                        size,
411 de73876b Stavros Sachtouris
                        container['count']))
412 7493ccb6 Stavros Sachtouris
                else:
413 7493ccb6 Stavros Sachtouris
                    print(cname)
414 439826ec Stavros Sachtouris
            if self['more']:
415 439826ec Stavros Sachtouris
                page_hold(index + 1, limit, len(container_list))
416 7493ccb6 Stavros Sachtouris
417 1395c40e Stavros Sachtouris
    @errors.generic.all
418 1395c40e Stavros Sachtouris
    @errors.pithos.connection
419 1395c40e Stavros Sachtouris
    @errors.pithos.object_path
420 1395c40e Stavros Sachtouris
    @errors.pithos.container
421 1395c40e Stavros Sachtouris
    def _run(self):
422 1395c40e Stavros Sachtouris
        if self.container is None:
423 1395c40e Stavros Sachtouris
            r = self.client.account_get(
424 1395c40e Stavros Sachtouris
                limit=False if self['more'] else self['limit'],
425 1395c40e Stavros Sachtouris
                marker=self['marker'],
426 1395c40e Stavros Sachtouris
                if_modified_since=self['if_modified_since'],
427 1395c40e Stavros Sachtouris
                if_unmodified_since=self['if_unmodified_since'],
428 1395c40e Stavros Sachtouris
                until=self['until'],
429 1395c40e Stavros Sachtouris
                show_only_shared=self['shared'])
430 545c6c29 Stavros Sachtouris
            self._print(r.json, self.print_containers)
431 1395c40e Stavros Sachtouris
        else:
432 52edad0a Stavros Sachtouris
            prefix = self.path or self['prefix']
433 1395c40e Stavros Sachtouris
            r = self.client.container_get(
434 1395c40e Stavros Sachtouris
                limit=False if self['more'] else self['limit'],
435 1395c40e Stavros Sachtouris
                marker=self['marker'],
436 1395c40e Stavros Sachtouris
                prefix=prefix,
437 1395c40e Stavros Sachtouris
                delimiter=self['delimiter'],
438 1395c40e Stavros Sachtouris
                path=self['path'],
439 1395c40e Stavros Sachtouris
                if_modified_since=self['if_modified_since'],
440 1395c40e Stavros Sachtouris
                if_unmodified_since=self['if_unmodified_since'],
441 1395c40e Stavros Sachtouris
                until=self['until'],
442 1395c40e Stavros Sachtouris
                meta=self['meta'],
443 1395c40e Stavros Sachtouris
                show_only_shared=self['shared'])
444 545c6c29 Stavros Sachtouris
            self._print(r.json, self.print_objects)
445 1395c40e Stavros Sachtouris
446 7493ccb6 Stavros Sachtouris
    def main(self, container____path__=None):
447 1395c40e Stavros Sachtouris
        super(self.__class__, self)._run(container____path__)
448 1395c40e Stavros Sachtouris
        self._run()
449 7493ccb6 Stavros Sachtouris
450 234954d1 Stavros Sachtouris
451 d486baec Stavros Sachtouris
@command(pithos_cmds)
452 915b99b5 Stavros Sachtouris
class file_mkdir(_file_container_command, _optional_output_cmd):
453 f17d6cb5 Stavros Sachtouris
    """Create a directory
454 f17d6cb5 Stavros Sachtouris
    Kamaki hanldes directories the same way as OOS Storage and Pithos+:
455 f17d6cb5 Stavros Sachtouris
    A directory  is   an  object  with  type  "application/directory"
456 f17d6cb5 Stavros Sachtouris
    An object with path  dir/name can exist even if  dir does not exist
457 f17d6cb5 Stavros Sachtouris
    or even if dir  is  a non  directory  object.  Users can modify dir '
458 f17d6cb5 Stavros Sachtouris
    without affecting the dir/name object in any way.
459 f17d6cb5 Stavros Sachtouris
    """
460 7493ccb6 Stavros Sachtouris
461 1395c40e Stavros Sachtouris
    @errors.generic.all
462 1395c40e Stavros Sachtouris
    @errors.pithos.connection
463 1395c40e Stavros Sachtouris
    @errors.pithos.container
464 1395c40e Stavros Sachtouris
    def _run(self):
465 915b99b5 Stavros Sachtouris
        self._optional_output(self.client.create_directory(self.path))
466 1395c40e Stavros Sachtouris
467 7493ccb6 Stavros Sachtouris
    def main(self, container___directory):
468 1395c40e Stavros Sachtouris
        super(self.__class__, self)._run(
469 1395c40e Stavros Sachtouris
            container___directory,
470 1395c40e Stavros Sachtouris
            path_is_optional=False)
471 1395c40e Stavros Sachtouris
        self._run()
472 7493ccb6 Stavros Sachtouris
473 234954d1 Stavros Sachtouris
474 d486baec Stavros Sachtouris
@command(pithos_cmds)
475 915b99b5 Stavros Sachtouris
class file_touch(_file_container_command, _optional_output_cmd):
476 1e29b9f6 Stavros Sachtouris
    """Create an empty object (file)
477 1e29b9f6 Stavros Sachtouris
    If object exists, this command will reset it to 0 length
478 1e29b9f6 Stavros Sachtouris
    """
479 1e29b9f6 Stavros Sachtouris
480 1e29b9f6 Stavros Sachtouris
    arguments = dict(
481 1e29b9f6 Stavros Sachtouris
        content_type=ValueArgument(
482 1e29b9f6 Stavros Sachtouris
            'Set content type (default: application/octet-stream)',
483 1e29b9f6 Stavros Sachtouris
            '--content-type',
484 915b99b5 Stavros Sachtouris
            default='application/octet-stream')
485 1e29b9f6 Stavros Sachtouris
    )
486 1e29b9f6 Stavros Sachtouris
487 1395c40e Stavros Sachtouris
    @errors.generic.all
488 1395c40e Stavros Sachtouris
    @errors.pithos.connection
489 1395c40e Stavros Sachtouris
    @errors.pithos.container
490 1395c40e Stavros Sachtouris
    def _run(self):
491 915b99b5 Stavros Sachtouris
        self._optional_output(
492 915b99b5 Stavros Sachtouris
            self.client.create_object(self.path, self['content_type']))
493 1395c40e Stavros Sachtouris
494 1e29b9f6 Stavros Sachtouris
    def main(self, container___path):
495 3ae60112 Stavros Sachtouris
        super(file_touch, self)._run(
496 52edad0a Stavros Sachtouris
            container___path,
497 52edad0a Stavros Sachtouris
            path_is_optional=False)
498 1395c40e Stavros Sachtouris
        self._run()
499 1e29b9f6 Stavros Sachtouris
500 1e29b9f6 Stavros Sachtouris
501 1e29b9f6 Stavros Sachtouris
@command(pithos_cmds)
502 915b99b5 Stavros Sachtouris
class file_create(_file_container_command, _optional_output_cmd):
503 1e29b9f6 Stavros Sachtouris
    """Create a container"""
504 7493ccb6 Stavros Sachtouris
505 2fe2672e Stavros Sachtouris
    arguments = dict(
506 2fe2672e Stavros Sachtouris
        versioning=ValueArgument(
507 201baa17 Stavros Sachtouris
            'set container versioning (auto/none)', '--versioning'),
508 3ed6dbde Stavros Sachtouris
        limit=IntArgument('set default container limit', '--limit'),
509 2fe2672e Stavros Sachtouris
        meta=KeyValueArgument(
510 201baa17 Stavros Sachtouris
            'set container metadata (can be repeated)', '--meta')
511 2fe2672e Stavros Sachtouris
    )
512 7493ccb6 Stavros Sachtouris
513 1395c40e Stavros Sachtouris
    @errors.generic.all
514 1395c40e Stavros Sachtouris
    @errors.pithos.connection
515 1395c40e Stavros Sachtouris
    @errors.pithos.container
516 94bedc5b Stavros Sachtouris
    def _run(self, container):
517 915b99b5 Stavros Sachtouris
        self._optional_output(self.client.create_container(
518 915b99b5 Stavros Sachtouris
            container=container,
519 915b99b5 Stavros Sachtouris
            sizelimit=self['limit'],
520 915b99b5 Stavros Sachtouris
            versioning=self['versioning'],
521 915b99b5 Stavros Sachtouris
            metadata=self['meta']))
522 1395c40e Stavros Sachtouris
523 edab7ba7 Stavros Sachtouris
    def main(self, container=None):
524 1395c40e Stavros Sachtouris
        super(self.__class__, self)._run(container)
525 edab7ba7 Stavros Sachtouris
        if container and self.container != container:
526 1395c40e Stavros Sachtouris
            raiseCLIError('Invalid container name %s' % container, details=[
527 edab7ba7 Stavros Sachtouris
                'Did you mean "%s" ?' % self.container,
528 52edad0a Stavros Sachtouris
                'Use --container for names containing :'])
529 94bedc5b Stavros Sachtouris
        self._run(container)
530 7493ccb6 Stavros Sachtouris
531 234954d1 Stavros Sachtouris
532 3ae60112 Stavros Sachtouris
class _source_destination_command(_file_container_command):
533 2fe2672e Stavros Sachtouris
534 2fe2672e Stavros Sachtouris
    arguments = dict(
535 f17d6cb5 Stavros Sachtouris
        destination_account=ValueArgument('', ('-a', '--dst-account')),
536 f40f0cb7 Stavros Sachtouris
        recursive=FlagArgument('', ('-R', '--recursive')),
537 761e0cbf Stavros Sachtouris
        prefix=FlagArgument('', '--with-prefix', default=''),
538 761e0cbf Stavros Sachtouris
        suffix=ValueArgument('', '--with-suffix', default=''),
539 761e0cbf Stavros Sachtouris
        add_prefix=ValueArgument('', '--add-prefix', default=''),
540 761e0cbf Stavros Sachtouris
        add_suffix=ValueArgument('', '--add-suffix', default=''),
541 761e0cbf Stavros Sachtouris
        prefix_replace=ValueArgument('', '--prefix-to-replace', default=''),
542 1d3f006b Stavros Sachtouris
        suffix_replace=ValueArgument('', '--suffix-to-replace', default=''),
543 2fe2672e Stavros Sachtouris
    )
544 7493ccb6 Stavros Sachtouris
545 b4f69041 Stavros Sachtouris
    def __init__(self, arguments={}, auth_base=None, cloud=None):
546 761e0cbf Stavros Sachtouris
        self.arguments.update(arguments)
547 f724cd35 Stavros Sachtouris
        super(_source_destination_command, self).__init__(
548 b4f69041 Stavros Sachtouris
            self.arguments, auth_base, cloud)
549 761e0cbf Stavros Sachtouris
550 300da0fb Stavros Sachtouris
    def _run(self, source_container___path, path_is_optional=False):
551 300da0fb Stavros Sachtouris
        super(_source_destination_command, self)._run(
552 300da0fb Stavros Sachtouris
            source_container___path,
553 300da0fb Stavros Sachtouris
            path_is_optional)
554 300da0fb Stavros Sachtouris
        self.dst_client = PithosClient(
555 300da0fb Stavros Sachtouris
            base_url=self.client.base_url,
556 300da0fb Stavros Sachtouris
            token=self.client.token,
557 300da0fb Stavros Sachtouris
            account=self['destination_account'] or self.client.account)
558 300da0fb Stavros Sachtouris
559 300da0fb Stavros Sachtouris
    @errors.generic.all
560 300da0fb Stavros Sachtouris
    @errors.pithos.account
561 300da0fb Stavros Sachtouris
    def _dest_container_path(self, dest_container_path):
562 300da0fb Stavros Sachtouris
        if self['destination_container']:
563 300da0fb Stavros Sachtouris
            self.dst_client.container = self['destination_container']
564 300da0fb Stavros Sachtouris
            return (self['destination_container'], dest_container_path)
565 300da0fb Stavros Sachtouris
        if dest_container_path:
566 300da0fb Stavros Sachtouris
            dst = dest_container_path.split(':')
567 300da0fb Stavros Sachtouris
            if len(dst) > 1:
568 300da0fb Stavros Sachtouris
                try:
569 300da0fb Stavros Sachtouris
                    self.dst_client.container = dst[0]
570 300da0fb Stavros Sachtouris
                    self.dst_client.get_container_info(dst[0])
571 300da0fb Stavros Sachtouris
                except ClientError as err:
572 300da0fb Stavros Sachtouris
                    if err.status in (404, 204):
573 300da0fb Stavros Sachtouris
                        raiseCLIError(
574 300da0fb Stavros Sachtouris
                            'Destination container %s not found' % dst[0])
575 300da0fb Stavros Sachtouris
                    raise
576 300da0fb Stavros Sachtouris
                else:
577 300da0fb Stavros Sachtouris
                    self.dst_client.container = dst[0]
578 300da0fb Stavros Sachtouris
                return (dst[0], dst[1])
579 300da0fb Stavros Sachtouris
            return(None, dst[0])
580 300da0fb Stavros Sachtouris
        raiseCLIError('No destination container:path provided')
581 300da0fb Stavros Sachtouris
582 ece4ae4b Stavros Sachtouris
    def _get_all(self, prefix):
583 ece4ae4b Stavros Sachtouris
        return self.client.container_get(prefix=prefix).json
584 ece4ae4b Stavros Sachtouris
585 1d3f006b Stavros Sachtouris
    def _get_src_objects(self, src_path, source_version=None):
586 6736f171 Stavros Sachtouris
        """Get a list of the source objects to be called
587 6736f171 Stavros Sachtouris

588 6736f171 Stavros Sachtouris
        :param src_path: (str) source path
589 6736f171 Stavros Sachtouris

590 6736f171 Stavros Sachtouris
        :returns: (method, params) a method that returns a list when called
591 6736f171 Stavros Sachtouris
        or (object) if it is a single object
592 6736f171 Stavros Sachtouris
        """
593 ece4ae4b Stavros Sachtouris
        if src_path and src_path[-1] == '/':
594 ece4ae4b Stavros Sachtouris
            src_path = src_path[:-1]
595 ece4ae4b Stavros Sachtouris
596 6736f171 Stavros Sachtouris
        if self['prefix']:
597 6736f171 Stavros Sachtouris
            return (self._get_all, dict(prefix=src_path))
598 ece4ae4b Stavros Sachtouris
        try:
599 1d3f006b Stavros Sachtouris
            srcobj = self.client.get_object_info(
600 1d3f006b Stavros Sachtouris
                src_path, version=source_version)
601 ece4ae4b Stavros Sachtouris
        except ClientError as srcerr:
602 6736f171 Stavros Sachtouris
            if srcerr.status == 404:
603 6736f171 Stavros Sachtouris
                raiseCLIError(
604 300da0fb Stavros Sachtouris
                    'Source object %s not in source container %s' % (
605 201baa17 Stavros Sachtouris
                        src_path, self.client.container),
606 6736f171 Stavros Sachtouris
                    details=['Hint: --with-prefix to match multiple objects'])
607 6736f171 Stavros Sachtouris
            elif srcerr.status not in (204,):
608 ece4ae4b Stavros Sachtouris
                raise
609 6736f171 Stavros Sachtouris
            return (self.client.list_objects, {})
610 300da0fb Stavros Sachtouris
611 6736f171 Stavros Sachtouris
        if self._is_dir(srcobj):
612 6736f171 Stavros Sachtouris
            if not self['recursive']:
613 6736f171 Stavros Sachtouris
                raiseCLIError(
614 300da0fb Stavros Sachtouris
                    'Object %s of cont. %s is a dir' % (
615 201baa17 Stavros Sachtouris
                        src_path, self.client.container),
616 6736f171 Stavros Sachtouris
                    details=['Use --recursive to access directories'])
617 6736f171 Stavros Sachtouris
            return (self._get_all, dict(prefix=src_path))
618 6736f171 Stavros Sachtouris
        srcobj['name'] = src_path
619 6736f171 Stavros Sachtouris
        return srcobj
620 6736f171 Stavros Sachtouris
621 1d3f006b Stavros Sachtouris
    def src_dst_pairs(self, dst_path, source_version=None):
622 1d3f006b Stavros Sachtouris
        src_iter = self._get_src_objects(self.path, source_version)
623 6736f171 Stavros Sachtouris
        src_N = isinstance(src_iter, tuple)
624 6736f171 Stavros Sachtouris
        add_prefix = self['add_prefix'].strip('/')
625 6736f171 Stavros Sachtouris
626 6736f171 Stavros Sachtouris
        if dst_path and dst_path.endswith('/'):
627 ece4ae4b Stavros Sachtouris
            dst_path = dst_path[:-1]
628 ece4ae4b Stavros Sachtouris
629 6736f171 Stavros Sachtouris
        try:
630 300da0fb Stavros Sachtouris
            dstobj = self.dst_client.get_object_info(dst_path)
631 6736f171 Stavros Sachtouris
        except ClientError as trgerr:
632 6736f171 Stavros Sachtouris
            if trgerr.status in (404,):
633 6736f171 Stavros Sachtouris
                if src_N:
634 6736f171 Stavros Sachtouris
                    raiseCLIError(
635 6736f171 Stavros Sachtouris
                        'Cannot merge multiple paths to path %s' % dst_path,
636 6736f171 Stavros Sachtouris
                        details=[
637 6736f171 Stavros Sachtouris
                            'Try to use / or a directory as destination',
638 3ae60112 Stavros Sachtouris
                            'or create the destination dir (/file mkdir)',
639 6736f171 Stavros Sachtouris
                            'or use a single object as source'])
640 6736f171 Stavros Sachtouris
            elif trgerr.status not in (204,):
641 6736f171 Stavros Sachtouris
                raise
642 6736f171 Stavros Sachtouris
        else:
643 6736f171 Stavros Sachtouris
            if self._is_dir(dstobj):
644 6736f171 Stavros Sachtouris
                add_prefix = '%s/%s' % (dst_path.strip('/'), add_prefix)
645 6736f171 Stavros Sachtouris
            elif src_N:
646 6736f171 Stavros Sachtouris
                raiseCLIError(
647 6736f171 Stavros Sachtouris
                    'Cannot merge multiple paths to path' % dst_path,
648 6736f171 Stavros Sachtouris
                    details=[
649 6736f171 Stavros Sachtouris
                        'Try to use / or a directory as destination',
650 3ae60112 Stavros Sachtouris
                        'or create the destination dir (/file mkdir)',
651 6736f171 Stavros Sachtouris
                        'or use a single object as source'])
652 6736f171 Stavros Sachtouris
653 6736f171 Stavros Sachtouris
        if src_N:
654 6736f171 Stavros Sachtouris
            (method, kwargs) = src_iter
655 6736f171 Stavros Sachtouris
            for obj in method(**kwargs):
656 6736f171 Stavros Sachtouris
                name = obj['name']
657 6736f171 Stavros Sachtouris
                if name.endswith(self['suffix']):
658 6736f171 Stavros Sachtouris
                    yield (name, self._get_new_object(name, add_prefix))
659 6736f171 Stavros Sachtouris
        elif src_iter['name'].endswith(self['suffix']):
660 6736f171 Stavros Sachtouris
            name = src_iter['name']
661 6736f171 Stavros Sachtouris
            yield (name, self._get_new_object(dst_path or name, add_prefix))
662 6736f171 Stavros Sachtouris
        else:
663 6736f171 Stavros Sachtouris
            raiseCLIError('Source path %s conflicts with suffix %s' % (
664 201baa17 Stavros Sachtouris
                src_iter['name'], self['suffix']))
665 ece4ae4b Stavros Sachtouris
666 6736f171 Stavros Sachtouris
    def _get_new_object(self, obj, add_prefix):
667 6736f171 Stavros Sachtouris
        if self['prefix_replace'] and obj.startswith(self['prefix_replace']):
668 6736f171 Stavros Sachtouris
            obj = obj[len(self['prefix_replace']):]
669 6736f171 Stavros Sachtouris
        if self['suffix_replace'] and obj.endswith(self['suffix_replace']):
670 6736f171 Stavros Sachtouris
            obj = obj[:-len(self['suffix_replace'])]
671 6736f171 Stavros Sachtouris
        return add_prefix + obj + self['add_suffix']
672 efdee310 Stavros Sachtouris
673 761e0cbf Stavros Sachtouris
674 761e0cbf Stavros Sachtouris
@command(pithos_cmds)
675 915b99b5 Stavros Sachtouris
class file_copy(_source_destination_command, _optional_output_cmd):
676 761e0cbf Stavros Sachtouris
    """Copy objects from container to (another) container
677 761e0cbf Stavros Sachtouris
    Semantics:
678 761e0cbf Stavros Sachtouris
    copy cont:path dir
679 761e0cbf Stavros Sachtouris
    .   transfer path as dir/path
680 761e0cbf Stavros Sachtouris
    copy cont:path cont2:
681 761e0cbf Stavros Sachtouris
    .   trasnfer all <obj> prefixed with path to container cont2
682 761e0cbf Stavros Sachtouris
    copy cont:path [cont2:]path2
683 761e0cbf Stavros Sachtouris
    .   transfer path to path2
684 761e0cbf Stavros Sachtouris
    Use options:
685 761e0cbf Stavros Sachtouris
    1. <container1>:<path1> [container2:]<path2> : if container2 is not given,
686 761e0cbf Stavros Sachtouris
    destination is container1:path2
687 761e0cbf Stavros Sachtouris
    2. <container>:<path1> <path2> : make a copy in the same container
688 761e0cbf Stavros Sachtouris
    3. Can use --container= instead of <container1>
689 761e0cbf Stavros Sachtouris
    """
690 761e0cbf Stavros Sachtouris
691 761e0cbf Stavros Sachtouris
    arguments = dict(
692 300da0fb Stavros Sachtouris
        destination_account=ValueArgument(
693 201baa17 Stavros Sachtouris
            'Account to copy to', ('-a', '--dst-account')),
694 761e0cbf Stavros Sachtouris
        destination_container=ValueArgument(
695 761e0cbf Stavros Sachtouris
            'use it if destination container name contains a : character',
696 f40f0cb7 Stavros Sachtouris
            ('-D', '--dst-container')),
697 761e0cbf Stavros Sachtouris
        public=ValueArgument('make object publicly accessible', '--public'),
698 761e0cbf Stavros Sachtouris
        content_type=ValueArgument(
699 201baa17 Stavros Sachtouris
            'change object\'s content type', '--content-type'),
700 761e0cbf Stavros Sachtouris
        recursive=FlagArgument(
701 201baa17 Stavros Sachtouris
            'copy directory and contents', ('-R', '--recursive')),
702 761e0cbf Stavros Sachtouris
        prefix=FlagArgument(
703 761e0cbf Stavros Sachtouris
            'Match objects prefixed with src path (feels like src_path*)',
704 761e0cbf Stavros Sachtouris
            '--with-prefix',
705 761e0cbf Stavros Sachtouris
            default=''),
706 761e0cbf Stavros Sachtouris
        suffix=ValueArgument(
707 201baa17 Stavros Sachtouris
            'Suffix of source objects (feels like *suffix)', '--with-suffix',
708 761e0cbf Stavros Sachtouris
            default=''),
709 761e0cbf Stavros Sachtouris
        add_prefix=ValueArgument('Prefix targets', '--add-prefix', default=''),
710 761e0cbf Stavros Sachtouris
        add_suffix=ValueArgument('Suffix targets', '--add-suffix', default=''),
711 761e0cbf Stavros Sachtouris
        prefix_replace=ValueArgument(
712 761e0cbf Stavros Sachtouris
            'Prefix of src to replace with dst path + add_prefix, if matched',
713 761e0cbf Stavros Sachtouris
            '--prefix-to-replace',
714 761e0cbf Stavros Sachtouris
            default=''),
715 761e0cbf Stavros Sachtouris
        suffix_replace=ValueArgument(
716 761e0cbf Stavros Sachtouris
            'Suffix of src to replace with add_suffix, if matched',
717 761e0cbf Stavros Sachtouris
            '--suffix-to-replace',
718 1d3f006b Stavros Sachtouris
            default=''),
719 1d3f006b Stavros Sachtouris
        source_version=ValueArgument(
720 201baa17 Stavros Sachtouris
            'copy specific version', ('-S', '--source-version'))
721 761e0cbf Stavros Sachtouris
    )
722 761e0cbf Stavros Sachtouris
723 1395c40e Stavros Sachtouris
    @errors.generic.all
724 1395c40e Stavros Sachtouris
    @errors.pithos.connection
725 1395c40e Stavros Sachtouris
    @errors.pithos.container
726 300da0fb Stavros Sachtouris
    @errors.pithos.account
727 300da0fb Stavros Sachtouris
    def _run(self, dst_path):
728 1395c40e Stavros Sachtouris
        no_source_object = True
729 300da0fb Stavros Sachtouris
        src_account = self.client.account if (
730 300da0fb Stavros Sachtouris
            self['destination_account']) else None
731 1d3f006b Stavros Sachtouris
        for src_obj, dst_obj in self.src_dst_pairs(
732 1d3f006b Stavros Sachtouris
                dst_path, self['source_version']):
733 1395c40e Stavros Sachtouris
            no_source_object = False
734 55c75058 Stavros Sachtouris
            r = self.dst_client.copy_object(
735 300da0fb Stavros Sachtouris
                src_container=self.client.container,
736 3a066af4 Stavros Sachtouris
                src_object=src_obj,
737 300da0fb Stavros Sachtouris
                dst_container=self.dst_client.container,
738 300da0fb Stavros Sachtouris
                dst_object=dst_obj,
739 300da0fb Stavros Sachtouris
                source_account=src_account,
740 1395c40e Stavros Sachtouris
                source_version=self['source_version'],
741 1395c40e Stavros Sachtouris
                public=self['public'],
742 1395c40e Stavros Sachtouris
                content_type=self['content_type'])
743 1395c40e Stavros Sachtouris
        if no_source_object:
744 1395c40e Stavros Sachtouris
            raiseCLIError('No object %s in container %s' % (
745 201baa17 Stavros Sachtouris
                self.path, self.container))
746 915b99b5 Stavros Sachtouris
        self._optional_output(r)
747 1395c40e Stavros Sachtouris
748 de73876b Stavros Sachtouris
    def main(
749 e2d7b883 Stavros Sachtouris
            self, source_container___path,
750 24ff0a35 Stavros Sachtouris
            destination_container___path=None):
751 3ae60112 Stavros Sachtouris
        super(file_copy, self)._run(
752 1395c40e Stavros Sachtouris
            source_container___path,
753 1395c40e Stavros Sachtouris
            path_is_optional=False)
754 1395c40e Stavros Sachtouris
        (dst_cont, dst_path) = self._dest_container_path(
755 0e4ee6d1 Stavros Sachtouris
            destination_container___path)
756 300da0fb Stavros Sachtouris
        self.dst_client.container = dst_cont or self.container
757 300da0fb Stavros Sachtouris
        self._run(dst_path=dst_path or '')
758 7493ccb6 Stavros Sachtouris
759 234954d1 Stavros Sachtouris
760 d486baec Stavros Sachtouris
@command(pithos_cmds)
761 915b99b5 Stavros Sachtouris
class file_move(_source_destination_command, _optional_output_cmd):
762 761e0cbf Stavros Sachtouris
    """Move/rename objects from container to (another) container
763 8249ee0f Stavros Sachtouris
    Semantics:
764 761e0cbf Stavros Sachtouris
    move cont:path dir
765 761e0cbf Stavros Sachtouris
    .   rename path as dir/path
766 8249ee0f Stavros Sachtouris
    move cont:path cont2:
767 761e0cbf Stavros Sachtouris
    .   trasnfer all <obj> prefixed with path to container cont2
768 761e0cbf Stavros Sachtouris
    move cont:path [cont2:]path2
769 761e0cbf Stavros Sachtouris
    .   transfer path to path2
770 2fe2672e Stavros Sachtouris
    Use options:
771 761e0cbf Stavros Sachtouris
    1. <container1>:<path1> [container2:]<path2> : if container2 is not given,
772 795bf206 Stavros Sachtouris
    destination is container1:path2
773 761e0cbf Stavros Sachtouris
    2. <container>:<path1> <path2> : move in the same container
774 8249ee0f Stavros Sachtouris
    3. Can use --container= instead of <container1>
775 2fe2672e Stavros Sachtouris
    """
776 2fe2672e Stavros Sachtouris
777 2fe2672e Stavros Sachtouris
    arguments = dict(
778 300da0fb Stavros Sachtouris
        destination_account=ValueArgument(
779 201baa17 Stavros Sachtouris
            'Account to move to', ('-a', '--dst-account')),
780 0e4ee6d1 Stavros Sachtouris
        destination_container=ValueArgument(
781 0e4ee6d1 Stavros Sachtouris
            'use it if destination container name contains a : character',
782 f40f0cb7 Stavros Sachtouris
            ('-D', '--dst-container')),
783 761e0cbf Stavros Sachtouris
        public=ValueArgument('make object publicly accessible', '--public'),
784 761e0cbf Stavros Sachtouris
        content_type=ValueArgument(
785 201baa17 Stavros Sachtouris
            'change object\'s content type', '--content-type'),
786 761e0cbf Stavros Sachtouris
        recursive=FlagArgument(
787 201baa17 Stavros Sachtouris
            'copy directory and contents', ('-R', '--recursive')),
788 761e0cbf Stavros Sachtouris
        prefix=FlagArgument(
789 761e0cbf Stavros Sachtouris
            'Match objects prefixed with src path (feels like src_path*)',
790 761e0cbf Stavros Sachtouris
            '--with-prefix',
791 761e0cbf Stavros Sachtouris
            default=''),
792 761e0cbf Stavros Sachtouris
        suffix=ValueArgument(
793 201baa17 Stavros Sachtouris
            'Suffix of source objects (feels like *suffix)', '--with-suffix',
794 761e0cbf Stavros Sachtouris
            default=''),
795 761e0cbf Stavros Sachtouris
        add_prefix=ValueArgument('Prefix targets', '--add-prefix', default=''),
796 761e0cbf Stavros Sachtouris
        add_suffix=ValueArgument('Suffix targets', '--add-suffix', default=''),
797 761e0cbf Stavros Sachtouris
        prefix_replace=ValueArgument(
798 761e0cbf Stavros Sachtouris
            'Prefix of src to replace with dst path + add_prefix, if matched',
799 761e0cbf Stavros Sachtouris
            '--prefix-to-replace',
800 761e0cbf Stavros Sachtouris
            default=''),
801 761e0cbf Stavros Sachtouris
        suffix_replace=ValueArgument(
802 761e0cbf Stavros Sachtouris
            'Suffix of src to replace with add_suffix, if matched',
803 761e0cbf Stavros Sachtouris
            '--suffix-to-replace',
804 915b99b5 Stavros Sachtouris
            default='')
805 2fe2672e Stavros Sachtouris
    )
806 7493ccb6 Stavros Sachtouris
807 1395c40e Stavros Sachtouris
    @errors.generic.all
808 1395c40e Stavros Sachtouris
    @errors.pithos.connection
809 1395c40e Stavros Sachtouris
    @errors.pithos.container
810 300da0fb Stavros Sachtouris
    def _run(self, dst_path):
811 1395c40e Stavros Sachtouris
        no_source_object = True
812 300da0fb Stavros Sachtouris
        src_account = self.client.account if (
813 300da0fb Stavros Sachtouris
            self['destination_account']) else None
814 300da0fb Stavros Sachtouris
        for src_obj, dst_obj in self.src_dst_pairs(dst_path):
815 1395c40e Stavros Sachtouris
            no_source_object = False
816 55c75058 Stavros Sachtouris
            r = self.dst_client.move_object(
817 1395c40e Stavros Sachtouris
                src_container=self.container,
818 300da0fb Stavros Sachtouris
                src_object=src_obj,
819 300da0fb Stavros Sachtouris
                dst_container=self.dst_client.container,
820 300da0fb Stavros Sachtouris
                dst_object=dst_obj,
821 4f266635 Stavros Sachtouris
                source_account=src_account,
822 1395c40e Stavros Sachtouris
                public=self['public'],
823 1395c40e Stavros Sachtouris
                content_type=self['content_type'])
824 1395c40e Stavros Sachtouris
        if no_source_object:
825 1395c40e Stavros Sachtouris
            raiseCLIError('No object %s in container %s' % (
826 1395c40e Stavros Sachtouris
                self.path,
827 1395c40e Stavros Sachtouris
                self.container))
828 915b99b5 Stavros Sachtouris
        self._optional_output(r)
829 1395c40e Stavros Sachtouris
830 de73876b Stavros Sachtouris
    def main(
831 e2d7b883 Stavros Sachtouris
            self, source_container___path,
832 24ff0a35 Stavros Sachtouris
            destination_container___path=None):
833 1395c40e Stavros Sachtouris
        super(self.__class__, self)._run(
834 1395c40e Stavros Sachtouris
            source_container___path,
835 1395c40e Stavros Sachtouris
            path_is_optional=False)
836 45b70023 Stavros Sachtouris
        (dst_cont, dst_path) = self._dest_container_path(
837 1395c40e Stavros Sachtouris
            destination_container___path)
838 300da0fb Stavros Sachtouris
        (dst_cont, dst_path) = self._dest_container_path(
839 300da0fb Stavros Sachtouris
            destination_container___path)
840 300da0fb Stavros Sachtouris
        self.dst_client.container = dst_cont or self.container
841 300da0fb Stavros Sachtouris
        self._run(dst_path=dst_path or '')
842 7493ccb6 Stavros Sachtouris
843 234954d1 Stavros Sachtouris
844 d486baec Stavros Sachtouris
@command(pithos_cmds)
845 915b99b5 Stavros Sachtouris
class file_append(_file_container_command, _optional_output_cmd):
846 aaca2ef4 Stavros Sachtouris
    """Append local file to (existing) remote object
847 aaca2ef4 Stavros Sachtouris
    The remote object should exist.
848 aaca2ef4 Stavros Sachtouris
    If the remote object is a directory, it is transformed into a file.
849 aaca2ef4 Stavros Sachtouris
    In the later case, objects under the directory remain intact.
850 aaca2ef4 Stavros Sachtouris
    """
851 7493ccb6 Stavros Sachtouris
852 2fe2672e Stavros Sachtouris
    arguments = dict(
853 2fe2672e Stavros Sachtouris
        progress_bar=ProgressBarArgument(
854 2fe2672e Stavros Sachtouris
            'do not show progress bar',
855 f40f0cb7 Stavros Sachtouris
            ('-N', '--no-progress-bar'),
856 915b99b5 Stavros Sachtouris
            default=False)
857 2fe2672e Stavros Sachtouris
    )
858 486f7af1 Stavros Sachtouris
859 ca092af4 Stavros Sachtouris
    @errors.generic.all
860 ca092af4 Stavros Sachtouris
    @errors.pithos.connection
861 ca092af4 Stavros Sachtouris
    @errors.pithos.container
862 ca092af4 Stavros Sachtouris
    @errors.pithos.object_path
863 ca092af4 Stavros Sachtouris
    def _run(self, local_path):
864 ca092af4 Stavros Sachtouris
        (progress_bar, upload_cb) = self._safe_progress_bar('Appending')
865 7493ccb6 Stavros Sachtouris
        try:
866 e876ac62 Stavros Sachtouris
            f = open(local_path, 'rb')
867 915b99b5 Stavros Sachtouris
            self._optional_output(
868 915b99b5 Stavros Sachtouris
                self.client.append_object(self.path, f, upload_cb))
869 ca092af4 Stavros Sachtouris
        except Exception:
870 ca092af4 Stavros Sachtouris
            self._safe_progress_bar_finish(progress_bar)
871 ca092af4 Stavros Sachtouris
            raise
872 852a22e7 Stavros Sachtouris
        finally:
873 ca092af4 Stavros Sachtouris
            self._safe_progress_bar_finish(progress_bar)
874 ca092af4 Stavros Sachtouris
875 ca092af4 Stavros Sachtouris
    def main(self, local_path, container___path):
876 ca092af4 Stavros Sachtouris
        super(self.__class__, self)._run(
877 201baa17 Stavros Sachtouris
            container___path, path_is_optional=False)
878 ca092af4 Stavros Sachtouris
        self._run(local_path)
879 7493ccb6 Stavros Sachtouris
880 234954d1 Stavros Sachtouris
881 d486baec Stavros Sachtouris
@command(pithos_cmds)
882 915b99b5 Stavros Sachtouris
class file_truncate(_file_container_command, _optional_output_cmd):
883 aaca2ef4 Stavros Sachtouris
    """Truncate remote file up to a size (default is 0)"""
884 7493ccb6 Stavros Sachtouris
885 ca092af4 Stavros Sachtouris
    @errors.generic.all
886 ca092af4 Stavros Sachtouris
    @errors.pithos.connection
887 ca092af4 Stavros Sachtouris
    @errors.pithos.container
888 ca092af4 Stavros Sachtouris
    @errors.pithos.object_path
889 ca092af4 Stavros Sachtouris
    @errors.pithos.object_size
890 ca092af4 Stavros Sachtouris
    def _run(self, size=0):
891 915b99b5 Stavros Sachtouris
        self._optional_output(self.client.truncate_object(self.path, size))
892 ca092af4 Stavros Sachtouris
893 7493ccb6 Stavros Sachtouris
    def main(self, container___path, size=0):
894 ca092af4 Stavros Sachtouris
        super(self.__class__, self)._run(container___path)
895 ca092af4 Stavros Sachtouris
        self._run(size=size)
896 7493ccb6 Stavros Sachtouris
897 234954d1 Stavros Sachtouris
898 d486baec Stavros Sachtouris
@command(pithos_cmds)
899 915b99b5 Stavros Sachtouris
class file_overwrite(_file_container_command, _optional_output_cmd):
900 aaca2ef4 Stavros Sachtouris
    """Overwrite part (from start to end) of a remote file
901 aaca2ef4 Stavros Sachtouris
    overwrite local-path container 10 20
902 aaca2ef4 Stavros Sachtouris
    .   will overwrite bytes from 10 to 20 of a remote file with the same name
903 aaca2ef4 Stavros Sachtouris
    .   as local-path basename
904 aaca2ef4 Stavros Sachtouris
    overwrite local-path container:path 10 20
905 aaca2ef4 Stavros Sachtouris
    .   will overwrite as above, but the remote file is named path
906 aaca2ef4 Stavros Sachtouris
    """
907 7493ccb6 Stavros Sachtouris
908 2fe2672e Stavros Sachtouris
    arguments = dict(
909 2fe2672e Stavros Sachtouris
        progress_bar=ProgressBarArgument(
910 2fe2672e Stavros Sachtouris
            'do not show progress bar',
911 f40f0cb7 Stavros Sachtouris
            ('-N', '--no-progress-bar'),
912 2fe2672e Stavros Sachtouris
            default=False)
913 2fe2672e Stavros Sachtouris
    )
914 486f7af1 Stavros Sachtouris
915 ca092af4 Stavros Sachtouris
    def _open_file(self, local_path, start):
916 ca092af4 Stavros Sachtouris
        f = open(path.abspath(local_path), 'rb')
917 ca092af4 Stavros Sachtouris
        f.seek(0, 2)
918 ca092af4 Stavros Sachtouris
        f_size = f.tell()
919 ca092af4 Stavros Sachtouris
        f.seek(start, 0)
920 ca092af4 Stavros Sachtouris
        return (f, f_size)
921 ca092af4 Stavros Sachtouris
922 ca092af4 Stavros Sachtouris
    @errors.generic.all
923 ca092af4 Stavros Sachtouris
    @errors.pithos.connection
924 ca092af4 Stavros Sachtouris
    @errors.pithos.container
925 ca092af4 Stavros Sachtouris
    @errors.pithos.object_path
926 ca092af4 Stavros Sachtouris
    @errors.pithos.object_size
927 ca092af4 Stavros Sachtouris
    def _run(self, local_path, start, end):
928 ca092af4 Stavros Sachtouris
        (start, end) = (int(start), int(end))
929 ca092af4 Stavros Sachtouris
        (f, f_size) = self._open_file(local_path, start)
930 ca092af4 Stavros Sachtouris
        (progress_bar, upload_cb) = self._safe_progress_bar(
931 ca092af4 Stavros Sachtouris
            'Overwrite %s bytes' % (end - start))
932 aaca2ef4 Stavros Sachtouris
        try:
933 915b99b5 Stavros Sachtouris
            self._optional_output(self.client.overwrite_object(
934 8fa6ef6a Stavros Sachtouris
                obj=self.path,
935 234954d1 Stavros Sachtouris
                start=start,
936 234954d1 Stavros Sachtouris
                end=end,
937 234954d1 Stavros Sachtouris
                source_file=f,
938 915b99b5 Stavros Sachtouris
                upload_cb=upload_cb))
939 852a22e7 Stavros Sachtouris
        finally:
940 ca092af4 Stavros Sachtouris
            self._safe_progress_bar_finish(progress_bar)
941 ca092af4 Stavros Sachtouris
942 52edad0a Stavros Sachtouris
    def main(self, local_path, container___path, start, end):
943 52edad0a Stavros Sachtouris
        super(self.__class__, self)._run(
944 201baa17 Stavros Sachtouris
            container___path, path_is_optional=None)
945 52edad0a Stavros Sachtouris
        self.path = self.path or path.basename(local_path)
946 ca092af4 Stavros Sachtouris
        self._run(local_path=local_path, start=start, end=end)
947 7493ccb6 Stavros Sachtouris
948 234954d1 Stavros Sachtouris
949 d486baec Stavros Sachtouris
@command(pithos_cmds)
950 915b99b5 Stavros Sachtouris
class file_manifest(_file_container_command, _optional_output_cmd):
951 8fa6ef6a Stavros Sachtouris
    """Create a remote file of uploaded parts by manifestation
952 35b52b0e Stavros Sachtouris
    Remains functional for compatibility with OOS Storage. Users are advised
953 35b52b0e Stavros Sachtouris
    to use the upload command instead.
954 35b52b0e Stavros Sachtouris
    Manifestation is a compliant process for uploading large files. The files
955 35b52b0e Stavros Sachtouris
    have to be chunked in smalled files and uploaded as <prefix><increment>
956 35b52b0e Stavros Sachtouris
    where increment is 1, 2, ...
957 35b52b0e Stavros Sachtouris
    Finally, the manifest command glues partial files together in one file
958 35b52b0e Stavros Sachtouris
    named <prefix>
959 35b52b0e Stavros Sachtouris
    The upload command is faster, easier and more intuitive than manifest
960 8fa6ef6a Stavros Sachtouris
    """
961 486f7af1 Stavros Sachtouris
962 2fe2672e Stavros Sachtouris
    arguments = dict(
963 2fe2672e Stavros Sachtouris
        etag=ValueArgument('check written data', '--etag'),
964 2fe2672e Stavros Sachtouris
        content_encoding=ValueArgument(
965 201baa17 Stavros Sachtouris
            'set MIME content type', '--content-encoding'),
966 2fe2672e Stavros Sachtouris
        content_disposition=ValueArgument(
967 201baa17 Stavros Sachtouris
            'the presentation style of the object', '--content-disposition'),
968 8fa6ef6a Stavros Sachtouris
        content_type=ValueArgument(
969 201baa17 Stavros Sachtouris
            'specify content type', '--content-type',
970 8fa6ef6a Stavros Sachtouris
            default='application/octet-stream'),
971 2fe2672e Stavros Sachtouris
        sharing=SharingArgument(
972 de73876b Stavros Sachtouris
            '\n'.join([
973 de73876b Stavros Sachtouris
                'define object sharing policy',
974 de73876b Stavros Sachtouris
                '    ( "read=user1,grp1,user2,... write=user1,grp2,..." )']),
975 2fe2672e Stavros Sachtouris
            '--sharing'),
976 2fe2672e Stavros Sachtouris
        public=FlagArgument('make object publicly accessible', '--public')
977 2fe2672e Stavros Sachtouris
    )
978 234954d1 Stavros Sachtouris
979 68858765 Stavros Sachtouris
    @errors.generic.all
980 68858765 Stavros Sachtouris
    @errors.pithos.connection
981 68858765 Stavros Sachtouris
    @errors.pithos.container
982 68858765 Stavros Sachtouris
    @errors.pithos.object_path
983 68858765 Stavros Sachtouris
    def _run(self):
984 915b99b5 Stavros Sachtouris
        self._optional_output(self.client.create_object_by_manifestation(
985 68858765 Stavros Sachtouris
            self.path,
986 68858765 Stavros Sachtouris
            content_encoding=self['content_encoding'],
987 68858765 Stavros Sachtouris
            content_disposition=self['content_disposition'],
988 68858765 Stavros Sachtouris
            content_type=self['content_type'],
989 68858765 Stavros Sachtouris
            sharing=self['sharing'],
990 915b99b5 Stavros Sachtouris
            public=self['public']))
991 68858765 Stavros Sachtouris
992 7493ccb6 Stavros Sachtouris
    def main(self, container___path):
993 68858765 Stavros Sachtouris
        super(self.__class__, self)._run(
994 201baa17 Stavros Sachtouris
            container___path, path_is_optional=False)
995 68858765 Stavros Sachtouris
        self.run()
996 7493ccb6 Stavros Sachtouris
997 234954d1 Stavros Sachtouris
998 d486baec Stavros Sachtouris
@command(pithos_cmds)
999 915b99b5 Stavros Sachtouris
class file_upload(_file_container_command, _optional_output_cmd):
1000 7493ccb6 Stavros Sachtouris
    """Upload a file"""
1001 7493ccb6 Stavros Sachtouris
1002 2fe2672e Stavros Sachtouris
    arguments = dict(
1003 2fe2672e Stavros Sachtouris
        use_hashes=FlagArgument(
1004 201baa17 Stavros Sachtouris
            'provide hashmap file instead of data', '--use-hashes'),
1005 2fe2672e Stavros Sachtouris
        etag=ValueArgument('check written data', '--etag'),
1006 2fe2672e Stavros Sachtouris
        unchunked=FlagArgument('avoid chunked transfer mode', '--unchunked'),
1007 2fe2672e Stavros Sachtouris
        content_encoding=ValueArgument(
1008 201baa17 Stavros Sachtouris
            'set MIME content type', '--content-encoding'),
1009 2fe2672e Stavros Sachtouris
        content_disposition=ValueArgument(
1010 201baa17 Stavros Sachtouris
            'specify objects presentation style', '--content-disposition'),
1011 2fe2672e Stavros Sachtouris
        content_type=ValueArgument('specify content type', '--content-type'),
1012 2fe2672e Stavros Sachtouris
        sharing=SharingArgument(
1013 de73876b Stavros Sachtouris
            help='\n'.join([
1014 de73876b Stavros Sachtouris
                'define sharing object policy',
1015 de73876b Stavros Sachtouris
                '( "read=user1,grp1,user2,... write=user1,grp2,... )']),
1016 2fe2672e Stavros Sachtouris
            parsed_name='--sharing'),
1017 2fe2672e Stavros Sachtouris
        public=FlagArgument('make object publicly accessible', '--public'),
1018 2fe2672e Stavros Sachtouris
        poolsize=IntArgument('set pool size', '--with-pool-size'),
1019 2fe2672e Stavros Sachtouris
        progress_bar=ProgressBarArgument(
1020 2fe2672e Stavros Sachtouris
            'do not show progress bar',
1021 8741c407 Stavros Sachtouris
            ('-N', '--no-progress-bar'),
1022 706fc940 Stavros Sachtouris
            default=False),
1023 2d158d8b Stavros Sachtouris
        overwrite=FlagArgument('Force (over)write', ('-f', '--force')),
1024 2d158d8b Stavros Sachtouris
        recursive=FlagArgument(
1025 2d158d8b Stavros Sachtouris
            'Recursively upload directory *contents* + subdirectories',
1026 915b99b5 Stavros Sachtouris
            ('-R', '--recursive'))
1027 2fe2672e Stavros Sachtouris
    )
1028 7493ccb6 Stavros Sachtouris
1029 14b25e00 Stavros Sachtouris
    def _check_container_limit(self, path):
1030 14b25e00 Stavros Sachtouris
        cl_dict = self.client.get_container_limit()
1031 14b25e00 Stavros Sachtouris
        container_limit = int(cl_dict['x-container-policy-quota'])
1032 2b74fc1d Stavros Sachtouris
        r = self.client.container_get()
1033 2b74fc1d Stavros Sachtouris
        used_bytes = sum(int(o['bytes']) for o in r.json)
1034 14b25e00 Stavros Sachtouris
        path_size = get_path_size(path)
1035 c8d8c6c9 Stavros Sachtouris
        if container_limit and path_size > (container_limit - used_bytes):
1036 2b74fc1d Stavros Sachtouris
            raiseCLIError(
1037 2b74fc1d Stavros Sachtouris
                'Container(%s) (limit(%s) - used(%s)) < size(%s) of %s' % (
1038 14b25e00 Stavros Sachtouris
                    self.client.container,
1039 14b25e00 Stavros Sachtouris
                    format_size(container_limit),
1040 2b74fc1d Stavros Sachtouris
                    format_size(used_bytes),
1041 14b25e00 Stavros Sachtouris
                    format_size(path_size),
1042 14b25e00 Stavros Sachtouris
                    path),
1043 14b25e00 Stavros Sachtouris
                importance=1, details=[
1044 14b25e00 Stavros Sachtouris
                    'Check accound limit: /file quota',
1045 14b25e00 Stavros Sachtouris
                    'Check container limit:',
1046 14b25e00 Stavros Sachtouris
                    '\t/file containerlimit get %s' % self.client.container,
1047 14b25e00 Stavros Sachtouris
                    'Increase container limit:',
1048 14b25e00 Stavros Sachtouris
                    '\t/file containerlimit set <new limit> %s' % (
1049 14b25e00 Stavros Sachtouris
                        self.client.container)])
1050 14b25e00 Stavros Sachtouris
1051 b666ef82 Stavros Sachtouris
    def _path_pairs(self, local_path, remote_path):
1052 b666ef82 Stavros Sachtouris
        """Get pairs of local and remote paths"""
1053 b666ef82 Stavros Sachtouris
        lpath = path.abspath(local_path)
1054 b666ef82 Stavros Sachtouris
        short_path = lpath.split(path.sep)[-1]
1055 b666ef82 Stavros Sachtouris
        rpath = remote_path or short_path
1056 b666ef82 Stavros Sachtouris
        if path.isdir(lpath):
1057 2d158d8b Stavros Sachtouris
            if not self['recursive']:
1058 2d158d8b Stavros Sachtouris
                raiseCLIError('%s is a directory' % lpath, details=[
1059 2d158d8b Stavros Sachtouris
                    'Use -R to upload directory contents'])
1060 b666ef82 Stavros Sachtouris
            robj = self.client.container_get(path=rpath)
1061 b666ef82 Stavros Sachtouris
            if robj.json and not self['overwrite']:
1062 b666ef82 Stavros Sachtouris
                raiseCLIError(
1063 b666ef82 Stavros Sachtouris
                    'Objects prefixed with %s already exist' % rpath,
1064 b666ef82 Stavros Sachtouris
                    importance=1,
1065 b666ef82 Stavros Sachtouris
                    details=['Existing objects:'] + ['\t%s:\t%s' % (
1066 b666ef82 Stavros Sachtouris
                        o['content_type'][12:],
1067 b666ef82 Stavros Sachtouris
                        o['name']) for o in robj.json] + [
1068 b666ef82 Stavros Sachtouris
                        'Use -f to add, overwrite or resume'])
1069 b666ef82 Stavros Sachtouris
            if not self['overwrite']:
1070 b666ef82 Stavros Sachtouris
                try:
1071 b666ef82 Stavros Sachtouris
                    topobj = self.client.get_object_info(rpath)
1072 b666ef82 Stavros Sachtouris
                    if not self._is_dir(topobj):
1073 b666ef82 Stavros Sachtouris
                        raiseCLIError(
1074 b666ef82 Stavros Sachtouris
                            'Object %s exists but it is not a dir' % rpath,
1075 b666ef82 Stavros Sachtouris
                            importance=1, details=['Use -f to overwrite'])
1076 b666ef82 Stavros Sachtouris
                except ClientError as ce:
1077 b666ef82 Stavros Sachtouris
                    if ce.status != 404:
1078 b666ef82 Stavros Sachtouris
                        raise
1079 2d158d8b Stavros Sachtouris
            self._check_container_limit(lpath)
1080 b666ef82 Stavros Sachtouris
            prev = ''
1081 b666ef82 Stavros Sachtouris
            for top, subdirs, files in walk(lpath):
1082 b666ef82 Stavros Sachtouris
                if top != prev:
1083 b666ef82 Stavros Sachtouris
                    prev = top
1084 b666ef82 Stavros Sachtouris
                    try:
1085 b666ef82 Stavros Sachtouris
                        rel_path = rpath + top.split(lpath)[1]
1086 b666ef82 Stavros Sachtouris
                    except IndexError:
1087 b666ef82 Stavros Sachtouris
                        rel_path = rpath
1088 b666ef82 Stavros Sachtouris
                    print('mkdir %s:%s' % (self.client.container, rel_path))
1089 b666ef82 Stavros Sachtouris
                    self.client.create_directory(rel_path)
1090 b666ef82 Stavros Sachtouris
                for f in files:
1091 b666ef82 Stavros Sachtouris
                    fpath = path.join(top, f)
1092 b666ef82 Stavros Sachtouris
                    if path.isfile(fpath):
1093 14d15eca Stavros Sachtouris
                        rel_path = rel_path.replace(path.sep, '/')
1094 14d15eca Stavros Sachtouris
                        pathfix = f.replace(path.sep, '/')
1095 14d15eca Stavros Sachtouris
                        yield open(fpath, 'rb'), '%s/%s' % (rel_path, pathfix)
1096 b666ef82 Stavros Sachtouris
                    else:
1097 b666ef82 Stavros Sachtouris
                        print('%s is not a regular file' % fpath)
1098 b666ef82 Stavros Sachtouris
        else:
1099 14b25e00 Stavros Sachtouris
            if not path.isfile(lpath):
1100 14b25e00 Stavros Sachtouris
                raiseCLIError('%s is not a regular file' % lpath)
1101 b666ef82 Stavros Sachtouris
            try:
1102 b666ef82 Stavros Sachtouris
                robj = self.client.get_object_info(rpath)
1103 b666ef82 Stavros Sachtouris
                if remote_path and self._is_dir(robj):
1104 14d15eca Stavros Sachtouris
                    rpath += '/%s' % (short_path.replace(path.sep, '/'))
1105 b666ef82 Stavros Sachtouris
                    self.client.get_object_info(rpath)
1106 b666ef82 Stavros Sachtouris
                if not self['overwrite']:
1107 b666ef82 Stavros Sachtouris
                    raiseCLIError(
1108 b666ef82 Stavros Sachtouris
                        'Object %s already exists' % rpath,
1109 b666ef82 Stavros Sachtouris
                        importance=1,
1110 b666ef82 Stavros Sachtouris
                        details=['use -f to overwrite or resume'])
1111 b666ef82 Stavros Sachtouris
            except ClientError as ce:
1112 b666ef82 Stavros Sachtouris
                if ce.status != 404:
1113 b666ef82 Stavros Sachtouris
                    raise
1114 2d158d8b Stavros Sachtouris
            self._check_container_limit(lpath)
1115 b666ef82 Stavros Sachtouris
            yield open(lpath, 'rb'), rpath
1116 706fc940 Stavros Sachtouris
1117 68858765 Stavros Sachtouris
    @errors.generic.all
1118 68858765 Stavros Sachtouris
    @errors.pithos.connection
1119 68858765 Stavros Sachtouris
    @errors.pithos.container
1120 68858765 Stavros Sachtouris
    @errors.pithos.object_path
1121 68858765 Stavros Sachtouris
    @errors.pithos.local_path
1122 68858765 Stavros Sachtouris
    def _run(self, local_path, remote_path):
1123 2fe2672e Stavros Sachtouris
        poolsize = self['poolsize']
1124 72952f4f Stavros Sachtouris
        if poolsize > 0:
1125 16b0afe6 Stavros Sachtouris
            self.client.MAX_THREADS = int(poolsize)
1126 72952f4f Stavros Sachtouris
        params = dict(
1127 72952f4f Stavros Sachtouris
            content_encoding=self['content_encoding'],
1128 2fe2672e Stavros Sachtouris
            content_type=self['content_type'],
1129 2fe2672e Stavros Sachtouris
            content_disposition=self['content_disposition'],
1130 2fe2672e Stavros Sachtouris
            sharing=self['sharing'],
1131 2fe2672e Stavros Sachtouris
            public=self['public'])
1132 0e728dcb Stavros Sachtouris
        uploaded = []
1133 74c65e80 Stavros Sachtouris
        container_info_cache = dict()
1134 b666ef82 Stavros Sachtouris
        for f, rpath in self._path_pairs(local_path, remote_path):
1135 b666ef82 Stavros Sachtouris
            print('%s --> %s:%s' % (f.name, self.client.container, rpath))
1136 68858765 Stavros Sachtouris
            if self['unchunked']:
1137 0e728dcb Stavros Sachtouris
                r = self.client.upload_object_unchunked(
1138 b666ef82 Stavros Sachtouris
                    rpath, f,
1139 b666ef82 Stavros Sachtouris
                    etag=self['etag'], withHashFile=self['use_hashes'],
1140 68858765 Stavros Sachtouris
                    **params)
1141 5655d560 Stavros Sachtouris
                if self['with_output'] or self['json_output']:
1142 0e728dcb Stavros Sachtouris
                    r['name'] = '%s: %s' % (self.client.container, rpath)
1143 0e728dcb Stavros Sachtouris
                    uploaded.append(r)
1144 68858765 Stavros Sachtouris
            else:
1145 68858765 Stavros Sachtouris
                try:
1146 68858765 Stavros Sachtouris
                    (progress_bar, upload_cb) = self._safe_progress_bar(
1147 b666ef82 Stavros Sachtouris
                        'Uploading %s' % f.name.split(path.sep)[-1])
1148 68858765 Stavros Sachtouris
                    if progress_bar:
1149 68858765 Stavros Sachtouris
                        hash_bar = progress_bar.clone()
1150 68858765 Stavros Sachtouris
                        hash_cb = hash_bar.get_generator(
1151 278c9018 Stavros Sachtouris
                            'Calculating block hashes')
1152 68858765 Stavros Sachtouris
                    else:
1153 68858765 Stavros Sachtouris
                        hash_cb = None
1154 0e728dcb Stavros Sachtouris
                    r = self.client.upload_object(
1155 b666ef82 Stavros Sachtouris
                        rpath, f,
1156 74c65e80 Stavros Sachtouris
                        hash_cb=hash_cb,
1157 74c65e80 Stavros Sachtouris
                        upload_cb=upload_cb,
1158 74c65e80 Stavros Sachtouris
                        container_info_cache=container_info_cache,
1159 852a22e7 Stavros Sachtouris
                        **params)
1160 5655d560 Stavros Sachtouris
                    if self['with_output'] or self['json_output']:
1161 0e728dcb Stavros Sachtouris
                        r['name'] = '%s: %s' % (self.client.container, rpath)
1162 0e728dcb Stavros Sachtouris
                        uploaded.append(r)
1163 68858765 Stavros Sachtouris
                except Exception:
1164 68858765 Stavros Sachtouris
                    self._safe_progress_bar_finish(progress_bar)
1165 68858765 Stavros Sachtouris
                    raise
1166 68858765 Stavros Sachtouris
                finally:
1167 68858765 Stavros Sachtouris
                    self._safe_progress_bar_finish(progress_bar)
1168 d9301a7a Stavros Sachtouris
        self._optional_output(uploaded)
1169 915b99b5 Stavros Sachtouris
        print('Upload completed')
1170 7493ccb6 Stavros Sachtouris
1171 68858765 Stavros Sachtouris
    def main(self, local_path, container____path__=None):
1172 68858765 Stavros Sachtouris
        super(self.__class__, self)._run(container____path__)
1173 9e0a1a4c Stavros Sachtouris
        remote_path = self.path or path.basename(path.abspath(local_path))
1174 68858765 Stavros Sachtouris
        self._run(local_path=local_path, remote_path=remote_path)
1175 68858765 Stavros Sachtouris
1176 234954d1 Stavros Sachtouris
1177 d486baec Stavros Sachtouris
@command(pithos_cmds)
1178 3ae60112 Stavros Sachtouris
class file_cat(_file_container_command):
1179 72952f4f Stavros Sachtouris
    """Print remote file contents to console"""
1180 9ceec15a Stavros Sachtouris
1181 2fe2672e Stavros Sachtouris
    arguments = dict(
1182 2fe2672e Stavros Sachtouris
        range=RangeArgument('show range of data', '--range'),
1183 2fe2672e Stavros Sachtouris
        if_match=ValueArgument('show output if ETags match', '--if-match'),
1184 2fe2672e Stavros Sachtouris
        if_none_match=ValueArgument(
1185 201baa17 Stavros Sachtouris
            'show output if ETags match', '--if-none-match'),
1186 2fe2672e Stavros Sachtouris
        if_modified_since=DateArgument(
1187 201baa17 Stavros Sachtouris
            'show output modified since then', '--if-modified-since'),
1188 2fe2672e Stavros Sachtouris
        if_unmodified_since=DateArgument(
1189 201baa17 Stavros Sachtouris
            'show output unmodified since then', '--if-unmodified-since'),
1190 2fe2672e Stavros Sachtouris
        object_version=ValueArgument(
1191 201baa17 Stavros Sachtouris
            'get the specific version', ('-O', '--object-version'))
1192 2fe2672e Stavros Sachtouris
    )
1193 2fe2672e Stavros Sachtouris
1194 68858765 Stavros Sachtouris
    @errors.generic.all
1195 68858765 Stavros Sachtouris
    @errors.pithos.connection
1196 68858765 Stavros Sachtouris
    @errors.pithos.container
1197 68858765 Stavros Sachtouris
    @errors.pithos.object_path
1198 68858765 Stavros Sachtouris
    def _run(self):
1199 68858765 Stavros Sachtouris
        self.client.download_object(
1200 68858765 Stavros Sachtouris
            self.path,
1201 68858765 Stavros Sachtouris
            stdout,
1202 3d568c09 Stavros Sachtouris
            range_str=self['range'],
1203 2fe2672e Stavros Sachtouris
            version=self['object_version'],
1204 2fe2672e Stavros Sachtouris
            if_match=self['if_match'],
1205 2fe2672e Stavros Sachtouris
            if_none_match=self['if_none_match'],
1206 2fe2672e Stavros Sachtouris
            if_modified_since=self['if_modified_since'],
1207 2fe2672e Stavros Sachtouris
            if_unmodified_since=self['if_unmodified_since'])
1208 68858765 Stavros Sachtouris
1209 68858765 Stavros Sachtouris
    def main(self, container___path):
1210 68858765 Stavros Sachtouris
        super(self.__class__, self)._run(
1211 201baa17 Stavros Sachtouris
            container___path, path_is_optional=False)
1212 68858765 Stavros Sachtouris
        self._run()
1213 9ceec15a Stavros Sachtouris
1214 234954d1 Stavros Sachtouris
1215 d486baec Stavros Sachtouris
@command(pithos_cmds)
1216 3ae60112 Stavros Sachtouris
class file_download(_file_container_command):
1217 ae99b37d Stavros Sachtouris
    """Download remote object as local file
1218 ae99b37d Stavros Sachtouris
    If local destination is a directory:
1219 f40f0cb7 Stavros Sachtouris
    *   download <container>:<path> <local dir> -R
1220 ae99b37d Stavros Sachtouris
    will download all files on <container> prefixed as <path>,
1221 c626151a Stavros Sachtouris
    to <local dir>/<full path> (or <local dir>\<full path> in windows)
1222 ae99b37d Stavros Sachtouris
    *   download <container>:<path> <local dir> --exact-match
1223 ae99b37d Stavros Sachtouris
    will download only one file, exactly matching <path>
1224 f6c09d14 Stavros Sachtouris
    ATTENTION: to download cont:dir1/dir2/file there must exist objects
1225 f6c09d14 Stavros Sachtouris
    cont:dir1 and cont:dir1/dir2 of type application/directory
1226 3ae60112 Stavros Sachtouris
    To create directory objects, use /file mkdir
1227 ae99b37d Stavros Sachtouris
    """
1228 7493ccb6 Stavros Sachtouris
1229 2fe2672e Stavros Sachtouris
    arguments = dict(
1230 f40f0cb7 Stavros Sachtouris
        resume=FlagArgument('Resume instead of overwrite', ('-r', '--resume')),
1231 2fe2672e Stavros Sachtouris
        range=RangeArgument('show range of data', '--range'),
1232 2fe2672e Stavros Sachtouris
        if_match=ValueArgument('show output if ETags match', '--if-match'),
1233 2fe2672e Stavros Sachtouris
        if_none_match=ValueArgument(
1234 201baa17 Stavros Sachtouris
            'show output if ETags match', '--if-none-match'),
1235 2fe2672e Stavros Sachtouris
        if_modified_since=DateArgument(
1236 201baa17 Stavros Sachtouris
            'show output modified since then', '--if-modified-since'),
1237 2fe2672e Stavros Sachtouris
        if_unmodified_since=DateArgument(
1238 201baa17 Stavros Sachtouris
            'show output unmodified since then', '--if-unmodified-since'),
1239 2fe2672e Stavros Sachtouris
        object_version=ValueArgument(
1240 201baa17 Stavros Sachtouris
            'get the specific version', ('-O', '--object-version')),
1241 2fe2672e Stavros Sachtouris
        poolsize=IntArgument('set pool size', '--with-pool-size'),
1242 2fe2672e Stavros Sachtouris
        progress_bar=ProgressBarArgument(
1243 2fe2672e Stavros Sachtouris
            'do not show progress bar',
1244 f40f0cb7 Stavros Sachtouris
            ('-N', '--no-progress-bar'),
1245 ae99b37d Stavros Sachtouris
            default=False),
1246 f6c09d14 Stavros Sachtouris
        recursive=FlagArgument(
1247 a3ba3bce Stavros Sachtouris
            'Download a remote path and all its contents',
1248 f40f0cb7 Stavros Sachtouris
            ('-R', '--recursive'))
1249 2fe2672e Stavros Sachtouris
    )
1250 7493ccb6 Stavros Sachtouris
1251 f6c09d14 Stavros Sachtouris
    def _outputs(self, local_path):
1252 a3ba3bce Stavros Sachtouris
        """:returns: (local_file, remote_path)"""
1253 a3ba3bce Stavros Sachtouris
        remotes = []
1254 a3ba3bce Stavros Sachtouris
        if self['recursive']:
1255 a3ba3bce Stavros Sachtouris
            r = self.client.container_get(
1256 a3ba3bce Stavros Sachtouris
                prefix=self.path or '/',
1257 f6c09d14 Stavros Sachtouris
                if_modified_since=self['if_modified_since'],
1258 f6c09d14 Stavros Sachtouris
                if_unmodified_since=self['if_unmodified_since'])
1259 a3ba3bce Stavros Sachtouris
            dirlist = dict()
1260 a3ba3bce Stavros Sachtouris
            for remote in r.json:
1261 a3ba3bce Stavros Sachtouris
                rname = remote['name'].strip('/')
1262 a3ba3bce Stavros Sachtouris
                tmppath = ''
1263 a3ba3bce Stavros Sachtouris
                for newdir in rname.strip('/').split('/')[:-1]:
1264 a3ba3bce Stavros Sachtouris
                    tmppath = '/'.join([tmppath, newdir])
1265 a3ba3bce Stavros Sachtouris
                    dirlist.update({tmppath.strip('/'): True})
1266 3ae60112 Stavros Sachtouris
                remotes.append((rname, file_download._is_dir(remote)))
1267 a3ba3bce Stavros Sachtouris
            dir_remotes = [r[0] for r in remotes if r[1]]
1268 a3ba3bce Stavros Sachtouris
            if not set(dirlist).issubset(dir_remotes):
1269 a3ba3bce Stavros Sachtouris
                badguys = [bg.strip('/') for bg in set(
1270 a3ba3bce Stavros Sachtouris
                    dirlist).difference(dir_remotes)]
1271 a3ba3bce Stavros Sachtouris
                raiseCLIError(
1272 a3ba3bce Stavros Sachtouris
                    'Some remote paths contain non existing directories',
1273 a3ba3bce Stavros Sachtouris
                    details=['Missing remote directories:'] + badguys)
1274 a3ba3bce Stavros Sachtouris
        elif self.path:
1275 a3ba3bce Stavros Sachtouris
            r = self.client.get_object_info(
1276 a3ba3bce Stavros Sachtouris
                self.path,
1277 a3ba3bce Stavros Sachtouris
                version=self['object_version'])
1278 3ae60112 Stavros Sachtouris
            if file_download._is_dir(r):
1279 a3ba3bce Stavros Sachtouris
                raiseCLIError(
1280 a3ba3bce Stavros Sachtouris
                    'Illegal download: Remote object %s is a directory' % (
1281 a3ba3bce Stavros Sachtouris
                        self.path),
1282 14d15eca Stavros Sachtouris
                    details=['To download a directory, try --recursive or -R'])
1283 a3ba3bce Stavros Sachtouris
            if '/' in self.path.strip('/') and not local_path:
1284 a3ba3bce Stavros Sachtouris
                raiseCLIError(
1285 a3ba3bce Stavros Sachtouris
                    'Illegal download: remote object %s contains "/"' % (
1286 a3ba3bce Stavros Sachtouris
                        self.path),
1287 a3ba3bce Stavros Sachtouris
                    details=[
1288 a3ba3bce Stavros Sachtouris
                        'To download an object containing "/" characters',
1289 a3ba3bce Stavros Sachtouris
                        'either create the remote directories or',
1290 a3ba3bce Stavros Sachtouris
                        'specify a non-directory local path for this object'])
1291 a3ba3bce Stavros Sachtouris
            remotes = [(self.path, False)]
1292 a3ba3bce Stavros Sachtouris
        if not remotes:
1293 a3ba3bce Stavros Sachtouris
            if self.path:
1294 a3ba3bce Stavros Sachtouris
                raiseCLIError(
1295 a3ba3bce Stavros Sachtouris
                    'No matching path %s on container %s' % (
1296 14d15eca Stavros Sachtouris
                        self.path, self.container),
1297 a3ba3bce Stavros Sachtouris
                    details=[
1298 a3ba3bce Stavros Sachtouris
                        'To list the contents of %s, try:' % self.container,
1299 3ae60112 Stavros Sachtouris
                        '   /file list %s' % self.container])
1300 a3ba3bce Stavros Sachtouris
            raiseCLIError(
1301 a3ba3bce Stavros Sachtouris
                'Illegal download of container %s' % self.container,
1302 a3ba3bce Stavros Sachtouris
                details=[
1303 a3ba3bce Stavros Sachtouris
                    'To download a whole container, try:',
1304 3ae60112 Stavros Sachtouris
                    '   /file download --recursive <container>'])
1305 a3ba3bce Stavros Sachtouris
1306 a3ba3bce Stavros Sachtouris
        lprefix = path.abspath(local_path or path.curdir)
1307 a3ba3bce Stavros Sachtouris
        if path.isdir(lprefix):
1308 a3ba3bce Stavros Sachtouris
            for rpath, remote_is_dir in remotes:
1309 14d15eca Stavros Sachtouris
                lpath = path.sep.join([
1310 14d15eca Stavros Sachtouris
                    lprefix[:-1] if lprefix.endswith(path.sep) else lprefix,
1311 14d15eca Stavros Sachtouris
                    rpath.strip('/').replace('/', path.sep)])
1312 a3ba3bce Stavros Sachtouris
                if remote_is_dir:
1313 a3ba3bce Stavros Sachtouris
                    if path.exists(lpath) and path.isdir(lpath):
1314 a3ba3bce Stavros Sachtouris
                        continue
1315 a3ba3bce Stavros Sachtouris
                    makedirs(lpath)
1316 a3ba3bce Stavros Sachtouris
                elif path.exists(lpath):
1317 a3ba3bce Stavros Sachtouris
                    if not self['resume']:
1318 a3ba3bce Stavros Sachtouris
                        print('File %s exists, aborting...' % lpath)
1319 a3ba3bce Stavros Sachtouris
                        continue
1320 a3ba3bce Stavros Sachtouris
                    with open(lpath, 'rwb+') as f:
1321 a3ba3bce Stavros Sachtouris
                        yield (f, rpath)
1322 a3ba3bce Stavros Sachtouris
                else:
1323 a3ba3bce Stavros Sachtouris
                    with open(lpath, 'wb+') as f:
1324 a3ba3bce Stavros Sachtouris
                        yield (f, rpath)
1325 a3ba3bce Stavros Sachtouris
        elif path.exists(lprefix):
1326 a3ba3bce Stavros Sachtouris
            if len(remotes) > 1:
1327 a3ba3bce Stavros Sachtouris
                raiseCLIError(
1328 a3ba3bce Stavros Sachtouris
                    '%s remote objects cannot be merged in local file %s' % (
1329 a3ba3bce Stavros Sachtouris
                        len(remotes),
1330 a3ba3bce Stavros Sachtouris
                        local_path),
1331 a3ba3bce Stavros Sachtouris
                    details=[
1332 a3ba3bce Stavros Sachtouris
                        'To download multiple objects, local path should be',
1333 a3ba3bce Stavros Sachtouris
                        'a directory, or use download without a local path'])
1334 a3ba3bce Stavros Sachtouris
            (rpath, remote_is_dir) = remotes[0]
1335 a3ba3bce Stavros Sachtouris
            if remote_is_dir:
1336 a3ba3bce Stavros Sachtouris
                raiseCLIError(
1337 a3ba3bce Stavros Sachtouris
                    'Remote directory %s should not replace local file %s' % (
1338 a3ba3bce Stavros Sachtouris
                        rpath,
1339 a3ba3bce Stavros Sachtouris
                        local_path))
1340 a3ba3bce Stavros Sachtouris
            if self['resume']:
1341 a3ba3bce Stavros Sachtouris
                with open(lprefix, 'rwb+') as f:
1342 a3ba3bce Stavros Sachtouris
                    yield (f, rpath)
1343 a3ba3bce Stavros Sachtouris
            else:
1344 a3ba3bce Stavros Sachtouris
                raiseCLIError(
1345 db36a6a7 Stavros Sachtouris
                    'Local file %s already exist' % local_path,
1346 db36a6a7 Stavros Sachtouris
                    details=['Try --resume to overwrite it'])
1347 a3ba3bce Stavros Sachtouris
        else:
1348 a3ba3bce Stavros Sachtouris
            if len(remotes) > 1 or remotes[0][1]:
1349 a3ba3bce Stavros Sachtouris
                raiseCLIError(
1350 a3ba3bce Stavros Sachtouris
                    'Local directory %s does not exist' % local_path)
1351 a3ba3bce Stavros Sachtouris
            with open(lprefix, 'wb+') as f:
1352 a3ba3bce Stavros Sachtouris
                yield (f, remotes[0][0])
1353 7493ccb6 Stavros Sachtouris
1354 68858765 Stavros Sachtouris
    @errors.generic.all
1355 68858765 Stavros Sachtouris
    @errors.pithos.connection
1356 68858765 Stavros Sachtouris
    @errors.pithos.container
1357 68858765 Stavros Sachtouris
    @errors.pithos.object_path
1358 68858765 Stavros Sachtouris
    @errors.pithos.local_path
1359 68858765 Stavros Sachtouris
    def _run(self, local_path):
1360 2fe2672e Stavros Sachtouris
        poolsize = self['poolsize']
1361 68858765 Stavros Sachtouris
        if poolsize:
1362 16b0afe6 Stavros Sachtouris
            self.client.MAX_THREADS = int(poolsize)
1363 f6c09d14 Stavros Sachtouris
        progress_bar = None
1364 ae99b37d Stavros Sachtouris
        try:
1365 a3ba3bce Stavros Sachtouris
            for f, rpath in self._outputs(local_path):
1366 3d568c09 Stavros Sachtouris
                (
1367 3d568c09 Stavros Sachtouris
                    progress_bar,
1368 a3ba3bce Stavros Sachtouris
                    download_cb) = self._safe_progress_bar(
1369 a3ba3bce Stavros Sachtouris
                        'Download %s' % rpath)
1370 ae99b37d Stavros Sachtouris
                self.client.download_object(
1371 5655d560 Stavros Sachtouris
                    rpath, f,
1372 ae99b37d Stavros Sachtouris
                    download_cb=download_cb,
1373 3d568c09 Stavros Sachtouris
                    range_str=self['range'],
1374 ae99b37d Stavros Sachtouris
                    version=self['object_version'],
1375 ae99b37d Stavros Sachtouris
                    if_match=self['if_match'],
1376 ae99b37d Stavros Sachtouris
                    resume=self['resume'],
1377 ae99b37d Stavros Sachtouris
                    if_none_match=self['if_none_match'],
1378 ae99b37d Stavros Sachtouris
                    if_modified_since=self['if_modified_since'],
1379 ae99b37d Stavros Sachtouris
                    if_unmodified_since=self['if_unmodified_since'])
1380 7493ccb6 Stavros Sachtouris
        except KeyboardInterrupt:
1381 b78ee581 Stavros Sachtouris
            from threading import activeCount, enumerate as activethreads
1382 cae76f25 Stavros Sachtouris
            timeout = 0.5
1383 b78ee581 Stavros Sachtouris
            while activeCount() > 1:
1384 cae76f25 Stavros Sachtouris
                stdout.write('\nCancel %s threads: ' % (activeCount() - 1))
1385 cae76f25 Stavros Sachtouris
                stdout.flush()
1386 b78ee581 Stavros Sachtouris
                for thread in activethreads():
1387 b78ee581 Stavros Sachtouris
                    try:
1388 cae76f25 Stavros Sachtouris
                        thread.join(timeout)
1389 cae76f25 Stavros Sachtouris
                        stdout.write('.' if thread.isAlive() else '*')
1390 b78ee581 Stavros Sachtouris
                    except RuntimeError:
1391 b78ee581 Stavros Sachtouris
                        continue
1392 cae76f25 Stavros Sachtouris
                    finally:
1393 cae76f25 Stavros Sachtouris
                        stdout.flush()
1394 cae76f25 Stavros Sachtouris
                        timeout += 0.1
1395 b78ee581 Stavros Sachtouris
            print('\nDownload canceled by user')
1396 7493ccb6 Stavros Sachtouris
            if local_path is not None:
1397 624ee36f Stavros Sachtouris
                print('to resume, re-run with --resume')
1398 68858765 Stavros Sachtouris
        except Exception:
1399 68858765 Stavros Sachtouris
            self._safe_progress_bar_finish(progress_bar)
1400 68858765 Stavros Sachtouris
            raise
1401 68858765 Stavros Sachtouris
        finally:
1402 68858765 Stavros Sachtouris
            self._safe_progress_bar_finish(progress_bar)
1403 68858765 Stavros Sachtouris
1404 68858765 Stavros Sachtouris
    def main(self, container___path, local_path=None):
1405 a3ba3bce Stavros Sachtouris
        super(self.__class__, self)._run(container___path)
1406 68858765 Stavros Sachtouris
        self._run(local_path=local_path)
1407 7493ccb6 Stavros Sachtouris
1408 234954d1 Stavros Sachtouris
1409 d486baec Stavros Sachtouris
@command(pithos_cmds)
1410 545c6c29 Stavros Sachtouris
class file_hashmap(_file_container_command, _optional_json):
1411 72952f4f Stavros Sachtouris
    """Get the hash-map of an object"""
1412 7493ccb6 Stavros Sachtouris
1413 2fe2672e Stavros Sachtouris
    arguments = dict(
1414 2fe2672e Stavros Sachtouris
        if_match=ValueArgument('show output if ETags match', '--if-match'),
1415 2fe2672e Stavros Sachtouris
        if_none_match=ValueArgument(
1416 545c6c29 Stavros Sachtouris
            'show output if ETags match', '--if-none-match'),
1417 2fe2672e Stavros Sachtouris
        if_modified_since=DateArgument(
1418 545c6c29 Stavros Sachtouris
            'show output modified since then', '--if-modified-since'),
1419 2fe2672e Stavros Sachtouris
        if_unmodified_since=DateArgument(
1420 545c6c29 Stavros Sachtouris
            'show output unmodified since then', '--if-unmodified-since'),
1421 2fe2672e Stavros Sachtouris
        object_version=ValueArgument(
1422 545c6c29 Stavros Sachtouris
            'get the specific version', ('-O', '--object-version'))
1423 2fe2672e Stavros Sachtouris
    )
1424 7493ccb6 Stavros Sachtouris
1425 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1426 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1427 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1428 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1429 b4cf92b8 Stavros Sachtouris
    def _run(self):
1430 545c6c29 Stavros Sachtouris
        self._print(self.client.get_object_hashmap(
1431 b4cf92b8 Stavros Sachtouris
            self.path,
1432 b4cf92b8 Stavros Sachtouris
            version=self['object_version'],
1433 b4cf92b8 Stavros Sachtouris
            if_match=self['if_match'],
1434 b4cf92b8 Stavros Sachtouris
            if_none_match=self['if_none_match'],
1435 b4cf92b8 Stavros Sachtouris
            if_modified_since=self['if_modified_since'],
1436 545c6c29 Stavros Sachtouris
            if_unmodified_since=self['if_unmodified_since']), print_dict)
1437 b4cf92b8 Stavros Sachtouris
1438 7493ccb6 Stavros Sachtouris
    def main(self, container___path):
1439 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(
1440 72952f4f Stavros Sachtouris
            container___path,
1441 72952f4f Stavros Sachtouris
            path_is_optional=False)
1442 b4cf92b8 Stavros Sachtouris
        self._run()
1443 7493ccb6 Stavros Sachtouris
1444 234954d1 Stavros Sachtouris
1445 d486baec Stavros Sachtouris
@command(pithos_cmds)
1446 915b99b5 Stavros Sachtouris
class file_delete(_file_container_command, _optional_output_cmd):
1447 72952f4f Stavros Sachtouris
    """Delete a container [or an object]
1448 72952f4f Stavros Sachtouris
    How to delete a non-empty container:
1449 3ae60112 Stavros Sachtouris
    - empty the container:  /file delete -R <container>
1450 3ae60112 Stavros Sachtouris
    - delete it:            /file delete <container>
1451 72952f4f Stavros Sachtouris
    .
1452 72952f4f Stavros Sachtouris
    Semantics of directory deletion:
1453 3ae60112 Stavros Sachtouris
    .a preserve the contents: /file delete <container>:<directory>
1454 72952f4f Stavros Sachtouris
    .    objects of the form dir/filename can exist with a dir object
1455 3ae60112 Stavros Sachtouris
    .b delete contents:       /file delete -R <container>:<directory>
1456 72952f4f Stavros Sachtouris
    .    all dir/* objects are affected, even if dir does not exist
1457 72952f4f Stavros Sachtouris
    .
1458 72952f4f Stavros Sachtouris
    To restore a deleted object OBJ in a container CONT:
1459 3ae60112 Stavros Sachtouris
    - get object versions: /file versions CONT:OBJ
1460 72952f4f Stavros Sachtouris
    .   and choose the version to be restored
1461 3ae60112 Stavros Sachtouris
    - restore the object:  /file copy --source-version=<version> CONT:OBJ OBJ
1462 72952f4f Stavros Sachtouris
    """
1463 7493ccb6 Stavros Sachtouris
1464 2fe2672e Stavros Sachtouris
    arguments = dict(
1465 439826ec Stavros Sachtouris
        until=DateArgument('remove history until that date', '--until'),
1466 7147e1ca Stavros Sachtouris
        yes=FlagArgument('Do not prompt for permission', '--yes'),
1467 2fe2672e Stavros Sachtouris
        recursive=FlagArgument(
1468 234954d1 Stavros Sachtouris
            'empty dir or container and delete (if dir)',
1469 915b99b5 Stavros Sachtouris
            ('-R', '--recursive'))
1470 2fe2672e Stavros Sachtouris
    )
1471 2fe2672e Stavros Sachtouris
1472 b4f69041 Stavros Sachtouris
    def __init__(self, arguments={}, auth_base=None, cloud=None):
1473 b4f69041 Stavros Sachtouris
        super(self.__class__, self).__init__(arguments,  auth_base, cloud)
1474 439826ec Stavros Sachtouris
        self['delimiter'] = DelimiterArgument(
1475 2fe2672e Stavros Sachtouris
            self,
1476 234954d1 Stavros Sachtouris
            parsed_name='--delimiter',
1477 234954d1 Stavros Sachtouris
            help='delete objects prefixed with <object><delimiter>')
1478 7493ccb6 Stavros Sachtouris
1479 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1480 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1481 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1482 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1483 b4cf92b8 Stavros Sachtouris
    def _run(self):
1484 b4cf92b8 Stavros Sachtouris
        if self.path:
1485 24ff0a35 Stavros Sachtouris
            if self['yes'] or ask_user(
1486 24ff0a35 Stavros Sachtouris
                    'Delete %s:%s ?' % (self.container, self.path)):
1487 915b99b5 Stavros Sachtouris
                self._optional_output(self.client.del_object(
1488 b4cf92b8 Stavros Sachtouris
                    self.path,
1489 915b99b5 Stavros Sachtouris
                    until=self['until'], delimiter=self['delimiter']))
1490 7493ccb6 Stavros Sachtouris
            else:
1491 b4cf92b8 Stavros Sachtouris
                print('Aborted')
1492 b4cf92b8 Stavros Sachtouris
        else:
1493 24ff0a35 Stavros Sachtouris
            if self['recursive']:
1494 de73876b Stavros Sachtouris
                ask_msg = 'Delete container contents'
1495 de73876b Stavros Sachtouris
            else:
1496 de73876b Stavros Sachtouris
                ask_msg = 'Delete container'
1497 b4cf92b8 Stavros Sachtouris
            if self['yes'] or ask_user('%s %s ?' % (ask_msg, self.container)):
1498 915b99b5 Stavros Sachtouris
                self._optional_output(self.client.del_container(
1499 915b99b5 Stavros Sachtouris
                    until=self['until'], delimiter=self['delimiter']))
1500 b4cf92b8 Stavros Sachtouris
            else:
1501 b4cf92b8 Stavros Sachtouris
                print('Aborted')
1502 b4cf92b8 Stavros Sachtouris
1503 edab7ba7 Stavros Sachtouris
    def main(self, container____path__=None):
1504 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(container____path__)
1505 b4cf92b8 Stavros Sachtouris
        self._run()
1506 7493ccb6 Stavros Sachtouris
1507 234954d1 Stavros Sachtouris
1508 d486baec Stavros Sachtouris
@command(pithos_cmds)
1509 915b99b5 Stavros Sachtouris
class file_purge(_file_container_command, _optional_output_cmd):
1510 72952f4f Stavros Sachtouris
    """Delete a container and release related data blocks
1511 72952f4f Stavros Sachtouris
    Non-empty containers can not purged.
1512 72952f4f Stavros Sachtouris
    To purge a container with content:
1513 3ae60112 Stavros Sachtouris
    .   /file delete -R <container>
1514 72952f4f Stavros Sachtouris
    .      objects are deleted, but data blocks remain on server
1515 3ae60112 Stavros Sachtouris
    .   /file purge <container>
1516 72952f4f Stavros Sachtouris
    .      container and data blocks are released and deleted
1517 2fe2672e Stavros Sachtouris
    """
1518 234954d1 Stavros Sachtouris
1519 7147e1ca Stavros Sachtouris
    arguments = dict(
1520 7147e1ca Stavros Sachtouris
        yes=FlagArgument('Do not prompt for permission', '--yes'),
1521 915b99b5 Stavros Sachtouris
        force=FlagArgument('purge even if not empty', ('-F', '--force'))
1522 7147e1ca Stavros Sachtouris
    )
1523 7147e1ca Stavros Sachtouris
1524 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1525 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1526 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1527 b4cf92b8 Stavros Sachtouris
    def _run(self):
1528 b4cf92b8 Stavros Sachtouris
        if self['yes'] or ask_user('Purge container %s?' % self.container):
1529 be4a8ccd Stavros Sachtouris
            try:
1530 5655d560 Stavros Sachtouris
                r = self.client.purge_container()
1531 be4a8ccd Stavros Sachtouris
            except ClientError as ce:
1532 be4a8ccd Stavros Sachtouris
                if ce.status in (409,):
1533 be4a8ccd Stavros Sachtouris
                    if self['force']:
1534 be4a8ccd Stavros Sachtouris
                        self.client.del_container(delimiter='/')
1535 5655d560 Stavros Sachtouris
                        r = self.client.purge_container()
1536 be4a8ccd Stavros Sachtouris
                    else:
1537 be4a8ccd Stavros Sachtouris
                        raiseCLIError(ce, details=['Try -F to force-purge'])
1538 be4a8ccd Stavros Sachtouris
                else:
1539 be4a8ccd Stavros Sachtouris
                    raise
1540 915b99b5 Stavros Sachtouris
            self._optional_output(r)
1541 b4cf92b8 Stavros Sachtouris
        else:
1542 b4cf92b8 Stavros Sachtouris
            print('Aborted')
1543 b4cf92b8 Stavros Sachtouris
1544 52edad0a Stavros Sachtouris
    def main(self, container=None):
1545 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(container)
1546 52edad0a Stavros Sachtouris
        if container and self.container != container:
1547 52edad0a Stavros Sachtouris
            raiseCLIError('Invalid container name %s' % container, details=[
1548 52edad0a Stavros Sachtouris
                'Did you mean "%s" ?' % self.container,
1549 52edad0a Stavros Sachtouris
                'Use --container for names containing :'])
1550 b4cf92b8 Stavros Sachtouris
        self._run()
1551 7493ccb6 Stavros Sachtouris
1552 234954d1 Stavros Sachtouris
1553 d486baec Stavros Sachtouris
@command(pithos_cmds)
1554 3ae60112 Stavros Sachtouris
class file_publish(_file_container_command):
1555 2fe2672e Stavros Sachtouris
    """Publish the object and print the public url"""
1556 7493ccb6 Stavros Sachtouris
1557 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1558 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1559 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1560 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1561 b4cf92b8 Stavros Sachtouris
    def _run(self):
1562 fa382f9e Stavros Sachtouris
        print self.client.publish_object(self.path)
1563 b4cf92b8 Stavros Sachtouris
1564 7493ccb6 Stavros Sachtouris
    def main(self, container___path):
1565 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(
1566 201baa17 Stavros Sachtouris
            container___path, path_is_optional=False)
1567 b4cf92b8 Stavros Sachtouris
        self._run()
1568 7493ccb6 Stavros Sachtouris
1569 234954d1 Stavros Sachtouris
1570 d486baec Stavros Sachtouris
@command(pithos_cmds)
1571 915b99b5 Stavros Sachtouris
class file_unpublish(_file_container_command, _optional_output_cmd):
1572 7493ccb6 Stavros Sachtouris
    """Unpublish an object"""
1573 7493ccb6 Stavros Sachtouris
1574 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1575 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1576 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1577 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1578 b4cf92b8 Stavros Sachtouris
    def _run(self):
1579 915b99b5 Stavros Sachtouris
            self._optional_output(self.client.unpublish_object(self.path))
1580 b4cf92b8 Stavros Sachtouris
1581 7493ccb6 Stavros Sachtouris
    def main(self, container___path):
1582 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(
1583 201baa17 Stavros Sachtouris
            container___path, path_is_optional=False)
1584 b4cf92b8 Stavros Sachtouris
        self._run()
1585 7493ccb6 Stavros Sachtouris
1586 234954d1 Stavros Sachtouris
1587 d486baec Stavros Sachtouris
@command(pithos_cmds)
1588 5655d560 Stavros Sachtouris
class file_permissions(_pithos_init):
1589 5655d560 Stavros Sachtouris
    """Manage user and group accessibility for objects
1590 5655d560 Stavros Sachtouris
    Permissions are lists of users and user groups. There are read and write
1591 72952f4f Stavros Sachtouris
    permissions. Users and groups with write permission have also read
1592 72952f4f Stavros Sachtouris
    permission.
1593 72952f4f Stavros Sachtouris
    """
1594 7493ccb6 Stavros Sachtouris
1595 5655d560 Stavros Sachtouris
1596 545c6c29 Stavros Sachtouris
def print_permissions(permissions_dict):
1597 545c6c29 Stavros Sachtouris
    expected_keys = ('read', 'write')
1598 545c6c29 Stavros Sachtouris
    if set(permissions_dict).issubset(expected_keys):
1599 545c6c29 Stavros Sachtouris
        print_dict(permissions_dict)
1600 545c6c29 Stavros Sachtouris
    else:
1601 545c6c29 Stavros Sachtouris
        invalid_keys = set(permissions_dict.keys()).difference(expected_keys)
1602 545c6c29 Stavros Sachtouris
        raiseCLIError(
1603 545c6c29 Stavros Sachtouris
            'Illegal permission keys: %s' % ', '.join(invalid_keys),
1604 545c6c29 Stavros Sachtouris
            importance=1, details=[
1605 545c6c29 Stavros Sachtouris
                'Valid permission types: %s' % ' '.join(expected_keys)])
1606 545c6c29 Stavros Sachtouris
1607 545c6c29 Stavros Sachtouris
1608 5655d560 Stavros Sachtouris
@command(pithos_cmds)
1609 545c6c29 Stavros Sachtouris
class file_permissions_get(_file_container_command, _optional_json):
1610 5655d560 Stavros Sachtouris
    """Get read and write permissions of an object"""
1611 5655d560 Stavros Sachtouris
1612 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1613 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1614 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1615 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1616 b4cf92b8 Stavros Sachtouris
    def _run(self):
1617 545c6c29 Stavros Sachtouris
        self._print(
1618 545c6c29 Stavros Sachtouris
            self.client.get_object_sharing(self.path), print_permissions)
1619 b4cf92b8 Stavros Sachtouris
1620 7493ccb6 Stavros Sachtouris
    def main(self, container___path):
1621 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(
1622 201baa17 Stavros Sachtouris
            container___path, path_is_optional=False)
1623 b4cf92b8 Stavros Sachtouris
        self._run()
1624 7493ccb6 Stavros Sachtouris
1625 234954d1 Stavros Sachtouris
1626 d486baec Stavros Sachtouris
@command(pithos_cmds)
1627 915b99b5 Stavros Sachtouris
class file_permissions_set(_file_container_command, _optional_output_cmd):
1628 72952f4f Stavros Sachtouris
    """Set permissions for an object
1629 72952f4f Stavros Sachtouris
    New permissions overwrite existing permissions.
1630 72952f4f Stavros Sachtouris
    Permission format:
1631 72952f4f Stavros Sachtouris
    -   read=<username>[,usergroup[,...]]
1632 72952f4f Stavros Sachtouris
    -   write=<username>[,usegroup[,...]]
1633 72952f4f Stavros Sachtouris
    E.g. to give read permissions for file F to users A and B and write for C:
1634 5655d560 Stavros Sachtouris
    .       /file permissions set F read=A,B write=C
1635 72952f4f Stavros Sachtouris
    """
1636 7493ccb6 Stavros Sachtouris
1637 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1638 00336c85 Stavros Sachtouris
    def format_permission_dict(self, permissions):
1639 7493ccb6 Stavros Sachtouris
        read = False
1640 7493ccb6 Stavros Sachtouris
        write = False
1641 3dabe5d2 Stavros Sachtouris
        for perms in permissions:
1642 7493ccb6 Stavros Sachtouris
            splstr = perms.split('=')
1643 7493ccb6 Stavros Sachtouris
            if 'read' == splstr[0]:
1644 24ff0a35 Stavros Sachtouris
                read = [ug.strip() for ug in splstr[1].split(',')]
1645 7493ccb6 Stavros Sachtouris
            elif 'write' == splstr[0]:
1646 24ff0a35 Stavros Sachtouris
                write = [ug.strip() for ug in splstr[1].split(',')]
1647 7493ccb6 Stavros Sachtouris
            else:
1648 24ff0a35 Stavros Sachtouris
                msg = 'Usage:\tread=<groups,users> write=<groups,users>'
1649 24ff0a35 Stavros Sachtouris
                raiseCLIError(None, msg)
1650 234954d1 Stavros Sachtouris
        return (read, write)
1651 7493ccb6 Stavros Sachtouris
1652 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1653 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1654 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1655 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1656 b4cf92b8 Stavros Sachtouris
    def _run(self, read, write):
1657 915b99b5 Stavros Sachtouris
        self._optional_output(self.client.set_object_sharing(
1658 201baa17 Stavros Sachtouris
            self.path, read_permission=read, write_permission=write))
1659 b4cf92b8 Stavros Sachtouris
1660 3dabe5d2 Stavros Sachtouris
    def main(self, container___path, *permissions):
1661 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(
1662 201baa17 Stavros Sachtouris
            container___path, path_is_optional=False)
1663 201baa17 Stavros Sachtouris
        read, write = self.format_permission_dict(permissions)
1664 b4cf92b8 Stavros Sachtouris
        self._run(read, write)
1665 7493ccb6 Stavros Sachtouris
1666 234954d1 Stavros Sachtouris
1667 d486baec Stavros Sachtouris
@command(pithos_cmds)
1668 915b99b5 Stavros Sachtouris
class file_permissions_delete(_file_container_command, _optional_output_cmd):
1669 72952f4f Stavros Sachtouris
    """Delete all permissions set on object
1670 5655d560 Stavros Sachtouris
    To modify permissions, use /file permissions set
1671 72952f4f Stavros Sachtouris
    """
1672 7493ccb6 Stavros Sachtouris
1673 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1674 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1675 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1676 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1677 b4cf92b8 Stavros Sachtouris
    def _run(self):
1678 915b99b5 Stavros Sachtouris
        self._optional_output(self.client.del_object_sharing(self.path))
1679 b4cf92b8 Stavros Sachtouris
1680 7493ccb6 Stavros Sachtouris
    def main(self, container___path):
1681 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(
1682 201baa17 Stavros Sachtouris
            container___path, path_is_optional=False)
1683 b4cf92b8 Stavros Sachtouris
        self._run()
1684 7493ccb6 Stavros Sachtouris
1685 234954d1 Stavros Sachtouris
1686 d486baec Stavros Sachtouris
@command(pithos_cmds)
1687 545c6c29 Stavros Sachtouris
class file_info(_file_container_command, _optional_json):
1688 72952f4f Stavros Sachtouris
    """Get detailed information for user account, containers or objects
1689 3ae60112 Stavros Sachtouris
    to get account info:    /file info
1690 3ae60112 Stavros Sachtouris
    to get container info:  /file info <container>
1691 3ae60112 Stavros Sachtouris
    to get object info:     /file info <container>:<path>
1692 72952f4f Stavros Sachtouris
    """
1693 7493ccb6 Stavros Sachtouris
1694 2fe2672e Stavros Sachtouris
    arguments = dict(
1695 2fe2672e Stavros Sachtouris
        object_version=ValueArgument(
1696 2fe2672e Stavros Sachtouris
            'show specific version \ (applies only for objects)',
1697 545c6c29 Stavros Sachtouris
            ('-O', '--object-version'))
1698 2fe2672e Stavros Sachtouris
    )
1699 6ac7f90f Stavros Sachtouris
1700 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1701 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1702 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1703 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1704 b4cf92b8 Stavros Sachtouris
    def _run(self):
1705 b4cf92b8 Stavros Sachtouris
        if self.container is None:
1706 b4cf92b8 Stavros Sachtouris
            r = self.client.get_account_info()
1707 b4cf92b8 Stavros Sachtouris
        elif self.path is None:
1708 b4cf92b8 Stavros Sachtouris
            r = self.client.get_container_info(self.container)
1709 b4cf92b8 Stavros Sachtouris
        else:
1710 b4cf92b8 Stavros Sachtouris
            r = self.client.get_object_info(
1711 201baa17 Stavros Sachtouris
                self.path, version=self['object_version'])
1712 545c6c29 Stavros Sachtouris
        self._print(r, print_dict)
1713 b4cf92b8 Stavros Sachtouris
1714 7493ccb6 Stavros Sachtouris
    def main(self, container____path__=None):
1715 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(container____path__)
1716 b4cf92b8 Stavros Sachtouris
        self._run()
1717 7493ccb6 Stavros Sachtouris
1718 234954d1 Stavros Sachtouris
1719 d486baec Stavros Sachtouris
@command(pithos_cmds)
1720 5655d560 Stavros Sachtouris
class file_metadata(_pithos_init):
1721 5655d560 Stavros Sachtouris
    """Metadata are attached on objects. They are formed as key:value pairs.
1722 5655d560 Stavros Sachtouris
    They can have arbitary values.
1723 5655d560 Stavros Sachtouris
    """
1724 5655d560 Stavros Sachtouris
1725 5655d560 Stavros Sachtouris
1726 5655d560 Stavros Sachtouris
@command(pithos_cmds)
1727 545c6c29 Stavros Sachtouris
class file_metadata_get(_file_container_command, _optional_json):
1728 72952f4f Stavros Sachtouris
    """Get metadata for account, containers or objects"""
1729 7493ccb6 Stavros Sachtouris
1730 2fe2672e Stavros Sachtouris
    arguments = dict(
1731 f40f0cb7 Stavros Sachtouris
        detail=FlagArgument('show detailed output', ('-l', '--details')),
1732 2fe2672e Stavros Sachtouris
        until=DateArgument('show metadata until then', '--until'),
1733 2fe2672e Stavros Sachtouris
        object_version=ValueArgument(
1734 c626151a Stavros Sachtouris
            'show specific version (applies only for objects)',
1735 545c6c29 Stavros Sachtouris
            ('-O', '--object-version'))
1736 2fe2672e Stavros Sachtouris
    )
1737 7493ccb6 Stavros Sachtouris
1738 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1739 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1740 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1741 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1742 b4cf92b8 Stavros Sachtouris
    def _run(self):
1743 b4cf92b8 Stavros Sachtouris
        until = self['until']
1744 545c6c29 Stavros Sachtouris
        r = None
1745 b4cf92b8 Stavros Sachtouris
        if self.container is None:
1746 b4cf92b8 Stavros Sachtouris
            if self['detail']:
1747 b4cf92b8 Stavros Sachtouris
                r = self.client.get_account_info(until=until)
1748 7493ccb6 Stavros Sachtouris
            else:
1749 b4cf92b8 Stavros Sachtouris
                r = self.client.get_account_meta(until=until)
1750 b4cf92b8 Stavros Sachtouris
                r = pretty_keys(r, '-')
1751 b4cf92b8 Stavros Sachtouris
        elif self.path is None:
1752 b4cf92b8 Stavros Sachtouris
            if self['detail']:
1753 b4cf92b8 Stavros Sachtouris
                r = self.client.get_container_info(until=until)
1754 b4cf92b8 Stavros Sachtouris
            else:
1755 b4cf92b8 Stavros Sachtouris
                cmeta = self.client.get_container_meta(until=until)
1756 b4cf92b8 Stavros Sachtouris
                ometa = self.client.get_container_object_meta(until=until)
1757 b4cf92b8 Stavros Sachtouris
                r = {}
1758 b4cf92b8 Stavros Sachtouris
                if cmeta:
1759 b4cf92b8 Stavros Sachtouris
                    r['container-meta'] = pretty_keys(cmeta, '-')
1760 b4cf92b8 Stavros Sachtouris
                if ometa:
1761 b4cf92b8 Stavros Sachtouris
                    r['object-meta'] = pretty_keys(ometa, '-')
1762 b4cf92b8 Stavros Sachtouris
        else:
1763 b4cf92b8 Stavros Sachtouris
            if self['detail']:
1764 de73876b Stavros Sachtouris
                r = self.client.get_object_info(
1765 de73876b Stavros Sachtouris
                    self.path,
1766 b4cf92b8 Stavros Sachtouris
                    version=self['object_version'])
1767 b4cf92b8 Stavros Sachtouris
            else:
1768 de73876b Stavros Sachtouris
                r = self.client.get_object_meta(
1769 de73876b Stavros Sachtouris
                    self.path,
1770 b4cf92b8 Stavros Sachtouris
                    version=self['object_version'])
1771 b4cf92b8 Stavros Sachtouris
                r = pretty_keys(pretty_keys(r, '-'))
1772 b4cf92b8 Stavros Sachtouris
        if r:
1773 545c6c29 Stavros Sachtouris
            self._print(r, print_dict)
1774 b4cf92b8 Stavros Sachtouris
1775 b4cf92b8 Stavros Sachtouris
    def main(self, container____path__=None):
1776 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(container____path__)
1777 b4cf92b8 Stavros Sachtouris
        self._run()
1778 7493ccb6 Stavros Sachtouris
1779 234954d1 Stavros Sachtouris
1780 d486baec Stavros Sachtouris
@command(pithos_cmds)
1781 915b99b5 Stavros Sachtouris
class file_metadata_set(_file_container_command, _optional_output_cmd):
1782 5655d560 Stavros Sachtouris
    """Set a piece of metadata for account, container or object"""
1783 7493ccb6 Stavros Sachtouris
1784 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1785 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1786 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1787 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1788 b4cf92b8 Stavros Sachtouris
    def _run(self, metakey, metaval):
1789 b4cf92b8 Stavros Sachtouris
        if not self.container:
1790 915b99b5 Stavros Sachtouris
            r = self.client.set_account_meta({metakey: metaval})
1791 b4cf92b8 Stavros Sachtouris
        elif not self.path:
1792 915b99b5 Stavros Sachtouris
            r = self.client.set_container_meta({metakey: metaval})
1793 b4cf92b8 Stavros Sachtouris
        else:
1794 915b99b5 Stavros Sachtouris
            r = self.client.set_object_meta(self.path, {metakey: metaval})
1795 915b99b5 Stavros Sachtouris
        self._optional_output(r)
1796 b4cf92b8 Stavros Sachtouris
1797 b4cf92b8 Stavros Sachtouris
    def main(self, metakey, metaval, container____path__=None):
1798 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(container____path__)
1799 b4cf92b8 Stavros Sachtouris
        self._run(metakey=metakey, metaval=metaval)
1800 7493ccb6 Stavros Sachtouris
1801 234954d1 Stavros Sachtouris
1802 d486baec Stavros Sachtouris
@command(pithos_cmds)
1803 915b99b5 Stavros Sachtouris
class file_metadata_delete(_file_container_command, _optional_output_cmd):
1804 72952f4f Stavros Sachtouris
    """Delete metadata with given key from account, container or object
1805 5655d560 Stavros Sachtouris
    - to get metadata of current account: /file metadata get
1806 5655d560 Stavros Sachtouris
    - to get metadata of a container:     /file metadata get <container>
1807 5655d560 Stavros Sachtouris
    - to get metadata of an object:       /file metadata get <container>:<path>
1808 72952f4f Stavros Sachtouris
    """
1809 7493ccb6 Stavros Sachtouris
1810 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1811 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1812 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1813 b4cf92b8 Stavros Sachtouris
    @errors.pithos.object_path
1814 b4cf92b8 Stavros Sachtouris
    def _run(self, metakey):
1815 b4cf92b8 Stavros Sachtouris
        if self.container is None:
1816 915b99b5 Stavros Sachtouris
            r = self.client.del_account_meta(metakey)
1817 b4cf92b8 Stavros Sachtouris
        elif self.path is None:
1818 915b99b5 Stavros Sachtouris
            r = self.client.del_container_meta(metakey)
1819 b4cf92b8 Stavros Sachtouris
        else:
1820 915b99b5 Stavros Sachtouris
            r = self.client.del_object_meta(self.path, metakey)
1821 915b99b5 Stavros Sachtouris
        self._optional_output(r)
1822 b4cf92b8 Stavros Sachtouris
1823 7493ccb6 Stavros Sachtouris
    def main(self, metakey, container____path__=None):
1824 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run(container____path__)
1825 b4cf92b8 Stavros Sachtouris
        self._run(metakey)
1826 7493ccb6 Stavros Sachtouris
1827 234954d1 Stavros Sachtouris
1828 d486baec Stavros Sachtouris
@command(pithos_cmds)
1829 545c6c29 Stavros Sachtouris
class file_quota(_file_account_command, _optional_json):
1830 3ed6dbde Stavros Sachtouris
    """Get account quota"""
1831 001200c3 Stavros Sachtouris
1832 001200c3 Stavros Sachtouris
    arguments = dict(
1833 001200c3 Stavros Sachtouris
        in_bytes=FlagArgument('Show result in bytes', ('-b', '--bytes'))
1834 de73876b Stavros Sachtouris
    )
1835 7493ccb6 Stavros Sachtouris
1836 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1837 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1838 b4cf92b8 Stavros Sachtouris
    def _run(self):
1839 545c6c29 Stavros Sachtouris
1840 545c6c29 Stavros Sachtouris
        def pretty_print(output):
1841 545c6c29 Stavros Sachtouris
            if not self['in_bytes']:
1842 545c6c29 Stavros Sachtouris
                for k in output:
1843 545c6c29 Stavros Sachtouris
                    output[k] = format_size(output[k])
1844 545c6c29 Stavros Sachtouris
            pretty_dict(output, '-')
1845 545c6c29 Stavros Sachtouris
1846 545c6c29 Stavros Sachtouris
        self._print(self.client.get_account_quota(), pretty_print)
1847 7493ccb6 Stavros Sachtouris
1848 3ed6dbde Stavros Sachtouris
    def main(self, custom_uuid=None):
1849 3ed6dbde Stavros Sachtouris
        super(self.__class__, self)._run(custom_account=custom_uuid)
1850 b4cf92b8 Stavros Sachtouris
        self._run()
1851 b4cf92b8 Stavros Sachtouris
1852 234954d1 Stavros Sachtouris
1853 d486baec Stavros Sachtouris
@command(pithos_cmds)
1854 326a79b9 Stavros Sachtouris
class file_containerlimit(_pithos_init):
1855 326a79b9 Stavros Sachtouris
    """Container size limit commands"""
1856 326a79b9 Stavros Sachtouris
1857 326a79b9 Stavros Sachtouris
1858 326a79b9 Stavros Sachtouris
@command(pithos_cmds)
1859 545c6c29 Stavros Sachtouris
class file_containerlimit_get(_file_container_command, _optional_json):
1860 3ed6dbde Stavros Sachtouris
    """Get container size limit"""
1861 3ed6dbde Stavros Sachtouris
1862 3ed6dbde Stavros Sachtouris
    arguments = dict(
1863 3ed6dbde Stavros Sachtouris
        in_bytes=FlagArgument('Show result in bytes', ('-b', '--bytes'))
1864 3ed6dbde Stavros Sachtouris
    )
1865 3ed6dbde Stavros Sachtouris
1866 3ed6dbde Stavros Sachtouris
    @errors.generic.all
1867 3ed6dbde Stavros Sachtouris
    @errors.pithos.container
1868 3ed6dbde Stavros Sachtouris
    def _run(self):
1869 545c6c29 Stavros Sachtouris
1870 545c6c29 Stavros Sachtouris
        def pretty_print(output):
1871 545c6c29 Stavros Sachtouris
            if not self['in_bytes']:
1872 545c6c29 Stavros Sachtouris
                for k, v in output.items():
1873 545c6c29 Stavros Sachtouris
                    output[k] = 'unlimited' if '0' == v else format_size(v)
1874 545c6c29 Stavros Sachtouris
            pretty_dict(output, '-')
1875 545c6c29 Stavros Sachtouris
1876 545c6c29 Stavros Sachtouris
        self._print(
1877 545c6c29 Stavros Sachtouris
            self.client.get_container_limit(self.container), pretty_print)
1878 3ed6dbde Stavros Sachtouris
1879 3ed6dbde Stavros Sachtouris
    def main(self, container=None):
1880 3ed6dbde Stavros Sachtouris
        super(self.__class__, self)._run()
1881 3ed6dbde Stavros Sachtouris
        self.container = container
1882 3ed6dbde Stavros Sachtouris
        self._run()
1883 3ed6dbde Stavros Sachtouris
1884 3ed6dbde Stavros Sachtouris
1885 3ed6dbde Stavros Sachtouris
@command(pithos_cmds)
1886 545c6c29 Stavros Sachtouris
class file_containerlimit_set(_file_account_command, _optional_output_cmd):
1887 326a79b9 Stavros Sachtouris
    """Set new storage limit for a container
1888 326a79b9 Stavros Sachtouris
    By default, the limit is set in bytes
1889 001200c3 Stavros Sachtouris
    Users may specify a different unit, e.g:
1890 326a79b9 Stavros Sachtouris
    /file containerlimit set 2.3GB mycontainer
1891 3ed6dbde Stavros Sachtouris
    Valid units: B, KiB (1024 B), KB (1000 B), MiB, MB, GiB, GB, TiB, TB
1892 9f783a51 Stavros Sachtouris
    To set container limit to "unlimited", use 0
1893 001200c3 Stavros Sachtouris
    """
1894 001200c3 Stavros Sachtouris
1895 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1896 326a79b9 Stavros Sachtouris
    def _calculate_limit(self, user_input):
1897 326a79b9 Stavros Sachtouris
        limit = 0
1898 001200c3 Stavros Sachtouris
        try:
1899 326a79b9 Stavros Sachtouris
            limit = int(user_input)
1900 001200c3 Stavros Sachtouris
        except ValueError:
1901 001200c3 Stavros Sachtouris
            index = 0
1902 001200c3 Stavros Sachtouris
            digits = [str(num) for num in range(0, 10)] + ['.']
1903 001200c3 Stavros Sachtouris
            while user_input[index] in digits:
1904 001200c3 Stavros Sachtouris
                index += 1
1905 326a79b9 Stavros Sachtouris
            limit = user_input[:index]
1906 001200c3 Stavros Sachtouris
            format = user_input[index:]
1907 001200c3 Stavros Sachtouris
            try:
1908 326a79b9 Stavros Sachtouris
                return to_bytes(limit, format)
1909 001200c3 Stavros Sachtouris
            except Exception as qe:
1910 de73876b Stavros Sachtouris
                msg = 'Failed to convert %s to bytes' % user_input,
1911 de73876b Stavros Sachtouris
                raiseCLIError(qe, msg, details=[
1912 326a79b9 Stavros Sachtouris
                    'Syntax: containerlimit set <limit>[format] [container]',
1913 326a79b9 Stavros Sachtouris
                    'e.g.: containerlimit set 2.3GB mycontainer',
1914 326a79b9 Stavros Sachtouris
                    'Valid formats:',
1915 de73876b Stavros Sachtouris
                    '(*1024): B, KiB, MiB, GiB, TiB',
1916 de73876b Stavros Sachtouris
                    '(*1000): B, KB, MB, GB, TB'])
1917 326a79b9 Stavros Sachtouris
        return limit
1918 7493ccb6 Stavros Sachtouris
1919 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1920 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1921 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1922 326a79b9 Stavros Sachtouris
    def _run(self, limit):
1923 b4cf92b8 Stavros Sachtouris
        if self.container:
1924 b4cf92b8 Stavros Sachtouris
            self.client.container = self.container
1925 915b99b5 Stavros Sachtouris
        self._optional_output(self.client.set_container_limit(limit))
1926 b4cf92b8 Stavros Sachtouris
1927 326a79b9 Stavros Sachtouris
    def main(self, limit, container=None):
1928 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run()
1929 326a79b9 Stavros Sachtouris
        limit = self._calculate_limit(limit)
1930 b4cf92b8 Stavros Sachtouris
        self.container = container
1931 326a79b9 Stavros Sachtouris
        self._run(limit)
1932 7493ccb6 Stavros Sachtouris
1933 234954d1 Stavros Sachtouris
1934 d486baec Stavros Sachtouris
@command(pithos_cmds)
1935 915b99b5 Stavros Sachtouris
class file_versioning(_pithos_init):
1936 915b99b5 Stavros Sachtouris
    """Manage the versioning scheme of current pithos user account"""
1937 915b99b5 Stavros Sachtouris
1938 915b99b5 Stavros Sachtouris
1939 915b99b5 Stavros Sachtouris
@command(pithos_cmds)
1940 545c6c29 Stavros Sachtouris
class file_versioning_get(_file_account_command, _optional_json):
1941 776eee69 Stavros Sachtouris
    """Get  versioning for account or container"""
1942 7493ccb6 Stavros Sachtouris
1943 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1944 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1945 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1946 b4cf92b8 Stavros Sachtouris
    def _run(self):
1947 545c6c29 Stavros Sachtouris
        self._print(
1948 df0045d8 Stavros Sachtouris
            self.client.get_container_versioning(self.container), print_dict)
1949 b4cf92b8 Stavros Sachtouris
1950 df0045d8 Stavros Sachtouris
    def main(self, container):
1951 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run()
1952 b4cf92b8 Stavros Sachtouris
        self.container = container
1953 b4cf92b8 Stavros Sachtouris
        self._run()
1954 7493ccb6 Stavros Sachtouris
1955 234954d1 Stavros Sachtouris
1956 d486baec Stavros Sachtouris
@command(pithos_cmds)
1957 915b99b5 Stavros Sachtouris
class file_versioning_set(_file_account_command, _optional_output_cmd):
1958 776eee69 Stavros Sachtouris
    """Set versioning mode (auto, none) for account or container"""
1959 7493ccb6 Stavros Sachtouris
1960 b4cf92b8 Stavros Sachtouris
    def _check_versioning(self, versioning):
1961 b4cf92b8 Stavros Sachtouris
        if versioning and versioning.lower() in ('auto', 'none'):
1962 b4cf92b8 Stavros Sachtouris
            return versioning.lower()
1963 b4cf92b8 Stavros Sachtouris
        raiseCLIError('Invalid versioning %s' % versioning, details=[
1964 b4cf92b8 Stavros Sachtouris
            'Versioning can be auto or none'])
1965 b4cf92b8 Stavros Sachtouris
1966 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1967 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1968 b4cf92b8 Stavros Sachtouris
    @errors.pithos.container
1969 b4cf92b8 Stavros Sachtouris
    def _run(self, versioning):
1970 df0045d8 Stavros Sachtouris
        self.client.container = self.container
1971 df0045d8 Stavros Sachtouris
        r = self.client.set_container_versioning(versioning)
1972 915b99b5 Stavros Sachtouris
        self._optional_output(r)
1973 b4cf92b8 Stavros Sachtouris
1974 df0045d8 Stavros Sachtouris
    def main(self, versioning, container):
1975 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run()
1976 b4cf92b8 Stavros Sachtouris
        self._run(self._check_versioning(versioning))
1977 7493ccb6 Stavros Sachtouris
1978 234954d1 Stavros Sachtouris
1979 d486baec Stavros Sachtouris
@command(pithos_cmds)
1980 915b99b5 Stavros Sachtouris
class file_group(_pithos_init):
1981 915b99b5 Stavros Sachtouris
    """Manage access groups and group members"""
1982 915b99b5 Stavros Sachtouris
1983 915b99b5 Stavros Sachtouris
1984 915b99b5 Stavros Sachtouris
@command(pithos_cmds)
1985 545c6c29 Stavros Sachtouris
class file_group_list(_file_account_command, _optional_json):
1986 545c6c29 Stavros Sachtouris
    """list all groups and group members"""
1987 7493ccb6 Stavros Sachtouris
1988 b4cf92b8 Stavros Sachtouris
    @errors.generic.all
1989 b4cf92b8 Stavros Sachtouris
    @errors.pithos.connection
1990 b4cf92b8 Stavros Sachtouris
    def _run(self):
1991 545c6c29 Stavros Sachtouris
        self._print(self.client.get_account_group(), pretty_dict, delim='-')
1992 b4cf92b8 Stavros Sachtouris
1993 7493ccb6 Stavros Sachtouris
    def main(self):
1994 b4cf92b8 Stavros Sachtouris
        super(self.__class__, self)._run()
1995 b4cf92b8 Stavros Sachtouris
        self._run()
1996 7493ccb6 Stavros Sachtouris
1997 234954d1 Stavros Sachtouris
1998 d486baec Stavros Sachtouris
@command(pithos_cmds)
1999 915b99b5 Stavros Sachtouris
class file_group_set(_file_account_command, _optional_output_cmd):
2000 4fcc38a2 Stavros Sachtouris
    """Set a user group"""
2001 7493ccb6 Stavros Sachtouris
2002