root / kamaki / cli / commands / pithos.py @ 7806f19d
History | View | Annotate | Download (78.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 | f8426b5c | Stavros Sachtouris | page_hold, bold, ask_user, get_path_size, print_json, guess_mime_type) |
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 | 7806f19d | Stavros Sachtouris | start, sep, end = newvalue.partition('-')
|
139 | 7806f19d | Stavros Sachtouris | if sep:
|
140 | 7806f19d | Stavros Sachtouris | if start:
|
141 | 7806f19d | Stavros Sachtouris | self._value = '%s-%s' % (int(start), int(end)) |
142 | 7806f19d | Stavros Sachtouris | else:
|
143 | 7806f19d | Stavros Sachtouris | self._value = '-%s' % int(end) |
144 | 7806f19d | Stavros Sachtouris | else:
|
145 | 7806f19d | Stavros Sachtouris | self._value = '%s' % int(start) |
146 | 234954d1 | Stavros Sachtouris | |
147 | 201baa17 | Stavros Sachtouris | |
148 | 234954d1 | Stavros Sachtouris | # Command specs
|
149 | 234954d1 | Stavros Sachtouris | |
150 | e3d4d442 | Stavros Sachtouris | |
151 | 5eae854d | Stavros Sachtouris | class _pithos_init(_command_init): |
152 | befed235 | Stavros Sachtouris | """Initialize a pithos+ kamaki client"""
|
153 | befed235 | Stavros Sachtouris | |
154 | ece4ae4b | Stavros Sachtouris | @staticmethod
|
155 | ece4ae4b | Stavros Sachtouris | def _is_dir(remote_dict): |
156 | ece4ae4b | Stavros Sachtouris | return 'application/directory' == remote_dict.get( |
157 | 8cec3671 | Stavros Sachtouris | 'content_type', remote_dict.get('content-type', '')) |
158 | ece4ae4b | Stavros Sachtouris | |
159 | b4f69041 | Stavros Sachtouris | @DontRaiseKeyError
|
160 | b4f69041 | Stavros Sachtouris | def _custom_container(self): |
161 | 144b3551 | Stavros Sachtouris | return self.config.get_cloud(self.cloud, 'pithos_container') |
162 | b4f69041 | Stavros Sachtouris | |
163 | b4f69041 | Stavros Sachtouris | @DontRaiseKeyError
|
164 | b4f69041 | Stavros Sachtouris | def _custom_uuid(self): |
165 | 144b3551 | Stavros Sachtouris | return self.config.get_cloud(self.cloud, 'pithos_uuid') |
166 | b4f69041 | Stavros Sachtouris | |
167 | b4f69041 | Stavros Sachtouris | def _set_account(self): |
168 | b4f69041 | Stavros Sachtouris | self.account = self._custom_uuid() |
169 | b4f69041 | Stavros Sachtouris | if self.account: |
170 | b4f69041 | Stavros Sachtouris | return
|
171 | b4f69041 | Stavros Sachtouris | if getattr(self, 'auth_base', False): |
172 | b4f69041 | Stavros Sachtouris | self.account = self.auth_base.user_term('id', self.token) |
173 | b4f69041 | Stavros Sachtouris | else:
|
174 | b4f69041 | Stavros Sachtouris | astakos_url = self._custom_url('astakos') |
175 | b4f69041 | Stavros Sachtouris | astakos_token = self._custom_token('astakos') or self.token |
176 | b4f69041 | Stavros Sachtouris | if not astakos_url: |
177 | b4f69041 | Stavros Sachtouris | raise CLIBaseUrlError(service='astakos') |
178 | b4f69041 | Stavros Sachtouris | astakos = AstakosClient(astakos_url, astakos_token) |
179 | b4f69041 | Stavros Sachtouris | self.account = astakos.user_term('id') |
180 | b4f69041 | Stavros Sachtouris | |
181 | 1395c40e | Stavros Sachtouris | @errors.generic.all
|
182 | b4f69041 | Stavros Sachtouris | @addLogSettings
|
183 | 1395c40e | Stavros Sachtouris | def _run(self): |
184 | b4f69041 | Stavros Sachtouris | self.base_url = None |
185 | b4f69041 | Stavros Sachtouris | if getattr(self, 'cloud', None): |
186 | b4f69041 | Stavros Sachtouris | self.base_url = self._custom_url('pithos') |
187 | b4f69041 | Stavros Sachtouris | else:
|
188 | b4f69041 | Stavros Sachtouris | self.cloud = 'default' |
189 | b4f69041 | Stavros Sachtouris | self.token = self._custom_token('pithos') |
190 | b4f69041 | Stavros Sachtouris | self.container = self._custom_container() |
191 | 8cec3671 | Stavros Sachtouris | |
192 | 8cec3671 | Stavros Sachtouris | if getattr(self, 'auth_base', False): |
193 | b4f69041 | Stavros Sachtouris | self.token = self.token or self.auth_base.token |
194 | b4f69041 | Stavros Sachtouris | if not self.base_url: |
195 | b4f69041 | Stavros Sachtouris | pithos_endpoints = self.auth_base.get_service_endpoints(
|
196 | b4f69041 | Stavros Sachtouris | self._custom_type('pithos') or 'object-store', |
197 | b4f69041 | Stavros Sachtouris | self._custom_version('pithos') or '') |
198 | b4f69041 | Stavros Sachtouris | self.base_url = pithos_endpoints['publicURL'] |
199 | b4f69041 | Stavros Sachtouris | elif not self.base_url: |
200 | 8cec3671 | Stavros Sachtouris | raise CLIBaseUrlError(service='pithos') |
201 | 8cec3671 | Stavros Sachtouris | |
202 | 1f5debf7 | Stavros Sachtouris | self._set_account()
|
203 | de73876b | Stavros Sachtouris | self.client = PithosClient(
|
204 | de73876b | Stavros Sachtouris | base_url=self.base_url,
|
205 | 234954d1 | Stavros Sachtouris | token=self.token,
|
206 | 234954d1 | Stavros Sachtouris | account=self.account,
|
207 | 7493ccb6 | Stavros Sachtouris | container=self.container)
|
208 | 7493ccb6 | Stavros Sachtouris | |
209 | 1395c40e | Stavros Sachtouris | def main(self): |
210 | 1395c40e | Stavros Sachtouris | self._run()
|
211 | 1395c40e | Stavros Sachtouris | |
212 | 234954d1 | Stavros Sachtouris | |
213 | 3ae60112 | Stavros Sachtouris | class _file_account_command(_pithos_init): |
214 | 7493ccb6 | Stavros Sachtouris | """Base class for account level storage commands"""
|
215 | 7493ccb6 | Stavros Sachtouris | |
216 | b4f69041 | Stavros Sachtouris | def __init__(self, arguments={}, auth_base=None, cloud=None): |
217 | b4f69041 | Stavros Sachtouris | super(_file_account_command, self).__init__( |
218 | b4f69041 | Stavros Sachtouris | arguments, auth_base, cloud) |
219 | 439826ec | Stavros Sachtouris | self['account'] = ValueArgument( |
220 | 201baa17 | Stavros Sachtouris | 'Set user account (not permanent)', ('-A', '--account')) |
221 | 7493ccb6 | Stavros Sachtouris | |
222 | 3ed6dbde | Stavros Sachtouris | def _run(self, custom_account=None): |
223 | 3ae60112 | Stavros Sachtouris | super(_file_account_command, self)._run() |
224 | 3ed6dbde | Stavros Sachtouris | if custom_account:
|
225 | 3ed6dbde | Stavros Sachtouris | self.client.account = custom_account
|
226 | 3ed6dbde | Stavros Sachtouris | elif self['account']: |
227 | 47ae7577 | Stavros Sachtouris | self.client.account = self['account'] |
228 | 7493ccb6 | Stavros Sachtouris | |
229 | 1395c40e | Stavros Sachtouris | @errors.generic.all
|
230 | 1395c40e | Stavros Sachtouris | def main(self): |
231 | 1395c40e | Stavros Sachtouris | self._run()
|
232 | 1395c40e | Stavros Sachtouris | |
233 | 234954d1 | Stavros Sachtouris | |
234 | 3ae60112 | Stavros Sachtouris | class _file_container_command(_file_account_command): |
235 | 7493ccb6 | Stavros Sachtouris | """Base class for container level storage commands"""
|
236 | 7493ccb6 | Stavros Sachtouris | |
237 | 47ae7577 | Stavros Sachtouris | container = None
|
238 | 47ae7577 | Stavros Sachtouris | path = None
|
239 | 2d7ce81e | Stavros Sachtouris | |
240 | b4f69041 | Stavros Sachtouris | def __init__(self, arguments={}, auth_base=None, cloud=None): |
241 | b4f69041 | Stavros Sachtouris | super(_file_container_command, self).__init__( |
242 | b4f69041 | Stavros Sachtouris | arguments, auth_base, cloud) |
243 | 439826ec | Stavros Sachtouris | self['container'] = ValueArgument( |
244 | 201baa17 | Stavros Sachtouris | 'Set container to work with (temporary)', ('-C', '--container')) |
245 | 7493ccb6 | Stavros Sachtouris | |
246 | de73876b | Stavros Sachtouris | def extract_container_and_path( |
247 | 24ff0a35 | Stavros Sachtouris | self,
|
248 | 24ff0a35 | Stavros Sachtouris | container_with_path, |
249 | 24ff0a35 | Stavros Sachtouris | path_is_optional=True):
|
250 | 1395c40e | Stavros Sachtouris | """Contains all heuristics for deciding what should be used as
|
251 | 1395c40e | Stavros Sachtouris | container or path. Options are:
|
252 | 1395c40e | Stavros Sachtouris | * user string of the form container:path
|
253 | 1395c40e | Stavros Sachtouris | * self.container, self.path variables set by super constructor, or
|
254 | 1395c40e | Stavros Sachtouris | explicitly by the caller application
|
255 | 1395c40e | Stavros Sachtouris | Error handling is explicit as these error cases happen only here
|
256 | 1395c40e | Stavros Sachtouris | """
|
257 | 83ba5545 | Stavros Sachtouris | try:
|
258 | 83ba5545 | Stavros Sachtouris | assert isinstance(container_with_path, str) |
259 | 83ba5545 | Stavros Sachtouris | except AssertionError as err: |
260 | edab7ba7 | Stavros Sachtouris | if self['container'] and path_is_optional: |
261 | edab7ba7 | Stavros Sachtouris | self.container = self['container'] |
262 | edab7ba7 | Stavros Sachtouris | self.client.container = self['container'] |
263 | edab7ba7 | Stavros Sachtouris | return
|
264 | 83ba5545 | Stavros Sachtouris | raiseCLIError(err) |
265 | 447c9568 | Stavros Sachtouris | |
266 | 1395c40e | Stavros Sachtouris | user_cont, sep, userpath = container_with_path.partition(':')
|
267 | 447c9568 | Stavros Sachtouris | |
268 | 447c9568 | Stavros Sachtouris | if sep:
|
269 | 1395c40e | Stavros Sachtouris | if not user_cont: |
270 | 2005b18e | Stavros Sachtouris | raiseCLIError(CLISyntaxError( |
271 | 2005b18e | Stavros Sachtouris | 'Container is missing\n',
|
272 | 1395c40e | Stavros Sachtouris | details=errors.pithos.container_howto)) |
273 | 47ae7577 | Stavros Sachtouris | alt_cont = self['container'] |
274 | 1395c40e | Stavros Sachtouris | if alt_cont and user_cont != alt_cont: |
275 | c1558584 | Stavros Sachtouris | raiseCLIError(CLISyntaxError( |
276 | 1395c40e | Stavros Sachtouris | 'Conflict: 2 containers (%s, %s)' % (user_cont, alt_cont),
|
277 | 1395c40e | Stavros Sachtouris | details=errors.pithos.container_howto) |
278 | 47ae7577 | Stavros Sachtouris | ) |
279 | 1395c40e | Stavros Sachtouris | self.container = user_cont
|
280 | 1395c40e | Stavros Sachtouris | if not userpath: |
281 | c1558584 | Stavros Sachtouris | raiseCLIError(CLISyntaxError( |
282 | 1395c40e | Stavros Sachtouris | 'Path is missing for object in container %s' % user_cont,
|
283 | 1395c40e | Stavros Sachtouris | details=errors.pithos.container_howto) |
284 | 47ae7577 | Stavros Sachtouris | ) |
285 | 1395c40e | Stavros Sachtouris | self.path = userpath
|
286 | 447c9568 | Stavros Sachtouris | else:
|
287 | 47ae7577 | Stavros Sachtouris | alt_cont = self['container'] or self.client.container |
288 | 447c9568 | Stavros Sachtouris | if alt_cont:
|
289 | 447c9568 | Stavros Sachtouris | self.container = alt_cont
|
290 | 1395c40e | Stavros Sachtouris | self.path = user_cont
|
291 | 447c9568 | Stavros Sachtouris | elif path_is_optional:
|
292 | 1395c40e | Stavros Sachtouris | self.container = user_cont
|
293 | 7493ccb6 | Stavros Sachtouris | self.path = None |
294 | 7493ccb6 | Stavros Sachtouris | else:
|
295 | 1395c40e | Stavros Sachtouris | self.container = user_cont
|
296 | 447c9568 | Stavros Sachtouris | raiseCLIError(CLISyntaxError( |
297 | c1558584 | Stavros Sachtouris | 'Both container and path are required',
|
298 | 1395c40e | Stavros Sachtouris | details=errors.pithos.container_howto) |
299 | 47ae7577 | Stavros Sachtouris | ) |
300 | 7493ccb6 | Stavros Sachtouris | |
301 | 1395c40e | Stavros Sachtouris | @errors.generic.all
|
302 | 1395c40e | Stavros Sachtouris | def _run(self, container_with_path=None, path_is_optional=True): |
303 | 3ae60112 | Stavros Sachtouris | super(_file_container_command, self)._run() |
304 | edab7ba7 | Stavros Sachtouris | if self['container']: |
305 | edab7ba7 | Stavros Sachtouris | self.client.container = self['container'] |
306 | edab7ba7 | Stavros Sachtouris | if container_with_path:
|
307 | edab7ba7 | Stavros Sachtouris | self.path = container_with_path
|
308 | edab7ba7 | Stavros Sachtouris | elif not path_is_optional: |
309 | edab7ba7 | Stavros Sachtouris | raise CLISyntaxError(
|
310 | edab7ba7 | Stavros Sachtouris | 'Both container and path are required',
|
311 | edab7ba7 | Stavros Sachtouris | details=errors.pithos.container_howto) |
312 | edab7ba7 | Stavros Sachtouris | elif container_with_path:
|
313 | 47ae7577 | Stavros Sachtouris | self.extract_container_and_path(
|
314 | 47ae7577 | Stavros Sachtouris | container_with_path, |
315 | 234954d1 | Stavros Sachtouris | path_is_optional) |
316 | 7493ccb6 | Stavros Sachtouris | self.client.container = self.container |
317 | 7493ccb6 | Stavros Sachtouris | self.container = self.client.container |
318 | 7493ccb6 | Stavros Sachtouris | |
319 | 1395c40e | Stavros Sachtouris | def main(self, container_with_path=None, path_is_optional=True): |
320 | 1395c40e | Stavros Sachtouris | self._run(container_with_path, path_is_optional)
|
321 | 1395c40e | Stavros Sachtouris | |
322 | 7493ccb6 | Stavros Sachtouris | |
323 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
324 | 545c6c29 | Stavros Sachtouris | class file_list(_file_container_command, _optional_json): |
325 | 7493ccb6 | Stavros Sachtouris | """List containers, object trees or objects in a directory
|
326 | 2703cceb | Stavros Sachtouris | Use with:
|
327 | 7ae842c2 | Stavros Sachtouris | 1 no parameters : containers in current account
|
328 | 2703cceb | Stavros Sachtouris | 2. one parameter (container) or --container : contents of container
|
329 | 2703cceb | Stavros Sachtouris | 3. <container>:<prefix> or --container=<container> <prefix>: objects in
|
330 | 439826ec | Stavros Sachtouris | . container starting with prefix
|
331 | 7493ccb6 | Stavros Sachtouris | """
|
332 | 7493ccb6 | Stavros Sachtouris | |
333 | 47ae7577 | Stavros Sachtouris | arguments = dict(
|
334 | f40f0cb7 | Stavros Sachtouris | detail=FlagArgument('detailed output', ('-l', '--list')), |
335 | f40f0cb7 | Stavros Sachtouris | limit=IntArgument('limit number of listed items', ('-n', '--number')), |
336 | f40f0cb7 | Stavros Sachtouris | marker=ValueArgument('output greater that marker', '--marker'), |
337 | f40f0cb7 | Stavros Sachtouris | prefix=ValueArgument('output starting with prefix', '--prefix'), |
338 | 47ae7577 | Stavros Sachtouris | delimiter=ValueArgument('show output up to delimiter', '--delimiter'), |
339 | 47ae7577 | Stavros Sachtouris | path=ValueArgument( |
340 | 201baa17 | Stavros Sachtouris | 'show output starting with prefix up to /', '--path'), |
341 | 47ae7577 | Stavros Sachtouris | meta=ValueArgument( |
342 | 201baa17 | Stavros Sachtouris | 'show output with specified meta keys', '--meta', |
343 | 47ae7577 | Stavros Sachtouris | default=[]), |
344 | 47ae7577 | Stavros Sachtouris | if_modified_since=ValueArgument( |
345 | 201baa17 | Stavros Sachtouris | 'show output modified since then', '--if-modified-since'), |
346 | 47ae7577 | Stavros Sachtouris | if_unmodified_since=ValueArgument( |
347 | 201baa17 | Stavros Sachtouris | 'show output not modified since then', '--if-unmodified-since'), |
348 | 47ae7577 | Stavros Sachtouris | until=DateArgument('show metadata until then', '--until'), |
349 | 47ae7577 | Stavros Sachtouris | format=ValueArgument( |
350 | 201baa17 | Stavros Sachtouris | 'format to parse until data (default: d/m/Y H:M:S )', '--format'), |
351 | 47ae7577 | Stavros Sachtouris | shared=FlagArgument('show only shared', '--shared'), |
352 | 439826ec | Stavros Sachtouris | more=FlagArgument( |
353 | 439826ec | Stavros Sachtouris | 'output results in pages (-n to set items per page, default 10)',
|
354 | eb18b8a7 | Stavros Sachtouris | '--more'),
|
355 | eb18b8a7 | Stavros Sachtouris | exact_match=FlagArgument( |
356 | eb18b8a7 | Stavros Sachtouris | 'Show only objects that match exactly with path',
|
357 | ed9af02c | Stavros Sachtouris | '--exact-match'),
|
358 | 545c6c29 | Stavros Sachtouris | enum=FlagArgument('Enumerate results', '--enumerate') |
359 | 47ae7577 | Stavros Sachtouris | ) |
360 | c41a86b2 | Stavros Sachtouris | |
361 | 7493ccb6 | Stavros Sachtouris | def print_objects(self, object_list): |
362 | 0399ac7e | Stavros Sachtouris | if self['json_output']: |
363 | 0399ac7e | Stavros Sachtouris | print_json(object_list) |
364 | 0399ac7e | Stavros Sachtouris | return
|
365 | 439826ec | Stavros Sachtouris | limit = int(self['limit']) if self['limit'] > 0 else len(object_list) |
366 | 234954d1 | Stavros Sachtouris | for index, obj in enumerate(object_list): |
367 | 24ff0a35 | Stavros Sachtouris | if self['exact_match'] and self.path and not ( |
368 | 2005b18e | Stavros Sachtouris | obj['name'] == self.path or 'content_type' in obj): |
369 | 2005b18e | Stavros Sachtouris | continue
|
370 | 7493ccb6 | Stavros Sachtouris | pretty_obj = obj.copy() |
371 | 7493ccb6 | Stavros Sachtouris | index += 1
|
372 | 234954d1 | Stavros Sachtouris | empty_space = ' ' * (len(str(len(object_list))) - len(str(index))) |
373 | a339a3ee | Stavros Sachtouris | if 'subdir' in obj: |
374 | a339a3ee | Stavros Sachtouris | continue
|
375 | 7493ccb6 | Stavros Sachtouris | if obj['content_type'] == 'application/directory': |
376 | 7493ccb6 | Stavros Sachtouris | isDir = True
|
377 | 7493ccb6 | Stavros Sachtouris | size = 'D'
|
378 | 7493ccb6 | Stavros Sachtouris | else:
|
379 | 7493ccb6 | Stavros Sachtouris | isDir = False
|
380 | 7493ccb6 | Stavros Sachtouris | size = format_size(obj['bytes'])
|
381 | 234954d1 | Stavros Sachtouris | pretty_obj['bytes'] = '%s (%s)' % (obj['bytes'], size) |
382 | 7493ccb6 | Stavros Sachtouris | oname = bold(obj['name'])
|
383 | ed9af02c | Stavros Sachtouris | prfx = ('%s%s. ' % (empty_space, index)) if self['enum'] else '' |
384 | 47ae7577 | Stavros Sachtouris | if self['detail']: |
385 | ed9af02c | Stavros Sachtouris | print('%s%s' % (prfx, oname))
|
386 | 7493ccb6 | Stavros Sachtouris | print_dict(pretty_keys(pretty_obj), exclude=('name'))
|
387 | 7493ccb6 | Stavros Sachtouris | print
|
388 | 7493ccb6 | Stavros Sachtouris | else:
|
389 | ed9af02c | Stavros Sachtouris | oname = '%s%9s %s' % (prfx, size, oname)
|
390 | 7493ccb6 | Stavros Sachtouris | oname += '/' if isDir else '' |
391 | 7493ccb6 | Stavros Sachtouris | print(oname) |
392 | 439826ec | Stavros Sachtouris | if self['more']: |
393 | 439826ec | Stavros Sachtouris | page_hold(index, limit, len(object_list))
|
394 | 7493ccb6 | Stavros Sachtouris | |
395 | 7493ccb6 | Stavros Sachtouris | def print_containers(self, container_list): |
396 | 0399ac7e | Stavros Sachtouris | if self['json_output']: |
397 | 0399ac7e | Stavros Sachtouris | print_json(container_list) |
398 | 0399ac7e | Stavros Sachtouris | return
|
399 | 439826ec | Stavros Sachtouris | limit = int(self['limit']) if self['limit'] > 0\ |
400 | 439826ec | Stavros Sachtouris | else len(container_list) |
401 | 234954d1 | Stavros Sachtouris | for index, container in enumerate(container_list): |
402 | 234954d1 | Stavros Sachtouris | if 'bytes' in container: |
403 | 234954d1 | Stavros Sachtouris | size = format_size(container['bytes'])
|
404 | ed9af02c | Stavros Sachtouris | prfx = ('%s. ' % (index + 1)) if self['enum'] else '' |
405 | ed9af02c | Stavros Sachtouris | cname = '%s%s' % (prfx, bold(container['name'])) |
406 | 47ae7577 | Stavros Sachtouris | if self['detail']: |
407 | 7493ccb6 | Stavros Sachtouris | print(cname) |
408 | 7493ccb6 | Stavros Sachtouris | pretty_c = container.copy() |
409 | 234954d1 | Stavros Sachtouris | if 'bytes' in container: |
410 | 234954d1 | Stavros Sachtouris | pretty_c['bytes'] = '%s (%s)' % (container['bytes'], size) |
411 | 7493ccb6 | Stavros Sachtouris | print_dict(pretty_keys(pretty_c), exclude=('name'))
|
412 | 7493ccb6 | Stavros Sachtouris | print
|
413 | 7493ccb6 | Stavros Sachtouris | else:
|
414 | 234954d1 | Stavros Sachtouris | if 'count' in container and 'bytes' in container: |
415 | de73876b | Stavros Sachtouris | print('%s (%s, %s objects)' % (
|
416 | de73876b | Stavros Sachtouris | cname, |
417 | de73876b | Stavros Sachtouris | size, |
418 | de73876b | Stavros Sachtouris | container['count']))
|
419 | 7493ccb6 | Stavros Sachtouris | else:
|
420 | 7493ccb6 | Stavros Sachtouris | print(cname) |
421 | 439826ec | Stavros Sachtouris | if self['more']: |
422 | 439826ec | Stavros Sachtouris | page_hold(index + 1, limit, len(container_list)) |
423 | 7493ccb6 | Stavros Sachtouris | |
424 | 1395c40e | Stavros Sachtouris | @errors.generic.all
|
425 | 1395c40e | Stavros Sachtouris | @errors.pithos.connection
|
426 | 1395c40e | Stavros Sachtouris | @errors.pithos.object_path
|
427 | 1395c40e | Stavros Sachtouris | @errors.pithos.container
|
428 | 1395c40e | Stavros Sachtouris | def _run(self): |
429 | 1395c40e | Stavros Sachtouris | if self.container is None: |
430 | 1395c40e | Stavros Sachtouris | r = self.client.account_get(
|
431 | 1395c40e | Stavros Sachtouris | limit=False if self['more'] else self['limit'], |
432 | 1395c40e | Stavros Sachtouris | marker=self['marker'], |
433 | 1395c40e | Stavros Sachtouris | if_modified_since=self['if_modified_since'], |
434 | 1395c40e | Stavros Sachtouris | if_unmodified_since=self['if_unmodified_since'], |
435 | 1395c40e | Stavros Sachtouris | until=self['until'], |
436 | 1395c40e | Stavros Sachtouris | show_only_shared=self['shared']) |
437 | 545c6c29 | Stavros Sachtouris | self._print(r.json, self.print_containers) |
438 | 1395c40e | Stavros Sachtouris | else:
|
439 | 52edad0a | Stavros Sachtouris | prefix = self.path or self['prefix'] |
440 | 1395c40e | Stavros Sachtouris | r = self.client.container_get(
|
441 | 1395c40e | Stavros Sachtouris | limit=False if self['more'] else self['limit'], |
442 | 1395c40e | Stavros Sachtouris | marker=self['marker'], |
443 | 1395c40e | Stavros Sachtouris | prefix=prefix, |
444 | 1395c40e | Stavros Sachtouris | delimiter=self['delimiter'], |
445 | 1395c40e | Stavros Sachtouris | path=self['path'], |
446 | 1395c40e | Stavros Sachtouris | if_modified_since=self['if_modified_since'], |
447 | 1395c40e | Stavros Sachtouris | if_unmodified_since=self['if_unmodified_since'], |
448 | 1395c40e | Stavros Sachtouris | until=self['until'], |
449 | 1395c40e | Stavros Sachtouris | meta=self['meta'], |
450 | 1395c40e | Stavros Sachtouris | show_only_shared=self['shared']) |
451 | 545c6c29 | Stavros Sachtouris | self._print(r.json, self.print_objects) |
452 | 1395c40e | Stavros Sachtouris | |
453 | 7493ccb6 | Stavros Sachtouris | def main(self, container____path__=None): |
454 | 1395c40e | Stavros Sachtouris | super(self.__class__, self)._run(container____path__) |
455 | 1395c40e | Stavros Sachtouris | self._run()
|
456 | 7493ccb6 | Stavros Sachtouris | |
457 | 234954d1 | Stavros Sachtouris | |
458 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
459 | 915b99b5 | Stavros Sachtouris | class file_mkdir(_file_container_command, _optional_output_cmd): |
460 | f17d6cb5 | Stavros Sachtouris | """Create a directory
|
461 | f17d6cb5 | Stavros Sachtouris | Kamaki hanldes directories the same way as OOS Storage and Pithos+:
|
462 | f17d6cb5 | Stavros Sachtouris | A directory is an object with type "application/directory"
|
463 | f17d6cb5 | Stavros Sachtouris | An object with path dir/name can exist even if dir does not exist
|
464 | f17d6cb5 | Stavros Sachtouris | or even if dir is a non directory object. Users can modify dir '
|
465 | f17d6cb5 | Stavros Sachtouris | without affecting the dir/name object in any way.
|
466 | f17d6cb5 | Stavros Sachtouris | """
|
467 | 7493ccb6 | Stavros Sachtouris | |
468 | 1395c40e | Stavros Sachtouris | @errors.generic.all
|
469 | 1395c40e | Stavros Sachtouris | @errors.pithos.connection
|
470 | 1395c40e | Stavros Sachtouris | @errors.pithos.container
|
471 | 1395c40e | Stavros Sachtouris | def _run(self): |
472 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.create_directory(self.path)) |
473 | 1395c40e | Stavros Sachtouris | |
474 | 7493ccb6 | Stavros Sachtouris | def main(self, container___directory): |
475 | 1395c40e | Stavros Sachtouris | super(self.__class__, self)._run( |
476 | 1395c40e | Stavros Sachtouris | container___directory, |
477 | 1395c40e | Stavros Sachtouris | path_is_optional=False)
|
478 | 1395c40e | Stavros Sachtouris | self._run()
|
479 | 7493ccb6 | Stavros Sachtouris | |
480 | 234954d1 | Stavros Sachtouris | |
481 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
482 | 915b99b5 | Stavros Sachtouris | class file_touch(_file_container_command, _optional_output_cmd): |
483 | 1e29b9f6 | Stavros Sachtouris | """Create an empty object (file)
|
484 | 1e29b9f6 | Stavros Sachtouris | If object exists, this command will reset it to 0 length
|
485 | 1e29b9f6 | Stavros Sachtouris | """
|
486 | 1e29b9f6 | Stavros Sachtouris | |
487 | 1e29b9f6 | Stavros Sachtouris | arguments = dict(
|
488 | 1e29b9f6 | Stavros Sachtouris | content_type=ValueArgument( |
489 | 1e29b9f6 | Stavros Sachtouris | 'Set content type (default: application/octet-stream)',
|
490 | 1e29b9f6 | Stavros Sachtouris | '--content-type',
|
491 | 915b99b5 | Stavros Sachtouris | default='application/octet-stream')
|
492 | 1e29b9f6 | Stavros Sachtouris | ) |
493 | 1e29b9f6 | Stavros Sachtouris | |
494 | 1395c40e | Stavros Sachtouris | @errors.generic.all
|
495 | 1395c40e | Stavros Sachtouris | @errors.pithos.connection
|
496 | 1395c40e | Stavros Sachtouris | @errors.pithos.container
|
497 | 1395c40e | Stavros Sachtouris | def _run(self): |
498 | 915b99b5 | Stavros Sachtouris | self._optional_output(
|
499 | 915b99b5 | Stavros Sachtouris | self.client.create_object(self.path, self['content_type'])) |
500 | 1395c40e | Stavros Sachtouris | |
501 | 1e29b9f6 | Stavros Sachtouris | def main(self, container___path): |
502 | 3ae60112 | Stavros Sachtouris | super(file_touch, self)._run( |
503 | 52edad0a | Stavros Sachtouris | container___path, |
504 | 52edad0a | Stavros Sachtouris | path_is_optional=False)
|
505 | 1395c40e | Stavros Sachtouris | self._run()
|
506 | 1e29b9f6 | Stavros Sachtouris | |
507 | 1e29b9f6 | Stavros Sachtouris | |
508 | 1e29b9f6 | Stavros Sachtouris | @command(pithos_cmds)
|
509 | 915b99b5 | Stavros Sachtouris | class file_create(_file_container_command, _optional_output_cmd): |
510 | 1e29b9f6 | Stavros Sachtouris | """Create a container"""
|
511 | 7493ccb6 | Stavros Sachtouris | |
512 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
513 | 2fe2672e | Stavros Sachtouris | versioning=ValueArgument( |
514 | 201baa17 | Stavros Sachtouris | 'set container versioning (auto/none)', '--versioning'), |
515 | 3ed6dbde | Stavros Sachtouris | limit=IntArgument('set default container limit', '--limit'), |
516 | 2fe2672e | Stavros Sachtouris | meta=KeyValueArgument( |
517 | 201baa17 | Stavros Sachtouris | 'set container metadata (can be repeated)', '--meta') |
518 | 2fe2672e | Stavros Sachtouris | ) |
519 | 7493ccb6 | Stavros Sachtouris | |
520 | 1395c40e | Stavros Sachtouris | @errors.generic.all
|
521 | 1395c40e | Stavros Sachtouris | @errors.pithos.connection
|
522 | 1395c40e | Stavros Sachtouris | @errors.pithos.container
|
523 | 94bedc5b | Stavros Sachtouris | def _run(self, container): |
524 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.create_container( |
525 | 915b99b5 | Stavros Sachtouris | container=container, |
526 | 915b99b5 | Stavros Sachtouris | sizelimit=self['limit'], |
527 | 915b99b5 | Stavros Sachtouris | versioning=self['versioning'], |
528 | 915b99b5 | Stavros Sachtouris | metadata=self['meta'])) |
529 | 1395c40e | Stavros Sachtouris | |
530 | edab7ba7 | Stavros Sachtouris | def main(self, container=None): |
531 | 1395c40e | Stavros Sachtouris | super(self.__class__, self)._run(container) |
532 | edab7ba7 | Stavros Sachtouris | if container and self.container != container: |
533 | 1395c40e | Stavros Sachtouris | raiseCLIError('Invalid container name %s' % container, details=[
|
534 | edab7ba7 | Stavros Sachtouris | 'Did you mean "%s" ?' % self.container, |
535 | 52edad0a | Stavros Sachtouris | 'Use --container for names containing :'])
|
536 | 94bedc5b | Stavros Sachtouris | self._run(container)
|
537 | 7493ccb6 | Stavros Sachtouris | |
538 | 234954d1 | Stavros Sachtouris | |
539 | 3ae60112 | Stavros Sachtouris | class _source_destination_command(_file_container_command): |
540 | 2fe2672e | Stavros Sachtouris | |
541 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
542 | f17d6cb5 | Stavros Sachtouris | destination_account=ValueArgument('', ('-a', '--dst-account')), |
543 | f40f0cb7 | Stavros Sachtouris | recursive=FlagArgument('', ('-R', '--recursive')), |
544 | 761e0cbf | Stavros Sachtouris | prefix=FlagArgument('', '--with-prefix', default=''), |
545 | 761e0cbf | Stavros Sachtouris | suffix=ValueArgument('', '--with-suffix', default=''), |
546 | 761e0cbf | Stavros Sachtouris | add_prefix=ValueArgument('', '--add-prefix', default=''), |
547 | 761e0cbf | Stavros Sachtouris | add_suffix=ValueArgument('', '--add-suffix', default=''), |
548 | 761e0cbf | Stavros Sachtouris | prefix_replace=ValueArgument('', '--prefix-to-replace', default=''), |
549 | 1d3f006b | Stavros Sachtouris | suffix_replace=ValueArgument('', '--suffix-to-replace', default=''), |
550 | 2fe2672e | Stavros Sachtouris | ) |
551 | 7493ccb6 | Stavros Sachtouris | |
552 | b4f69041 | Stavros Sachtouris | def __init__(self, arguments={}, auth_base=None, cloud=None): |
553 | 761e0cbf | Stavros Sachtouris | self.arguments.update(arguments)
|
554 | f724cd35 | Stavros Sachtouris | super(_source_destination_command, self).__init__( |
555 | b4f69041 | Stavros Sachtouris | self.arguments, auth_base, cloud)
|
556 | 761e0cbf | Stavros Sachtouris | |
557 | 300da0fb | Stavros Sachtouris | def _run(self, source_container___path, path_is_optional=False): |
558 | 300da0fb | Stavros Sachtouris | super(_source_destination_command, self)._run( |
559 | 300da0fb | Stavros Sachtouris | source_container___path, |
560 | 300da0fb | Stavros Sachtouris | path_is_optional) |
561 | 300da0fb | Stavros Sachtouris | self.dst_client = PithosClient(
|
562 | 300da0fb | Stavros Sachtouris | base_url=self.client.base_url,
|
563 | 300da0fb | Stavros Sachtouris | token=self.client.token,
|
564 | 300da0fb | Stavros Sachtouris | account=self['destination_account'] or self.client.account) |
565 | 300da0fb | Stavros Sachtouris | |
566 | 300da0fb | Stavros Sachtouris | @errors.generic.all
|
567 | 300da0fb | Stavros Sachtouris | @errors.pithos.account
|
568 | 300da0fb | Stavros Sachtouris | def _dest_container_path(self, dest_container_path): |
569 | 300da0fb | Stavros Sachtouris | if self['destination_container']: |
570 | 300da0fb | Stavros Sachtouris | self.dst_client.container = self['destination_container'] |
571 | 300da0fb | Stavros Sachtouris | return (self['destination_container'], dest_container_path) |
572 | 300da0fb | Stavros Sachtouris | if dest_container_path:
|
573 | 300da0fb | Stavros Sachtouris | dst = dest_container_path.split(':')
|
574 | 300da0fb | Stavros Sachtouris | if len(dst) > 1: |
575 | 300da0fb | Stavros Sachtouris | try:
|
576 | 300da0fb | Stavros Sachtouris | self.dst_client.container = dst[0] |
577 | 300da0fb | Stavros Sachtouris | self.dst_client.get_container_info(dst[0]) |
578 | 300da0fb | Stavros Sachtouris | except ClientError as err: |
579 | 300da0fb | Stavros Sachtouris | if err.status in (404, 204): |
580 | 300da0fb | Stavros Sachtouris | raiseCLIError( |
581 | 300da0fb | Stavros Sachtouris | 'Destination container %s not found' % dst[0]) |
582 | 300da0fb | Stavros Sachtouris | raise
|
583 | 300da0fb | Stavros Sachtouris | else:
|
584 | 300da0fb | Stavros Sachtouris | self.dst_client.container = dst[0] |
585 | 300da0fb | Stavros Sachtouris | return (dst[0], dst[1]) |
586 | 300da0fb | Stavros Sachtouris | return(None, dst[0]) |
587 | 300da0fb | Stavros Sachtouris | raiseCLIError('No destination container:path provided')
|
588 | 300da0fb | Stavros Sachtouris | |
589 | ece4ae4b | Stavros Sachtouris | def _get_all(self, prefix): |
590 | ece4ae4b | Stavros Sachtouris | return self.client.container_get(prefix=prefix).json |
591 | ece4ae4b | Stavros Sachtouris | |
592 | 1d3f006b | Stavros Sachtouris | def _get_src_objects(self, src_path, source_version=None): |
593 | 6736f171 | Stavros Sachtouris | """Get a list of the source objects to be called
|
594 | 6736f171 | Stavros Sachtouris |
|
595 | 6736f171 | Stavros Sachtouris | :param src_path: (str) source path
|
596 | 6736f171 | Stavros Sachtouris |
|
597 | 6736f171 | Stavros Sachtouris | :returns: (method, params) a method that returns a list when called
|
598 | 6736f171 | Stavros Sachtouris | or (object) if it is a single object
|
599 | 6736f171 | Stavros Sachtouris | """
|
600 | ece4ae4b | Stavros Sachtouris | if src_path and src_path[-1] == '/': |
601 | ece4ae4b | Stavros Sachtouris | src_path = src_path[:-1]
|
602 | ece4ae4b | Stavros Sachtouris | |
603 | 6736f171 | Stavros Sachtouris | if self['prefix']: |
604 | 6736f171 | Stavros Sachtouris | return (self._get_all, dict(prefix=src_path)) |
605 | ece4ae4b | Stavros Sachtouris | try:
|
606 | 1d3f006b | Stavros Sachtouris | srcobj = self.client.get_object_info(
|
607 | 1d3f006b | Stavros Sachtouris | src_path, version=source_version) |
608 | ece4ae4b | Stavros Sachtouris | except ClientError as srcerr: |
609 | 6736f171 | Stavros Sachtouris | if srcerr.status == 404: |
610 | 6736f171 | Stavros Sachtouris | raiseCLIError( |
611 | 300da0fb | Stavros Sachtouris | 'Source object %s not in source container %s' % (
|
612 | 201baa17 | Stavros Sachtouris | src_path, self.client.container),
|
613 | 6736f171 | Stavros Sachtouris | details=['Hint: --with-prefix to match multiple objects'])
|
614 | 6736f171 | Stavros Sachtouris | elif srcerr.status not in (204,): |
615 | ece4ae4b | Stavros Sachtouris | raise
|
616 | 6736f171 | Stavros Sachtouris | return (self.client.list_objects, {}) |
617 | 300da0fb | Stavros Sachtouris | |
618 | 6736f171 | Stavros Sachtouris | if self._is_dir(srcobj): |
619 | 6736f171 | Stavros Sachtouris | if not self['recursive']: |
620 | 6736f171 | Stavros Sachtouris | raiseCLIError( |
621 | 300da0fb | Stavros Sachtouris | 'Object %s of cont. %s is a dir' % (
|
622 | 201baa17 | Stavros Sachtouris | src_path, self.client.container),
|
623 | 6736f171 | Stavros Sachtouris | details=['Use --recursive to access directories'])
|
624 | 6736f171 | Stavros Sachtouris | return (self._get_all, dict(prefix=src_path)) |
625 | 6736f171 | Stavros Sachtouris | srcobj['name'] = src_path
|
626 | 6736f171 | Stavros Sachtouris | return srcobj
|
627 | 6736f171 | Stavros Sachtouris | |
628 | 1d3f006b | Stavros Sachtouris | def src_dst_pairs(self, dst_path, source_version=None): |
629 | 1d3f006b | Stavros Sachtouris | src_iter = self._get_src_objects(self.path, source_version) |
630 | 6736f171 | Stavros Sachtouris | src_N = isinstance(src_iter, tuple) |
631 | 6736f171 | Stavros Sachtouris | add_prefix = self['add_prefix'].strip('/') |
632 | 6736f171 | Stavros Sachtouris | |
633 | 6736f171 | Stavros Sachtouris | if dst_path and dst_path.endswith('/'): |
634 | ece4ae4b | Stavros Sachtouris | dst_path = dst_path[:-1]
|
635 | ece4ae4b | Stavros Sachtouris | |
636 | 6736f171 | Stavros Sachtouris | try:
|
637 | 300da0fb | Stavros Sachtouris | dstobj = self.dst_client.get_object_info(dst_path)
|
638 | 6736f171 | Stavros Sachtouris | except ClientError as trgerr: |
639 | 6736f171 | Stavros Sachtouris | if trgerr.status in (404,): |
640 | 6736f171 | Stavros Sachtouris | if src_N:
|
641 | 6736f171 | Stavros Sachtouris | raiseCLIError( |
642 | 6736f171 | Stavros Sachtouris | 'Cannot merge multiple paths to path %s' % dst_path,
|
643 | 6736f171 | Stavros Sachtouris | details=[ |
644 | 6736f171 | Stavros Sachtouris | 'Try to use / or a directory as destination',
|
645 | 3ae60112 | Stavros Sachtouris | 'or create the destination dir (/file mkdir)',
|
646 | 6736f171 | Stavros Sachtouris | 'or use a single object as source'])
|
647 | 6736f171 | Stavros Sachtouris | elif trgerr.status not in (204,): |
648 | 6736f171 | Stavros Sachtouris | raise
|
649 | 6736f171 | Stavros Sachtouris | else:
|
650 | 6736f171 | Stavros Sachtouris | if self._is_dir(dstobj): |
651 | 6736f171 | Stavros Sachtouris | add_prefix = '%s/%s' % (dst_path.strip('/'), add_prefix) |
652 | 6736f171 | Stavros Sachtouris | elif src_N:
|
653 | 6736f171 | Stavros Sachtouris | raiseCLIError( |
654 | 6736f171 | Stavros Sachtouris | 'Cannot merge multiple paths to path' % dst_path,
|
655 | 6736f171 | Stavros Sachtouris | details=[ |
656 | 6736f171 | Stavros Sachtouris | 'Try to use / or a directory as destination',
|
657 | 3ae60112 | Stavros Sachtouris | 'or create the destination dir (/file mkdir)',
|
658 | 6736f171 | Stavros Sachtouris | 'or use a single object as source'])
|
659 | 6736f171 | Stavros Sachtouris | |
660 | 6736f171 | Stavros Sachtouris | if src_N:
|
661 | 6736f171 | Stavros Sachtouris | (method, kwargs) = src_iter |
662 | 6736f171 | Stavros Sachtouris | for obj in method(**kwargs): |
663 | 6736f171 | Stavros Sachtouris | name = obj['name']
|
664 | 6736f171 | Stavros Sachtouris | if name.endswith(self['suffix']): |
665 | 6736f171 | Stavros Sachtouris | yield (name, self._get_new_object(name, add_prefix)) |
666 | 6736f171 | Stavros Sachtouris | elif src_iter['name'].endswith(self['suffix']): |
667 | 6736f171 | Stavros Sachtouris | name = src_iter['name']
|
668 | 6736f171 | Stavros Sachtouris | yield (name, self._get_new_object(dst_path or name, add_prefix)) |
669 | 6736f171 | Stavros Sachtouris | else:
|
670 | 6736f171 | Stavros Sachtouris | raiseCLIError('Source path %s conflicts with suffix %s' % (
|
671 | 201baa17 | Stavros Sachtouris | src_iter['name'], self['suffix'])) |
672 | ece4ae4b | Stavros Sachtouris | |
673 | 6736f171 | Stavros Sachtouris | def _get_new_object(self, obj, add_prefix): |
674 | 6736f171 | Stavros Sachtouris | if self['prefix_replace'] and obj.startswith(self['prefix_replace']): |
675 | 6736f171 | Stavros Sachtouris | obj = obj[len(self['prefix_replace']):] |
676 | 6736f171 | Stavros Sachtouris | if self['suffix_replace'] and obj.endswith(self['suffix_replace']): |
677 | 6736f171 | Stavros Sachtouris | obj = obj[:-len(self['suffix_replace'])] |
678 | 6736f171 | Stavros Sachtouris | return add_prefix + obj + self['add_suffix'] |
679 | efdee310 | Stavros Sachtouris | |
680 | 761e0cbf | Stavros Sachtouris | |
681 | 761e0cbf | Stavros Sachtouris | @command(pithos_cmds)
|
682 | 915b99b5 | Stavros Sachtouris | class file_copy(_source_destination_command, _optional_output_cmd): |
683 | 761e0cbf | Stavros Sachtouris | """Copy objects from container to (another) container
|
684 | 761e0cbf | Stavros Sachtouris | Semantics:
|
685 | 761e0cbf | Stavros Sachtouris | copy cont:path dir
|
686 | 761e0cbf | Stavros Sachtouris | . transfer path as dir/path
|
687 | 761e0cbf | Stavros Sachtouris | copy cont:path cont2:
|
688 | 761e0cbf | Stavros Sachtouris | . trasnfer all <obj> prefixed with path to container cont2
|
689 | 761e0cbf | Stavros Sachtouris | copy cont:path [cont2:]path2
|
690 | 761e0cbf | Stavros Sachtouris | . transfer path to path2
|
691 | 761e0cbf | Stavros Sachtouris | Use options:
|
692 | 761e0cbf | Stavros Sachtouris | 1. <container1>:<path1> [container2:]<path2> : if container2 is not given,
|
693 | 761e0cbf | Stavros Sachtouris | destination is container1:path2
|
694 | 761e0cbf | Stavros Sachtouris | 2. <container>:<path1> <path2> : make a copy in the same container
|
695 | 761e0cbf | Stavros Sachtouris | 3. Can use --container= instead of <container1>
|
696 | 761e0cbf | Stavros Sachtouris | """
|
697 | 761e0cbf | Stavros Sachtouris | |
698 | 761e0cbf | Stavros Sachtouris | arguments = dict(
|
699 | 300da0fb | Stavros Sachtouris | destination_account=ValueArgument( |
700 | 201baa17 | Stavros Sachtouris | 'Account to copy to', ('-a', '--dst-account')), |
701 | 761e0cbf | Stavros Sachtouris | destination_container=ValueArgument( |
702 | 761e0cbf | Stavros Sachtouris | 'use it if destination container name contains a : character',
|
703 | f40f0cb7 | Stavros Sachtouris | ('-D', '--dst-container')), |
704 | 761e0cbf | Stavros Sachtouris | public=ValueArgument('make object publicly accessible', '--public'), |
705 | 761e0cbf | Stavros Sachtouris | content_type=ValueArgument( |
706 | 201baa17 | Stavros Sachtouris | 'change object\'s content type', '--content-type'), |
707 | 761e0cbf | Stavros Sachtouris | recursive=FlagArgument( |
708 | 201baa17 | Stavros Sachtouris | 'copy directory and contents', ('-R', '--recursive')), |
709 | 761e0cbf | Stavros Sachtouris | prefix=FlagArgument( |
710 | 761e0cbf | Stavros Sachtouris | 'Match objects prefixed with src path (feels like src_path*)',
|
711 | 761e0cbf | Stavros Sachtouris | '--with-prefix',
|
712 | 761e0cbf | Stavros Sachtouris | default=''),
|
713 | 761e0cbf | Stavros Sachtouris | suffix=ValueArgument( |
714 | 201baa17 | Stavros Sachtouris | 'Suffix of source objects (feels like *suffix)', '--with-suffix', |
715 | 761e0cbf | Stavros Sachtouris | default=''),
|
716 | 761e0cbf | Stavros Sachtouris | add_prefix=ValueArgument('Prefix targets', '--add-prefix', default=''), |
717 | 761e0cbf | Stavros Sachtouris | add_suffix=ValueArgument('Suffix targets', '--add-suffix', default=''), |
718 | 761e0cbf | Stavros Sachtouris | prefix_replace=ValueArgument( |
719 | 761e0cbf | Stavros Sachtouris | 'Prefix of src to replace with dst path + add_prefix, if matched',
|
720 | 761e0cbf | Stavros Sachtouris | '--prefix-to-replace',
|
721 | 761e0cbf | Stavros Sachtouris | default=''),
|
722 | 761e0cbf | Stavros Sachtouris | suffix_replace=ValueArgument( |
723 | 761e0cbf | Stavros Sachtouris | 'Suffix of src to replace with add_suffix, if matched',
|
724 | 761e0cbf | Stavros Sachtouris | '--suffix-to-replace',
|
725 | 1d3f006b | Stavros Sachtouris | default=''),
|
726 | 1d3f006b | Stavros Sachtouris | source_version=ValueArgument( |
727 | 201baa17 | Stavros Sachtouris | 'copy specific version', ('-S', '--source-version')) |
728 | 761e0cbf | Stavros Sachtouris | ) |
729 | 761e0cbf | Stavros Sachtouris | |
730 | 1395c40e | Stavros Sachtouris | @errors.generic.all
|
731 | 1395c40e | Stavros Sachtouris | @errors.pithos.connection
|
732 | 1395c40e | Stavros Sachtouris | @errors.pithos.container
|
733 | 300da0fb | Stavros Sachtouris | @errors.pithos.account
|
734 | 300da0fb | Stavros Sachtouris | def _run(self, dst_path): |
735 | 1395c40e | Stavros Sachtouris | no_source_object = True
|
736 | 300da0fb | Stavros Sachtouris | src_account = self.client.account if ( |
737 | 300da0fb | Stavros Sachtouris | self['destination_account']) else None |
738 | 1d3f006b | Stavros Sachtouris | for src_obj, dst_obj in self.src_dst_pairs( |
739 | 1d3f006b | Stavros Sachtouris | dst_path, self['source_version']): |
740 | 1395c40e | Stavros Sachtouris | no_source_object = False
|
741 | 55c75058 | Stavros Sachtouris | r = self.dst_client.copy_object(
|
742 | 300da0fb | Stavros Sachtouris | src_container=self.client.container,
|
743 | 3a066af4 | Stavros Sachtouris | src_object=src_obj, |
744 | 300da0fb | Stavros Sachtouris | dst_container=self.dst_client.container,
|
745 | 300da0fb | Stavros Sachtouris | dst_object=dst_obj, |
746 | 300da0fb | Stavros Sachtouris | source_account=src_account, |
747 | 1395c40e | Stavros Sachtouris | source_version=self['source_version'], |
748 | 1395c40e | Stavros Sachtouris | public=self['public'], |
749 | 1395c40e | Stavros Sachtouris | content_type=self['content_type']) |
750 | 1395c40e | Stavros Sachtouris | if no_source_object:
|
751 | 1395c40e | Stavros Sachtouris | raiseCLIError('No object %s in container %s' % (
|
752 | 201baa17 | Stavros Sachtouris | self.path, self.container)) |
753 | 915b99b5 | Stavros Sachtouris | self._optional_output(r)
|
754 | 1395c40e | Stavros Sachtouris | |
755 | de73876b | Stavros Sachtouris | def main( |
756 | e2d7b883 | Stavros Sachtouris | self, source_container___path,
|
757 | 24ff0a35 | Stavros Sachtouris | destination_container___path=None):
|
758 | 3ae60112 | Stavros Sachtouris | super(file_copy, self)._run( |
759 | 1395c40e | Stavros Sachtouris | source_container___path, |
760 | 1395c40e | Stavros Sachtouris | path_is_optional=False)
|
761 | 1395c40e | Stavros Sachtouris | (dst_cont, dst_path) = self._dest_container_path(
|
762 | 0e4ee6d1 | Stavros Sachtouris | destination_container___path) |
763 | 300da0fb | Stavros Sachtouris | self.dst_client.container = dst_cont or self.container |
764 | 300da0fb | Stavros Sachtouris | self._run(dst_path=dst_path or '') |
765 | 7493ccb6 | Stavros Sachtouris | |
766 | 234954d1 | Stavros Sachtouris | |
767 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
768 | 915b99b5 | Stavros Sachtouris | class file_move(_source_destination_command, _optional_output_cmd): |
769 | 761e0cbf | Stavros Sachtouris | """Move/rename objects from container to (another) container
|
770 | 8249ee0f | Stavros Sachtouris | Semantics:
|
771 | 761e0cbf | Stavros Sachtouris | move cont:path dir
|
772 | 761e0cbf | Stavros Sachtouris | . rename path as dir/path
|
773 | 8249ee0f | Stavros Sachtouris | move cont:path cont2:
|
774 | 761e0cbf | Stavros Sachtouris | . trasnfer all <obj> prefixed with path to container cont2
|
775 | 761e0cbf | Stavros Sachtouris | move cont:path [cont2:]path2
|
776 | 761e0cbf | Stavros Sachtouris | . transfer path to path2
|
777 | 2fe2672e | Stavros Sachtouris | Use options:
|
778 | 761e0cbf | Stavros Sachtouris | 1. <container1>:<path1> [container2:]<path2> : if container2 is not given,
|
779 | 795bf206 | Stavros Sachtouris | destination is container1:path2
|
780 | 761e0cbf | Stavros Sachtouris | 2. <container>:<path1> <path2> : move in the same container
|
781 | 8249ee0f | Stavros Sachtouris | 3. Can use --container= instead of <container1>
|
782 | 2fe2672e | Stavros Sachtouris | """
|
783 | 2fe2672e | Stavros Sachtouris | |
784 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
785 | 300da0fb | Stavros Sachtouris | destination_account=ValueArgument( |
786 | 201baa17 | Stavros Sachtouris | 'Account to move to', ('-a', '--dst-account')), |
787 | 0e4ee6d1 | Stavros Sachtouris | destination_container=ValueArgument( |
788 | 0e4ee6d1 | Stavros Sachtouris | 'use it if destination container name contains a : character',
|
789 | f40f0cb7 | Stavros Sachtouris | ('-D', '--dst-container')), |
790 | 761e0cbf | Stavros Sachtouris | public=ValueArgument('make object publicly accessible', '--public'), |
791 | 761e0cbf | Stavros Sachtouris | content_type=ValueArgument( |
792 | 201baa17 | Stavros Sachtouris | 'change object\'s content type', '--content-type'), |
793 | 761e0cbf | Stavros Sachtouris | recursive=FlagArgument( |
794 | 201baa17 | Stavros Sachtouris | 'copy directory and contents', ('-R', '--recursive')), |
795 | 761e0cbf | Stavros Sachtouris | prefix=FlagArgument( |
796 | 761e0cbf | Stavros Sachtouris | 'Match objects prefixed with src path (feels like src_path*)',
|
797 | 761e0cbf | Stavros Sachtouris | '--with-prefix',
|
798 | 761e0cbf | Stavros Sachtouris | default=''),
|
799 | 761e0cbf | Stavros Sachtouris | suffix=ValueArgument( |
800 | 201baa17 | Stavros Sachtouris | 'Suffix of source objects (feels like *suffix)', '--with-suffix', |
801 | 761e0cbf | Stavros Sachtouris | default=''),
|
802 | 761e0cbf | Stavros Sachtouris | add_prefix=ValueArgument('Prefix targets', '--add-prefix', default=''), |
803 | 761e0cbf | Stavros Sachtouris | add_suffix=ValueArgument('Suffix targets', '--add-suffix', default=''), |
804 | 761e0cbf | Stavros Sachtouris | prefix_replace=ValueArgument( |
805 | 761e0cbf | Stavros Sachtouris | 'Prefix of src to replace with dst path + add_prefix, if matched',
|
806 | 761e0cbf | Stavros Sachtouris | '--prefix-to-replace',
|
807 | 761e0cbf | Stavros Sachtouris | default=''),
|
808 | 761e0cbf | Stavros Sachtouris | suffix_replace=ValueArgument( |
809 | 761e0cbf | Stavros Sachtouris | 'Suffix of src to replace with add_suffix, if matched',
|
810 | 761e0cbf | Stavros Sachtouris | '--suffix-to-replace',
|
811 | 915b99b5 | Stavros Sachtouris | default='')
|
812 | 2fe2672e | Stavros Sachtouris | ) |
813 | 7493ccb6 | Stavros Sachtouris | |
814 | 1395c40e | Stavros Sachtouris | @errors.generic.all
|
815 | 1395c40e | Stavros Sachtouris | @errors.pithos.connection
|
816 | 1395c40e | Stavros Sachtouris | @errors.pithos.container
|
817 | 300da0fb | Stavros Sachtouris | def _run(self, dst_path): |
818 | 1395c40e | Stavros Sachtouris | no_source_object = True
|
819 | 300da0fb | Stavros Sachtouris | src_account = self.client.account if ( |
820 | 300da0fb | Stavros Sachtouris | self['destination_account']) else None |
821 | 300da0fb | Stavros Sachtouris | for src_obj, dst_obj in self.src_dst_pairs(dst_path): |
822 | 1395c40e | Stavros Sachtouris | no_source_object = False
|
823 | 55c75058 | Stavros Sachtouris | r = self.dst_client.move_object(
|
824 | 1395c40e | Stavros Sachtouris | src_container=self.container,
|
825 | 300da0fb | Stavros Sachtouris | src_object=src_obj, |
826 | 300da0fb | Stavros Sachtouris | dst_container=self.dst_client.container,
|
827 | 300da0fb | Stavros Sachtouris | dst_object=dst_obj, |
828 | 4f266635 | Stavros Sachtouris | source_account=src_account, |
829 | 1395c40e | Stavros Sachtouris | public=self['public'], |
830 | 1395c40e | Stavros Sachtouris | content_type=self['content_type']) |
831 | 1395c40e | Stavros Sachtouris | if no_source_object:
|
832 | 1395c40e | Stavros Sachtouris | raiseCLIError('No object %s in container %s' % (
|
833 | 1395c40e | Stavros Sachtouris | self.path,
|
834 | 1395c40e | Stavros Sachtouris | self.container))
|
835 | 915b99b5 | Stavros Sachtouris | self._optional_output(r)
|
836 | 1395c40e | Stavros Sachtouris | |
837 | de73876b | Stavros Sachtouris | def main( |
838 | e2d7b883 | Stavros Sachtouris | self, source_container___path,
|
839 | 24ff0a35 | Stavros Sachtouris | destination_container___path=None):
|
840 | 1395c40e | Stavros Sachtouris | super(self.__class__, self)._run( |
841 | 1395c40e | Stavros Sachtouris | source_container___path, |
842 | 1395c40e | Stavros Sachtouris | path_is_optional=False)
|
843 | 45b70023 | Stavros Sachtouris | (dst_cont, dst_path) = self._dest_container_path(
|
844 | 1395c40e | Stavros Sachtouris | destination_container___path) |
845 | 300da0fb | Stavros Sachtouris | (dst_cont, dst_path) = self._dest_container_path(
|
846 | 300da0fb | Stavros Sachtouris | destination_container___path) |
847 | 300da0fb | Stavros Sachtouris | self.dst_client.container = dst_cont or self.container |
848 | 300da0fb | Stavros Sachtouris | self._run(dst_path=dst_path or '') |
849 | 7493ccb6 | Stavros Sachtouris | |
850 | 234954d1 | Stavros Sachtouris | |
851 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
852 | 915b99b5 | Stavros Sachtouris | class file_append(_file_container_command, _optional_output_cmd): |
853 | aaca2ef4 | Stavros Sachtouris | """Append local file to (existing) remote object
|
854 | aaca2ef4 | Stavros Sachtouris | The remote object should exist.
|
855 | aaca2ef4 | Stavros Sachtouris | If the remote object is a directory, it is transformed into a file.
|
856 | aaca2ef4 | Stavros Sachtouris | In the later case, objects under the directory remain intact.
|
857 | aaca2ef4 | Stavros Sachtouris | """
|
858 | 7493ccb6 | Stavros Sachtouris | |
859 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
860 | 2fe2672e | Stavros Sachtouris | progress_bar=ProgressBarArgument( |
861 | 2fe2672e | Stavros Sachtouris | 'do not show progress bar',
|
862 | f40f0cb7 | Stavros Sachtouris | ('-N', '--no-progress-bar'), |
863 | 915b99b5 | Stavros Sachtouris | default=False)
|
864 | 2fe2672e | Stavros Sachtouris | ) |
865 | 486f7af1 | Stavros Sachtouris | |
866 | ca092af4 | Stavros Sachtouris | @errors.generic.all
|
867 | ca092af4 | Stavros Sachtouris | @errors.pithos.connection
|
868 | ca092af4 | Stavros Sachtouris | @errors.pithos.container
|
869 | ca092af4 | Stavros Sachtouris | @errors.pithos.object_path
|
870 | ca092af4 | Stavros Sachtouris | def _run(self, local_path): |
871 | ca092af4 | Stavros Sachtouris | (progress_bar, upload_cb) = self._safe_progress_bar('Appending') |
872 | 7493ccb6 | Stavros Sachtouris | try:
|
873 | e876ac62 | Stavros Sachtouris | f = open(local_path, 'rb') |
874 | 915b99b5 | Stavros Sachtouris | self._optional_output(
|
875 | 915b99b5 | Stavros Sachtouris | self.client.append_object(self.path, f, upload_cb)) |
876 | ca092af4 | Stavros Sachtouris | except Exception: |
877 | ca092af4 | Stavros Sachtouris | self._safe_progress_bar_finish(progress_bar)
|
878 | ca092af4 | Stavros Sachtouris | raise
|
879 | 852a22e7 | Stavros Sachtouris | finally:
|
880 | ca092af4 | Stavros Sachtouris | self._safe_progress_bar_finish(progress_bar)
|
881 | ca092af4 | Stavros Sachtouris | |
882 | ca092af4 | Stavros Sachtouris | def main(self, local_path, container___path): |
883 | ca092af4 | Stavros Sachtouris | super(self.__class__, self)._run( |
884 | 201baa17 | Stavros Sachtouris | container___path, path_is_optional=False)
|
885 | ca092af4 | Stavros Sachtouris | self._run(local_path)
|
886 | 7493ccb6 | Stavros Sachtouris | |
887 | 234954d1 | Stavros Sachtouris | |
888 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
889 | 915b99b5 | Stavros Sachtouris | class file_truncate(_file_container_command, _optional_output_cmd): |
890 | aaca2ef4 | Stavros Sachtouris | """Truncate remote file up to a size (default is 0)"""
|
891 | 7493ccb6 | Stavros Sachtouris | |
892 | ca092af4 | Stavros Sachtouris | @errors.generic.all
|
893 | ca092af4 | Stavros Sachtouris | @errors.pithos.connection
|
894 | ca092af4 | Stavros Sachtouris | @errors.pithos.container
|
895 | ca092af4 | Stavros Sachtouris | @errors.pithos.object_path
|
896 | ca092af4 | Stavros Sachtouris | @errors.pithos.object_size
|
897 | ca092af4 | Stavros Sachtouris | def _run(self, size=0): |
898 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.truncate_object(self.path, size)) |
899 | ca092af4 | Stavros Sachtouris | |
900 | 7493ccb6 | Stavros Sachtouris | def main(self, container___path, size=0): |
901 | ca092af4 | Stavros Sachtouris | super(self.__class__, self)._run(container___path) |
902 | ca092af4 | Stavros Sachtouris | self._run(size=size)
|
903 | 7493ccb6 | Stavros Sachtouris | |
904 | 234954d1 | Stavros Sachtouris | |
905 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
906 | 915b99b5 | Stavros Sachtouris | class file_overwrite(_file_container_command, _optional_output_cmd): |
907 | aaca2ef4 | Stavros Sachtouris | """Overwrite part (from start to end) of a remote file
|
908 | aaca2ef4 | Stavros Sachtouris | overwrite local-path container 10 20
|
909 | aaca2ef4 | Stavros Sachtouris | . will overwrite bytes from 10 to 20 of a remote file with the same name
|
910 | aaca2ef4 | Stavros Sachtouris | . as local-path basename
|
911 | aaca2ef4 | Stavros Sachtouris | overwrite local-path container:path 10 20
|
912 | aaca2ef4 | Stavros Sachtouris | . will overwrite as above, but the remote file is named path
|
913 | aaca2ef4 | Stavros Sachtouris | """
|
914 | 7493ccb6 | Stavros Sachtouris | |
915 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
916 | 2fe2672e | Stavros Sachtouris | progress_bar=ProgressBarArgument( |
917 | 2fe2672e | Stavros Sachtouris | 'do not show progress bar',
|
918 | f40f0cb7 | Stavros Sachtouris | ('-N', '--no-progress-bar'), |
919 | 2fe2672e | Stavros Sachtouris | default=False)
|
920 | 2fe2672e | Stavros Sachtouris | ) |
921 | 486f7af1 | Stavros Sachtouris | |
922 | ca092af4 | Stavros Sachtouris | def _open_file(self, local_path, start): |
923 | ca092af4 | Stavros Sachtouris | f = open(path.abspath(local_path), 'rb') |
924 | ca092af4 | Stavros Sachtouris | f.seek(0, 2) |
925 | ca092af4 | Stavros Sachtouris | f_size = f.tell() |
926 | ca092af4 | Stavros Sachtouris | f.seek(start, 0)
|
927 | ca092af4 | Stavros Sachtouris | return (f, f_size)
|
928 | ca092af4 | Stavros Sachtouris | |
929 | ca092af4 | Stavros Sachtouris | @errors.generic.all
|
930 | ca092af4 | Stavros Sachtouris | @errors.pithos.connection
|
931 | ca092af4 | Stavros Sachtouris | @errors.pithos.container
|
932 | ca092af4 | Stavros Sachtouris | @errors.pithos.object_path
|
933 | ca092af4 | Stavros Sachtouris | @errors.pithos.object_size
|
934 | ca092af4 | Stavros Sachtouris | def _run(self, local_path, start, end): |
935 | ca092af4 | Stavros Sachtouris | (start, end) = (int(start), int(end)) |
936 | ca092af4 | Stavros Sachtouris | (f, f_size) = self._open_file(local_path, start)
|
937 | ca092af4 | Stavros Sachtouris | (progress_bar, upload_cb) = self._safe_progress_bar(
|
938 | ca092af4 | Stavros Sachtouris | 'Overwrite %s bytes' % (end - start))
|
939 | aaca2ef4 | Stavros Sachtouris | try:
|
940 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.overwrite_object( |
941 | 8fa6ef6a | Stavros Sachtouris | obj=self.path,
|
942 | 234954d1 | Stavros Sachtouris | start=start, |
943 | 234954d1 | Stavros Sachtouris | end=end, |
944 | 234954d1 | Stavros Sachtouris | source_file=f, |
945 | 915b99b5 | Stavros Sachtouris | upload_cb=upload_cb)) |
946 | 852a22e7 | Stavros Sachtouris | finally:
|
947 | ca092af4 | Stavros Sachtouris | self._safe_progress_bar_finish(progress_bar)
|
948 | ca092af4 | Stavros Sachtouris | |
949 | 52edad0a | Stavros Sachtouris | def main(self, local_path, container___path, start, end): |
950 | 52edad0a | Stavros Sachtouris | super(self.__class__, self)._run( |
951 | 201baa17 | Stavros Sachtouris | container___path, path_is_optional=None)
|
952 | 52edad0a | Stavros Sachtouris | self.path = self.path or path.basename(local_path) |
953 | ca092af4 | Stavros Sachtouris | self._run(local_path=local_path, start=start, end=end)
|
954 | 7493ccb6 | Stavros Sachtouris | |
955 | 234954d1 | Stavros Sachtouris | |
956 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
957 | 915b99b5 | Stavros Sachtouris | class file_manifest(_file_container_command, _optional_output_cmd): |
958 | 8fa6ef6a | Stavros Sachtouris | """Create a remote file of uploaded parts by manifestation
|
959 | 35b52b0e | Stavros Sachtouris | Remains functional for compatibility with OOS Storage. Users are advised
|
960 | 35b52b0e | Stavros Sachtouris | to use the upload command instead.
|
961 | 35b52b0e | Stavros Sachtouris | Manifestation is a compliant process for uploading large files. The files
|
962 | 35b52b0e | Stavros Sachtouris | have to be chunked in smalled files and uploaded as <prefix><increment>
|
963 | 35b52b0e | Stavros Sachtouris | where increment is 1, 2, ...
|
964 | 35b52b0e | Stavros Sachtouris | Finally, the manifest command glues partial files together in one file
|
965 | 35b52b0e | Stavros Sachtouris | named <prefix>
|
966 | 35b52b0e | Stavros Sachtouris | The upload command is faster, easier and more intuitive than manifest
|
967 | 8fa6ef6a | Stavros Sachtouris | """
|
968 | 486f7af1 | Stavros Sachtouris | |
969 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
970 | 2fe2672e | Stavros Sachtouris | etag=ValueArgument('check written data', '--etag'), |
971 | 2fe2672e | Stavros Sachtouris | content_encoding=ValueArgument( |
972 | 201baa17 | Stavros Sachtouris | 'set MIME content type', '--content-encoding'), |
973 | 2fe2672e | Stavros Sachtouris | content_disposition=ValueArgument( |
974 | 201baa17 | Stavros Sachtouris | 'the presentation style of the object', '--content-disposition'), |
975 | 8fa6ef6a | Stavros Sachtouris | content_type=ValueArgument( |
976 | 201baa17 | Stavros Sachtouris | 'specify content type', '--content-type', |
977 | 8fa6ef6a | Stavros Sachtouris | default='application/octet-stream'),
|
978 | 2fe2672e | Stavros Sachtouris | sharing=SharingArgument( |
979 | de73876b | Stavros Sachtouris | '\n'.join([
|
980 | de73876b | Stavros Sachtouris | 'define object sharing policy',
|
981 | de73876b | Stavros Sachtouris | ' ( "read=user1,grp1,user2,... write=user1,grp2,..." )']),
|
982 | 2fe2672e | Stavros Sachtouris | '--sharing'),
|
983 | 2fe2672e | Stavros Sachtouris | public=FlagArgument('make object publicly accessible', '--public') |
984 | 2fe2672e | Stavros Sachtouris | ) |
985 | 234954d1 | Stavros Sachtouris | |
986 | 68858765 | Stavros Sachtouris | @errors.generic.all
|
987 | 68858765 | Stavros Sachtouris | @errors.pithos.connection
|
988 | 68858765 | Stavros Sachtouris | @errors.pithos.container
|
989 | 68858765 | Stavros Sachtouris | @errors.pithos.object_path
|
990 | 68858765 | Stavros Sachtouris | def _run(self): |
991 | abff3366 | Stavros Sachtouris | ctype, cenc = guess_mime_type(self.path)
|
992 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.create_object_by_manifestation( |
993 | 68858765 | Stavros Sachtouris | self.path,
|
994 | f8426b5c | Stavros Sachtouris | content_encoding=self['content_encoding'] or cenc, |
995 | 68858765 | Stavros Sachtouris | content_disposition=self['content_disposition'], |
996 | abff3366 | Stavros Sachtouris | content_type=self['content_type'] or ctype, |
997 | 68858765 | Stavros Sachtouris | sharing=self['sharing'], |
998 | 915b99b5 | Stavros Sachtouris | public=self['public'])) |
999 | 68858765 | Stavros Sachtouris | |
1000 | 7493ccb6 | Stavros Sachtouris | def main(self, container___path): |
1001 | 68858765 | Stavros Sachtouris | super(self.__class__, self)._run( |
1002 | 201baa17 | Stavros Sachtouris | container___path, path_is_optional=False)
|
1003 | 68858765 | Stavros Sachtouris | self.run()
|
1004 | 7493ccb6 | Stavros Sachtouris | |
1005 | 234954d1 | Stavros Sachtouris | |
1006 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1007 | 915b99b5 | Stavros Sachtouris | class file_upload(_file_container_command, _optional_output_cmd): |
1008 | 7493ccb6 | Stavros Sachtouris | """Upload a file"""
|
1009 | 7493ccb6 | Stavros Sachtouris | |
1010 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
1011 | 2fe2672e | Stavros Sachtouris | use_hashes=FlagArgument( |
1012 | 201baa17 | Stavros Sachtouris | 'provide hashmap file instead of data', '--use-hashes'), |
1013 | 2fe2672e | Stavros Sachtouris | etag=ValueArgument('check written data', '--etag'), |
1014 | 2fe2672e | Stavros Sachtouris | unchunked=FlagArgument('avoid chunked transfer mode', '--unchunked'), |
1015 | 2fe2672e | Stavros Sachtouris | content_encoding=ValueArgument( |
1016 | 201baa17 | Stavros Sachtouris | 'set MIME content type', '--content-encoding'), |
1017 | 2fe2672e | Stavros Sachtouris | content_disposition=ValueArgument( |
1018 | 201baa17 | Stavros Sachtouris | 'specify objects presentation style', '--content-disposition'), |
1019 | 2fe2672e | Stavros Sachtouris | content_type=ValueArgument('specify content type', '--content-type'), |
1020 | 2fe2672e | Stavros Sachtouris | sharing=SharingArgument( |
1021 | de73876b | Stavros Sachtouris | help='\n'.join([
|
1022 | de73876b | Stavros Sachtouris | 'define sharing object policy',
|
1023 | de73876b | Stavros Sachtouris | '( "read=user1,grp1,user2,... write=user1,grp2,... )']),
|
1024 | 2fe2672e | Stavros Sachtouris | parsed_name='--sharing'),
|
1025 | 2fe2672e | Stavros Sachtouris | public=FlagArgument('make object publicly accessible', '--public'), |
1026 | 2fe2672e | Stavros Sachtouris | poolsize=IntArgument('set pool size', '--with-pool-size'), |
1027 | 2fe2672e | Stavros Sachtouris | progress_bar=ProgressBarArgument( |
1028 | 2fe2672e | Stavros Sachtouris | 'do not show progress bar',
|
1029 | 8741c407 | Stavros Sachtouris | ('-N', '--no-progress-bar'), |
1030 | 706fc940 | Stavros Sachtouris | default=False),
|
1031 | 2d158d8b | Stavros Sachtouris | overwrite=FlagArgument('Force (over)write', ('-f', '--force')), |
1032 | 2d158d8b | Stavros Sachtouris | recursive=FlagArgument( |
1033 | 2d158d8b | Stavros Sachtouris | 'Recursively upload directory *contents* + subdirectories',
|
1034 | 915b99b5 | Stavros Sachtouris | ('-R', '--recursive')) |
1035 | 2fe2672e | Stavros Sachtouris | ) |
1036 | 7493ccb6 | Stavros Sachtouris | |
1037 | 14b25e00 | Stavros Sachtouris | def _check_container_limit(self, path): |
1038 | 14b25e00 | Stavros Sachtouris | cl_dict = self.client.get_container_limit()
|
1039 | 14b25e00 | Stavros Sachtouris | container_limit = int(cl_dict['x-container-policy-quota']) |
1040 | 2b74fc1d | Stavros Sachtouris | r = self.client.container_get()
|
1041 | 2b74fc1d | Stavros Sachtouris | used_bytes = sum(int(o['bytes']) for o in r.json) |
1042 | 14b25e00 | Stavros Sachtouris | path_size = get_path_size(path) |
1043 | c8d8c6c9 | Stavros Sachtouris | if container_limit and path_size > (container_limit - used_bytes): |
1044 | 2b74fc1d | Stavros Sachtouris | raiseCLIError( |
1045 | 2b74fc1d | Stavros Sachtouris | 'Container(%s) (limit(%s) - used(%s)) < size(%s) of %s' % (
|
1046 | 14b25e00 | Stavros Sachtouris | self.client.container,
|
1047 | 14b25e00 | Stavros Sachtouris | format_size(container_limit), |
1048 | 2b74fc1d | Stavros Sachtouris | format_size(used_bytes), |
1049 | 14b25e00 | Stavros Sachtouris | format_size(path_size), |
1050 | 14b25e00 | Stavros Sachtouris | path), |
1051 | 14b25e00 | Stavros Sachtouris | importance=1, details=[
|
1052 | 14b25e00 | Stavros Sachtouris | 'Check accound limit: /file quota',
|
1053 | 14b25e00 | Stavros Sachtouris | 'Check container limit:',
|
1054 | 14b25e00 | Stavros Sachtouris | '\t/file containerlimit get %s' % self.client.container, |
1055 | 14b25e00 | Stavros Sachtouris | 'Increase container limit:',
|
1056 | 14b25e00 | Stavros Sachtouris | '\t/file containerlimit set <new limit> %s' % (
|
1057 | 14b25e00 | Stavros Sachtouris | self.client.container)])
|
1058 | 14b25e00 | Stavros Sachtouris | |
1059 | b666ef82 | Stavros Sachtouris | def _path_pairs(self, local_path, remote_path): |
1060 | b666ef82 | Stavros Sachtouris | """Get pairs of local and remote paths"""
|
1061 | b666ef82 | Stavros Sachtouris | lpath = path.abspath(local_path) |
1062 | b666ef82 | Stavros Sachtouris | short_path = lpath.split(path.sep)[-1]
|
1063 | b666ef82 | Stavros Sachtouris | rpath = remote_path or short_path
|
1064 | b666ef82 | Stavros Sachtouris | if path.isdir(lpath):
|
1065 | 2d158d8b | Stavros Sachtouris | if not self['recursive']: |
1066 | 2d158d8b | Stavros Sachtouris | raiseCLIError('%s is a directory' % lpath, details=[
|
1067 | 2d158d8b | Stavros Sachtouris | 'Use -R to upload directory contents'])
|
1068 | b666ef82 | Stavros Sachtouris | robj = self.client.container_get(path=rpath)
|
1069 | b666ef82 | Stavros Sachtouris | if robj.json and not self['overwrite']: |
1070 | b666ef82 | Stavros Sachtouris | raiseCLIError( |
1071 | b666ef82 | Stavros Sachtouris | 'Objects prefixed with %s already exist' % rpath,
|
1072 | b666ef82 | Stavros Sachtouris | importance=1,
|
1073 | b666ef82 | Stavros Sachtouris | details=['Existing objects:'] + ['\t%s:\t%s' % ( |
1074 | b666ef82 | Stavros Sachtouris | o['content_type'][12:], |
1075 | b666ef82 | Stavros Sachtouris | o['name']) for o in robj.json] + [ |
1076 | b666ef82 | Stavros Sachtouris | 'Use -f to add, overwrite or resume'])
|
1077 | b666ef82 | Stavros Sachtouris | if not self['overwrite']: |
1078 | b666ef82 | Stavros Sachtouris | try:
|
1079 | b666ef82 | Stavros Sachtouris | topobj = self.client.get_object_info(rpath)
|
1080 | b666ef82 | Stavros Sachtouris | if not self._is_dir(topobj): |
1081 | b666ef82 | Stavros Sachtouris | raiseCLIError( |
1082 | b666ef82 | Stavros Sachtouris | 'Object %s exists but it is not a dir' % rpath,
|
1083 | b666ef82 | Stavros Sachtouris | importance=1, details=['Use -f to overwrite']) |
1084 | b666ef82 | Stavros Sachtouris | except ClientError as ce: |
1085 | b666ef82 | Stavros Sachtouris | if ce.status != 404: |
1086 | b666ef82 | Stavros Sachtouris | raise
|
1087 | 2d158d8b | Stavros Sachtouris | self._check_container_limit(lpath)
|
1088 | b666ef82 | Stavros Sachtouris | prev = ''
|
1089 | b666ef82 | Stavros Sachtouris | for top, subdirs, files in walk(lpath): |
1090 | b666ef82 | Stavros Sachtouris | if top != prev:
|
1091 | b666ef82 | Stavros Sachtouris | prev = top |
1092 | b666ef82 | Stavros Sachtouris | try:
|
1093 | b666ef82 | Stavros Sachtouris | rel_path = rpath + top.split(lpath)[1]
|
1094 | b666ef82 | Stavros Sachtouris | except IndexError: |
1095 | b666ef82 | Stavros Sachtouris | rel_path = rpath |
1096 | b666ef82 | Stavros Sachtouris | print('mkdir %s:%s' % (self.client.container, rel_path)) |
1097 | b666ef82 | Stavros Sachtouris | self.client.create_directory(rel_path)
|
1098 | b666ef82 | Stavros Sachtouris | for f in files: |
1099 | b666ef82 | Stavros Sachtouris | fpath = path.join(top, f) |
1100 | b666ef82 | Stavros Sachtouris | if path.isfile(fpath):
|
1101 | 14d15eca | Stavros Sachtouris | rel_path = rel_path.replace(path.sep, '/')
|
1102 | 14d15eca | Stavros Sachtouris | pathfix = f.replace(path.sep, '/')
|
1103 | 14d15eca | Stavros Sachtouris | yield open(fpath, 'rb'), '%s/%s' % (rel_path, pathfix) |
1104 | b666ef82 | Stavros Sachtouris | else:
|
1105 | b666ef82 | Stavros Sachtouris | print('%s is not a regular file' % fpath)
|
1106 | b666ef82 | Stavros Sachtouris | else:
|
1107 | 14b25e00 | Stavros Sachtouris | if not path.isfile(lpath): |
1108 | 9d3cd179 | Stavros Sachtouris | raiseCLIError(('%s is not a regular file' % lpath) if ( |
1109 | a0a61f30 | Stavros Sachtouris | path.exists(lpath)) else '%s does not exist' % lpath) |
1110 | b666ef82 | Stavros Sachtouris | try:
|
1111 | b666ef82 | Stavros Sachtouris | robj = self.client.get_object_info(rpath)
|
1112 | b666ef82 | Stavros Sachtouris | if remote_path and self._is_dir(robj): |
1113 | 14d15eca | Stavros Sachtouris | rpath += '/%s' % (short_path.replace(path.sep, '/')) |
1114 | b666ef82 | Stavros Sachtouris | self.client.get_object_info(rpath)
|
1115 | b666ef82 | Stavros Sachtouris | if not self['overwrite']: |
1116 | b666ef82 | Stavros Sachtouris | raiseCLIError( |
1117 | b666ef82 | Stavros Sachtouris | 'Object %s already exists' % rpath,
|
1118 | b666ef82 | Stavros Sachtouris | importance=1,
|
1119 | b666ef82 | Stavros Sachtouris | details=['use -f to overwrite or resume'])
|
1120 | b666ef82 | Stavros Sachtouris | except ClientError as ce: |
1121 | b666ef82 | Stavros Sachtouris | if ce.status != 404: |
1122 | b666ef82 | Stavros Sachtouris | raise
|
1123 | 2d158d8b | Stavros Sachtouris | self._check_container_limit(lpath)
|
1124 | b666ef82 | Stavros Sachtouris | yield open(lpath, 'rb'), rpath |
1125 | 706fc940 | Stavros Sachtouris | |
1126 | 68858765 | Stavros Sachtouris | @errors.generic.all
|
1127 | 68858765 | Stavros Sachtouris | @errors.pithos.connection
|
1128 | 68858765 | Stavros Sachtouris | @errors.pithos.container
|
1129 | 68858765 | Stavros Sachtouris | @errors.pithos.object_path
|
1130 | 68858765 | Stavros Sachtouris | @errors.pithos.local_path
|
1131 | 68858765 | Stavros Sachtouris | def _run(self, local_path, remote_path): |
1132 | 2fe2672e | Stavros Sachtouris | poolsize = self['poolsize'] |
1133 | 72952f4f | Stavros Sachtouris | if poolsize > 0: |
1134 | 16b0afe6 | Stavros Sachtouris | self.client.MAX_THREADS = int(poolsize) |
1135 | 72952f4f | Stavros Sachtouris | params = dict(
|
1136 | 72952f4f | Stavros Sachtouris | content_encoding=self['content_encoding'], |
1137 | 2fe2672e | Stavros Sachtouris | content_type=self['content_type'], |
1138 | 2fe2672e | Stavros Sachtouris | content_disposition=self['content_disposition'], |
1139 | 2fe2672e | Stavros Sachtouris | sharing=self['sharing'], |
1140 | 2fe2672e | Stavros Sachtouris | public=self['public']) |
1141 | 0e728dcb | Stavros Sachtouris | uploaded = [] |
1142 | 74c65e80 | Stavros Sachtouris | container_info_cache = dict()
|
1143 | b666ef82 | Stavros Sachtouris | for f, rpath in self._path_pairs(local_path, remote_path): |
1144 | b666ef82 | Stavros Sachtouris | print('%s --> %s:%s' % (f.name, self.client.container, rpath)) |
1145 | f8426b5c | Stavros Sachtouris | if not (self['content_type'] and self['content_encoding']): |
1146 | f8426b5c | Stavros Sachtouris | ctype, cenc = guess_mime_type(f.name) |
1147 | f8426b5c | Stavros Sachtouris | params['content_type'] = self['content_type'] or ctype |
1148 | f8426b5c | Stavros Sachtouris | params['content_encoding'] = self['content_encoding'] or cenc |
1149 | 68858765 | Stavros Sachtouris | if self['unchunked']: |
1150 | 0e728dcb | Stavros Sachtouris | r = self.client.upload_object_unchunked(
|
1151 | b666ef82 | Stavros Sachtouris | rpath, f, |
1152 | b666ef82 | Stavros Sachtouris | etag=self['etag'], withHashFile=self['use_hashes'], |
1153 | 68858765 | Stavros Sachtouris | **params) |
1154 | 5655d560 | Stavros Sachtouris | if self['with_output'] or self['json_output']: |
1155 | 0e728dcb | Stavros Sachtouris | r['name'] = '%s: %s' % (self.client.container, rpath) |
1156 | 0e728dcb | Stavros Sachtouris | uploaded.append(r) |
1157 | 68858765 | Stavros Sachtouris | else:
|
1158 | 68858765 | Stavros Sachtouris | try:
|
1159 | 68858765 | Stavros Sachtouris | (progress_bar, upload_cb) = self._safe_progress_bar(
|
1160 | b666ef82 | Stavros Sachtouris | 'Uploading %s' % f.name.split(path.sep)[-1]) |
1161 | 68858765 | Stavros Sachtouris | if progress_bar:
|
1162 | 68858765 | Stavros Sachtouris | hash_bar = progress_bar.clone() |
1163 | 68858765 | Stavros Sachtouris | hash_cb = hash_bar.get_generator( |
1164 | 278c9018 | Stavros Sachtouris | 'Calculating block hashes')
|
1165 | 68858765 | Stavros Sachtouris | else:
|
1166 | 68858765 | Stavros Sachtouris | hash_cb = None
|
1167 | 0e728dcb | Stavros Sachtouris | r = self.client.upload_object(
|
1168 | b666ef82 | Stavros Sachtouris | rpath, f, |
1169 | 74c65e80 | Stavros Sachtouris | hash_cb=hash_cb, |
1170 | 74c65e80 | Stavros Sachtouris | upload_cb=upload_cb, |
1171 | 74c65e80 | Stavros Sachtouris | container_info_cache=container_info_cache, |
1172 | 852a22e7 | Stavros Sachtouris | **params) |
1173 | 5655d560 | Stavros Sachtouris | if self['with_output'] or self['json_output']: |
1174 | 0e728dcb | Stavros Sachtouris | r['name'] = '%s: %s' % (self.client.container, rpath) |
1175 | 0e728dcb | Stavros Sachtouris | uploaded.append(r) |
1176 | 68858765 | Stavros Sachtouris | except Exception: |
1177 | 68858765 | Stavros Sachtouris | self._safe_progress_bar_finish(progress_bar)
|
1178 | 68858765 | Stavros Sachtouris | raise
|
1179 | 68858765 | Stavros Sachtouris | finally:
|
1180 | 68858765 | Stavros Sachtouris | self._safe_progress_bar_finish(progress_bar)
|
1181 | d9301a7a | Stavros Sachtouris | self._optional_output(uploaded)
|
1182 | 915b99b5 | Stavros Sachtouris | print('Upload completed')
|
1183 | 7493ccb6 | Stavros Sachtouris | |
1184 | 68858765 | Stavros Sachtouris | def main(self, local_path, container____path__=None): |
1185 | 68858765 | Stavros Sachtouris | super(self.__class__, self)._run(container____path__) |
1186 | 9e0a1a4c | Stavros Sachtouris | remote_path = self.path or path.basename(path.abspath(local_path)) |
1187 | 68858765 | Stavros Sachtouris | self._run(local_path=local_path, remote_path=remote_path)
|
1188 | 68858765 | Stavros Sachtouris | |
1189 | 234954d1 | Stavros Sachtouris | |
1190 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1191 | 3ae60112 | Stavros Sachtouris | class file_cat(_file_container_command): |
1192 | 72952f4f | Stavros Sachtouris | """Print remote file contents to console"""
|
1193 | 9ceec15a | Stavros Sachtouris | |
1194 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
1195 | 2fe2672e | Stavros Sachtouris | range=RangeArgument('show range of data', '--range'), |
1196 | 2fe2672e | Stavros Sachtouris | if_match=ValueArgument('show output if ETags match', '--if-match'), |
1197 | 2fe2672e | Stavros Sachtouris | if_none_match=ValueArgument( |
1198 | 201baa17 | Stavros Sachtouris | 'show output if ETags match', '--if-none-match'), |
1199 | 2fe2672e | Stavros Sachtouris | if_modified_since=DateArgument( |
1200 | 201baa17 | Stavros Sachtouris | 'show output modified since then', '--if-modified-since'), |
1201 | 2fe2672e | Stavros Sachtouris | if_unmodified_since=DateArgument( |
1202 | 201baa17 | Stavros Sachtouris | 'show output unmodified since then', '--if-unmodified-since'), |
1203 | 2fe2672e | Stavros Sachtouris | object_version=ValueArgument( |
1204 | 201baa17 | Stavros Sachtouris | 'get the specific version', ('-O', '--object-version')) |
1205 | 2fe2672e | Stavros Sachtouris | ) |
1206 | 2fe2672e | Stavros Sachtouris | |
1207 | 68858765 | Stavros Sachtouris | @errors.generic.all
|
1208 | 68858765 | Stavros Sachtouris | @errors.pithos.connection
|
1209 | 68858765 | Stavros Sachtouris | @errors.pithos.container
|
1210 | 68858765 | Stavros Sachtouris | @errors.pithos.object_path
|
1211 | 68858765 | Stavros Sachtouris | def _run(self): |
1212 | 68858765 | Stavros Sachtouris | self.client.download_object(
|
1213 | 68858765 | Stavros Sachtouris | self.path,
|
1214 | 68858765 | Stavros Sachtouris | stdout, |
1215 | 3d568c09 | Stavros Sachtouris | range_str=self['range'], |
1216 | 2fe2672e | Stavros Sachtouris | version=self['object_version'], |
1217 | 2fe2672e | Stavros Sachtouris | if_match=self['if_match'], |
1218 | 2fe2672e | Stavros Sachtouris | if_none_match=self['if_none_match'], |
1219 | 2fe2672e | Stavros Sachtouris | if_modified_since=self['if_modified_since'], |
1220 | 2fe2672e | Stavros Sachtouris | if_unmodified_since=self['if_unmodified_since']) |
1221 | 68858765 | Stavros Sachtouris | |
1222 | 68858765 | Stavros Sachtouris | def main(self, container___path): |
1223 | 68858765 | Stavros Sachtouris | super(self.__class__, self)._run( |
1224 | 201baa17 | Stavros Sachtouris | container___path, path_is_optional=False)
|
1225 | 68858765 | Stavros Sachtouris | self._run()
|
1226 | 9ceec15a | Stavros Sachtouris | |
1227 | 234954d1 | Stavros Sachtouris | |
1228 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1229 | 3ae60112 | Stavros Sachtouris | class file_download(_file_container_command): |
1230 | ae99b37d | Stavros Sachtouris | """Download remote object as local file
|
1231 | ae99b37d | Stavros Sachtouris | If local destination is a directory:
|
1232 | f40f0cb7 | Stavros Sachtouris | * download <container>:<path> <local dir> -R
|
1233 | ae99b37d | Stavros Sachtouris | will download all files on <container> prefixed as <path>,
|
1234 | c626151a | Stavros Sachtouris | to <local dir>/<full path> (or <local dir>\<full path> in windows)
|
1235 | ae99b37d | Stavros Sachtouris | * download <container>:<path> <local dir> --exact-match
|
1236 | ae99b37d | Stavros Sachtouris | will download only one file, exactly matching <path>
|
1237 | f6c09d14 | Stavros Sachtouris | ATTENTION: to download cont:dir1/dir2/file there must exist objects
|
1238 | f6c09d14 | Stavros Sachtouris | cont:dir1 and cont:dir1/dir2 of type application/directory
|
1239 | 3ae60112 | Stavros Sachtouris | To create directory objects, use /file mkdir
|
1240 | ae99b37d | Stavros Sachtouris | """
|
1241 | 7493ccb6 | Stavros Sachtouris | |
1242 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
1243 | f40f0cb7 | Stavros Sachtouris | resume=FlagArgument('Resume instead of overwrite', ('-r', '--resume')), |
1244 | 2fe2672e | Stavros Sachtouris | range=RangeArgument('show range of data', '--range'), |
1245 | 2fe2672e | Stavros Sachtouris | if_match=ValueArgument('show output if ETags match', '--if-match'), |
1246 | 2fe2672e | Stavros Sachtouris | if_none_match=ValueArgument( |
1247 | 201baa17 | Stavros Sachtouris | 'show output if ETags match', '--if-none-match'), |
1248 | 2fe2672e | Stavros Sachtouris | if_modified_since=DateArgument( |
1249 | 201baa17 | Stavros Sachtouris | 'show output modified since then', '--if-modified-since'), |
1250 | 2fe2672e | Stavros Sachtouris | if_unmodified_since=DateArgument( |
1251 | 201baa17 | Stavros Sachtouris | 'show output unmodified since then', '--if-unmodified-since'), |
1252 | 2fe2672e | Stavros Sachtouris | object_version=ValueArgument( |
1253 | 201baa17 | Stavros Sachtouris | 'get the specific version', ('-O', '--object-version')), |
1254 | 2fe2672e | Stavros Sachtouris | poolsize=IntArgument('set pool size', '--with-pool-size'), |
1255 | 2fe2672e | Stavros Sachtouris | progress_bar=ProgressBarArgument( |
1256 | 2fe2672e | Stavros Sachtouris | 'do not show progress bar',
|
1257 | f40f0cb7 | Stavros Sachtouris | ('-N', '--no-progress-bar'), |
1258 | ae99b37d | Stavros Sachtouris | default=False),
|
1259 | f6c09d14 | Stavros Sachtouris | recursive=FlagArgument( |
1260 | a3ba3bce | Stavros Sachtouris | 'Download a remote path and all its contents',
|
1261 | f40f0cb7 | Stavros Sachtouris | ('-R', '--recursive')) |
1262 | 2fe2672e | Stavros Sachtouris | ) |
1263 | 7493ccb6 | Stavros Sachtouris | |
1264 | f6c09d14 | Stavros Sachtouris | def _outputs(self, local_path): |
1265 | a3ba3bce | Stavros Sachtouris | """:returns: (local_file, remote_path)"""
|
1266 | a3ba3bce | Stavros Sachtouris | remotes = [] |
1267 | a3ba3bce | Stavros Sachtouris | if self['recursive']: |
1268 | a3ba3bce | Stavros Sachtouris | r = self.client.container_get(
|
1269 | a3ba3bce | Stavros Sachtouris | prefix=self.path or '/', |
1270 | f6c09d14 | Stavros Sachtouris | if_modified_since=self['if_modified_since'], |
1271 | f6c09d14 | Stavros Sachtouris | if_unmodified_since=self['if_unmodified_since']) |
1272 | a3ba3bce | Stavros Sachtouris | dirlist = dict()
|
1273 | a3ba3bce | Stavros Sachtouris | for remote in r.json: |
1274 | a3ba3bce | Stavros Sachtouris | rname = remote['name'].strip('/') |
1275 | a3ba3bce | Stavros Sachtouris | tmppath = ''
|
1276 | a3ba3bce | Stavros Sachtouris | for newdir in rname.strip('/').split('/')[:-1]: |
1277 | a3ba3bce | Stavros Sachtouris | tmppath = '/'.join([tmppath, newdir])
|
1278 | a3ba3bce | Stavros Sachtouris | dirlist.update({tmppath.strip('/'): True}) |
1279 | 3ae60112 | Stavros Sachtouris | remotes.append((rname, file_download._is_dir(remote))) |
1280 | a3ba3bce | Stavros Sachtouris | dir_remotes = [r[0] for r in remotes if r[1]] |
1281 | a3ba3bce | Stavros Sachtouris | if not set(dirlist).issubset(dir_remotes): |
1282 | a3ba3bce | Stavros Sachtouris | badguys = [bg.strip('/') for bg in set( |
1283 | a3ba3bce | Stavros Sachtouris | dirlist).difference(dir_remotes)] |
1284 | a3ba3bce | Stavros Sachtouris | raiseCLIError( |
1285 | a3ba3bce | Stavros Sachtouris | 'Some remote paths contain non existing directories',
|
1286 | a3ba3bce | Stavros Sachtouris | details=['Missing remote directories:'] + badguys)
|
1287 | a3ba3bce | Stavros Sachtouris | elif self.path: |
1288 | a3ba3bce | Stavros Sachtouris | r = self.client.get_object_info(
|
1289 | a3ba3bce | Stavros Sachtouris | self.path,
|
1290 | a3ba3bce | Stavros Sachtouris | version=self['object_version']) |
1291 | 3ae60112 | Stavros Sachtouris | if file_download._is_dir(r):
|
1292 | a3ba3bce | Stavros Sachtouris | raiseCLIError( |
1293 | a3ba3bce | Stavros Sachtouris | 'Illegal download: Remote object %s is a directory' % (
|
1294 | a3ba3bce | Stavros Sachtouris | self.path),
|
1295 | 14d15eca | Stavros Sachtouris | details=['To download a directory, try --recursive or -R'])
|
1296 | a3ba3bce | Stavros Sachtouris | if '/' in self.path.strip('/') and not local_path: |
1297 | a3ba3bce | Stavros Sachtouris | raiseCLIError( |
1298 | a3ba3bce | Stavros Sachtouris | 'Illegal download: remote object %s contains "/"' % (
|
1299 | a3ba3bce | Stavros Sachtouris | self.path),
|
1300 | a3ba3bce | Stavros Sachtouris | details=[ |
1301 | a3ba3bce | Stavros Sachtouris | 'To download an object containing "/" characters',
|
1302 | a3ba3bce | Stavros Sachtouris | 'either create the remote directories or',
|
1303 | a3ba3bce | Stavros Sachtouris | 'specify a non-directory local path for this object'])
|
1304 | a3ba3bce | Stavros Sachtouris | remotes = [(self.path, False)] |
1305 | a3ba3bce | Stavros Sachtouris | if not remotes: |
1306 | a3ba3bce | Stavros Sachtouris | if self.path: |
1307 | a3ba3bce | Stavros Sachtouris | raiseCLIError( |
1308 | a3ba3bce | Stavros Sachtouris | 'No matching path %s on container %s' % (
|
1309 | 14d15eca | Stavros Sachtouris | self.path, self.container), |
1310 | a3ba3bce | Stavros Sachtouris | details=[ |
1311 | a3ba3bce | Stavros Sachtouris | 'To list the contents of %s, try:' % self.container, |
1312 | 3ae60112 | Stavros Sachtouris | ' /file list %s' % self.container]) |
1313 | a3ba3bce | Stavros Sachtouris | raiseCLIError( |
1314 | a3ba3bce | Stavros Sachtouris | 'Illegal download of container %s' % self.container, |
1315 | a3ba3bce | Stavros Sachtouris | details=[ |
1316 | a3ba3bce | Stavros Sachtouris | 'To download a whole container, try:',
|
1317 | 3ae60112 | Stavros Sachtouris | ' /file download --recursive <container>'])
|
1318 | a3ba3bce | Stavros Sachtouris | |
1319 | a3ba3bce | Stavros Sachtouris | lprefix = path.abspath(local_path or path.curdir)
|
1320 | a3ba3bce | Stavros Sachtouris | if path.isdir(lprefix):
|
1321 | a3ba3bce | Stavros Sachtouris | for rpath, remote_is_dir in remotes: |
1322 | 14d15eca | Stavros Sachtouris | lpath = path.sep.join([ |
1323 | 14d15eca | Stavros Sachtouris | lprefix[:-1] if lprefix.endswith(path.sep) else lprefix, |
1324 | 14d15eca | Stavros Sachtouris | rpath.strip('/').replace('/', path.sep)]) |
1325 | a3ba3bce | Stavros Sachtouris | if remote_is_dir:
|
1326 | a3ba3bce | Stavros Sachtouris | if path.exists(lpath) and path.isdir(lpath): |
1327 | a3ba3bce | Stavros Sachtouris | continue
|
1328 | a3ba3bce | Stavros Sachtouris | makedirs(lpath) |
1329 | a3ba3bce | Stavros Sachtouris | elif path.exists(lpath):
|
1330 | a3ba3bce | Stavros Sachtouris | if not self['resume']: |
1331 | a3ba3bce | Stavros Sachtouris | print('File %s exists, aborting...' % lpath)
|
1332 | a3ba3bce | Stavros Sachtouris | continue
|
1333 | a3ba3bce | Stavros Sachtouris | with open(lpath, 'rwb+') as f: |
1334 | a3ba3bce | Stavros Sachtouris | yield (f, rpath)
|
1335 | a3ba3bce | Stavros Sachtouris | else:
|
1336 | a3ba3bce | Stavros Sachtouris | with open(lpath, 'wb+') as f: |
1337 | a3ba3bce | Stavros Sachtouris | yield (f, rpath)
|
1338 | a3ba3bce | Stavros Sachtouris | elif path.exists(lprefix):
|
1339 | a3ba3bce | Stavros Sachtouris | if len(remotes) > 1: |
1340 | a3ba3bce | Stavros Sachtouris | raiseCLIError( |
1341 | a3ba3bce | Stavros Sachtouris | '%s remote objects cannot be merged in local file %s' % (
|
1342 | a3ba3bce | Stavros Sachtouris | len(remotes),
|
1343 | a3ba3bce | Stavros Sachtouris | local_path), |
1344 | a3ba3bce | Stavros Sachtouris | details=[ |
1345 | a3ba3bce | Stavros Sachtouris | 'To download multiple objects, local path should be',
|
1346 | a3ba3bce | Stavros Sachtouris | 'a directory, or use download without a local path'])
|
1347 | a3ba3bce | Stavros Sachtouris | (rpath, remote_is_dir) = remotes[0]
|
1348 | a3ba3bce | Stavros Sachtouris | if remote_is_dir:
|
1349 | a3ba3bce | Stavros Sachtouris | raiseCLIError( |
1350 | a3ba3bce | Stavros Sachtouris | 'Remote directory %s should not replace local file %s' % (
|
1351 | a3ba3bce | Stavros Sachtouris | rpath, |
1352 | a3ba3bce | Stavros Sachtouris | local_path)) |
1353 | a3ba3bce | Stavros Sachtouris | if self['resume']: |
1354 | a3ba3bce | Stavros Sachtouris | with open(lprefix, 'rwb+') as f: |
1355 | a3ba3bce | Stavros Sachtouris | yield (f, rpath)
|
1356 | a3ba3bce | Stavros Sachtouris | else:
|
1357 | a3ba3bce | Stavros Sachtouris | raiseCLIError( |
1358 | db36a6a7 | Stavros Sachtouris | 'Local file %s already exist' % local_path,
|
1359 | db36a6a7 | Stavros Sachtouris | details=['Try --resume to overwrite it'])
|
1360 | a3ba3bce | Stavros Sachtouris | else:
|
1361 | a3ba3bce | Stavros Sachtouris | if len(remotes) > 1 or remotes[0][1]: |
1362 | a3ba3bce | Stavros Sachtouris | raiseCLIError( |
1363 | a3ba3bce | Stavros Sachtouris | 'Local directory %s does not exist' % local_path)
|
1364 | a3ba3bce | Stavros Sachtouris | with open(lprefix, 'wb+') as f: |
1365 | a3ba3bce | Stavros Sachtouris | yield (f, remotes[0][0]) |
1366 | 7493ccb6 | Stavros Sachtouris | |
1367 | 68858765 | Stavros Sachtouris | @errors.generic.all
|
1368 | 68858765 | Stavros Sachtouris | @errors.pithos.connection
|
1369 | 68858765 | Stavros Sachtouris | @errors.pithos.container
|
1370 | 68858765 | Stavros Sachtouris | @errors.pithos.object_path
|
1371 | 68858765 | Stavros Sachtouris | @errors.pithos.local_path
|
1372 | 68858765 | Stavros Sachtouris | def _run(self, local_path): |
1373 | 2fe2672e | Stavros Sachtouris | poolsize = self['poolsize'] |
1374 | 68858765 | Stavros Sachtouris | if poolsize:
|
1375 | 16b0afe6 | Stavros Sachtouris | self.client.MAX_THREADS = int(poolsize) |
1376 | f6c09d14 | Stavros Sachtouris | progress_bar = None
|
1377 | ae99b37d | Stavros Sachtouris | try:
|
1378 | a3ba3bce | Stavros Sachtouris | for f, rpath in self._outputs(local_path): |
1379 | 3d568c09 | Stavros Sachtouris | ( |
1380 | 3d568c09 | Stavros Sachtouris | progress_bar, |
1381 | a3ba3bce | Stavros Sachtouris | download_cb) = self._safe_progress_bar(
|
1382 | a3ba3bce | Stavros Sachtouris | 'Download %s' % rpath)
|
1383 | ae99b37d | Stavros Sachtouris | self.client.download_object(
|
1384 | 5655d560 | Stavros Sachtouris | rpath, f, |
1385 | ae99b37d | Stavros Sachtouris | download_cb=download_cb, |
1386 | 3d568c09 | Stavros Sachtouris | range_str=self['range'], |
1387 | ae99b37d | Stavros Sachtouris | version=self['object_version'], |
1388 | ae99b37d | Stavros Sachtouris | if_match=self['if_match'], |
1389 | ae99b37d | Stavros Sachtouris | resume=self['resume'], |
1390 | ae99b37d | Stavros Sachtouris | if_none_match=self['if_none_match'], |
1391 | ae99b37d | Stavros Sachtouris | if_modified_since=self['if_modified_since'], |
1392 | ae99b37d | Stavros Sachtouris | if_unmodified_since=self['if_unmodified_since']) |
1393 | 7493ccb6 | Stavros Sachtouris | except KeyboardInterrupt: |
1394 | b78ee581 | Stavros Sachtouris | from threading import activeCount, enumerate as activethreads |
1395 | cae76f25 | Stavros Sachtouris | timeout = 0.5
|
1396 | b78ee581 | Stavros Sachtouris | while activeCount() > 1: |
1397 | cae76f25 | Stavros Sachtouris | stdout.write('\nCancel %s threads: ' % (activeCount() - 1)) |
1398 | cae76f25 | Stavros Sachtouris | stdout.flush() |
1399 | b78ee581 | Stavros Sachtouris | for thread in activethreads(): |
1400 | b78ee581 | Stavros Sachtouris | try:
|
1401 | cae76f25 | Stavros Sachtouris | thread.join(timeout) |
1402 | cae76f25 | Stavros Sachtouris | stdout.write('.' if thread.isAlive() else '*') |
1403 | b78ee581 | Stavros Sachtouris | except RuntimeError: |
1404 | b78ee581 | Stavros Sachtouris | continue
|
1405 | cae76f25 | Stavros Sachtouris | finally:
|
1406 | cae76f25 | Stavros Sachtouris | stdout.flush() |
1407 | cae76f25 | Stavros Sachtouris | timeout += 0.1
|
1408 | b78ee581 | Stavros Sachtouris | print('\nDownload canceled by user')
|
1409 | 7493ccb6 | Stavros Sachtouris | if local_path is not None: |
1410 | 624ee36f | Stavros Sachtouris | print('to resume, re-run with --resume')
|
1411 | 68858765 | Stavros Sachtouris | except Exception: |
1412 | 68858765 | Stavros Sachtouris | self._safe_progress_bar_finish(progress_bar)
|
1413 | 68858765 | Stavros Sachtouris | raise
|
1414 | 68858765 | Stavros Sachtouris | finally:
|
1415 | 68858765 | Stavros Sachtouris | self._safe_progress_bar_finish(progress_bar)
|
1416 | 68858765 | Stavros Sachtouris | |
1417 | 68858765 | Stavros Sachtouris | def main(self, container___path, local_path=None): |
1418 | a3ba3bce | Stavros Sachtouris | super(self.__class__, self)._run(container___path) |
1419 | 68858765 | Stavros Sachtouris | self._run(local_path=local_path)
|
1420 | 7493ccb6 | Stavros Sachtouris | |
1421 | 234954d1 | Stavros Sachtouris | |
1422 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1423 | 545c6c29 | Stavros Sachtouris | class file_hashmap(_file_container_command, _optional_json): |
1424 | 72952f4f | Stavros Sachtouris | """Get the hash-map of an object"""
|
1425 | 7493ccb6 | Stavros Sachtouris | |
1426 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
1427 | 2fe2672e | Stavros Sachtouris | if_match=ValueArgument('show output if ETags match', '--if-match'), |
1428 | 2fe2672e | Stavros Sachtouris | if_none_match=ValueArgument( |
1429 | 545c6c29 | Stavros Sachtouris | 'show output if ETags match', '--if-none-match'), |
1430 | 2fe2672e | Stavros Sachtouris | if_modified_since=DateArgument( |
1431 | 545c6c29 | Stavros Sachtouris | 'show output modified since then', '--if-modified-since'), |
1432 | 2fe2672e | Stavros Sachtouris | if_unmodified_since=DateArgument( |
1433 | 545c6c29 | Stavros Sachtouris | 'show output unmodified since then', '--if-unmodified-since'), |
1434 | 2fe2672e | Stavros Sachtouris | object_version=ValueArgument( |
1435 | 545c6c29 | Stavros Sachtouris | 'get the specific version', ('-O', '--object-version')) |
1436 | 2fe2672e | Stavros Sachtouris | ) |
1437 | 7493ccb6 | Stavros Sachtouris | |
1438 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1439 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1440 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1441 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1442 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1443 | 545c6c29 | Stavros Sachtouris | self._print(self.client.get_object_hashmap( |
1444 | b4cf92b8 | Stavros Sachtouris | self.path,
|
1445 | b4cf92b8 | Stavros Sachtouris | version=self['object_version'], |
1446 | b4cf92b8 | Stavros Sachtouris | if_match=self['if_match'], |
1447 | b4cf92b8 | Stavros Sachtouris | if_none_match=self['if_none_match'], |
1448 | b4cf92b8 | Stavros Sachtouris | if_modified_since=self['if_modified_since'], |
1449 | 545c6c29 | Stavros Sachtouris | if_unmodified_since=self['if_unmodified_since']), print_dict) |
1450 | b4cf92b8 | Stavros Sachtouris | |
1451 | 7493ccb6 | Stavros Sachtouris | def main(self, container___path): |
1452 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run( |
1453 | 72952f4f | Stavros Sachtouris | container___path, |
1454 | 72952f4f | Stavros Sachtouris | path_is_optional=False)
|
1455 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1456 | 7493ccb6 | Stavros Sachtouris | |
1457 | 234954d1 | Stavros Sachtouris | |
1458 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1459 | 915b99b5 | Stavros Sachtouris | class file_delete(_file_container_command, _optional_output_cmd): |
1460 | 72952f4f | Stavros Sachtouris | """Delete a container [or an object]
|
1461 | 72952f4f | Stavros Sachtouris | How to delete a non-empty container:
|
1462 | 3ae60112 | Stavros Sachtouris | - empty the container: /file delete -R <container>
|
1463 | 3ae60112 | Stavros Sachtouris | - delete it: /file delete <container>
|
1464 | 72952f4f | Stavros Sachtouris | .
|
1465 | 72952f4f | Stavros Sachtouris | Semantics of directory deletion:
|
1466 | 3ae60112 | Stavros Sachtouris | .a preserve the contents: /file delete <container>:<directory>
|
1467 | 72952f4f | Stavros Sachtouris | . objects of the form dir/filename can exist with a dir object
|
1468 | 3ae60112 | Stavros Sachtouris | .b delete contents: /file delete -R <container>:<directory>
|
1469 | 72952f4f | Stavros Sachtouris | . all dir/* objects are affected, even if dir does not exist
|
1470 | 72952f4f | Stavros Sachtouris | .
|
1471 | 72952f4f | Stavros Sachtouris | To restore a deleted object OBJ in a container CONT:
|
1472 | 3ae60112 | Stavros Sachtouris | - get object versions: /file versions CONT:OBJ
|
1473 | 72952f4f | Stavros Sachtouris | . and choose the version to be restored
|
1474 | 3ae60112 | Stavros Sachtouris | - restore the object: /file copy --source-version=<version> CONT:OBJ OBJ
|
1475 | 72952f4f | Stavros Sachtouris | """
|
1476 | 7493ccb6 | Stavros Sachtouris | |
1477 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
1478 | 439826ec | Stavros Sachtouris | until=DateArgument('remove history until that date', '--until'), |
1479 | 7147e1ca | Stavros Sachtouris | yes=FlagArgument('Do not prompt for permission', '--yes'), |
1480 | 2fe2672e | Stavros Sachtouris | recursive=FlagArgument( |
1481 | 234954d1 | Stavros Sachtouris | 'empty dir or container and delete (if dir)',
|
1482 | 915b99b5 | Stavros Sachtouris | ('-R', '--recursive')) |
1483 | 2fe2672e | Stavros Sachtouris | ) |
1484 | 2fe2672e | Stavros Sachtouris | |
1485 | b4f69041 | Stavros Sachtouris | def __init__(self, arguments={}, auth_base=None, cloud=None): |
1486 | b4f69041 | Stavros Sachtouris | super(self.__class__, self).__init__(arguments, auth_base, cloud) |
1487 | 439826ec | Stavros Sachtouris | self['delimiter'] = DelimiterArgument( |
1488 | 2fe2672e | Stavros Sachtouris | self,
|
1489 | 234954d1 | Stavros Sachtouris | parsed_name='--delimiter',
|
1490 | 234954d1 | Stavros Sachtouris | help='delete objects prefixed with <object><delimiter>')
|
1491 | 7493ccb6 | Stavros Sachtouris | |
1492 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1493 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1494 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1495 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1496 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1497 | b4cf92b8 | Stavros Sachtouris | if self.path: |
1498 | 24ff0a35 | Stavros Sachtouris | if self['yes'] or ask_user( |
1499 | 24ff0a35 | Stavros Sachtouris | 'Delete %s:%s ?' % (self.container, self.path)): |
1500 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.del_object( |
1501 | b4cf92b8 | Stavros Sachtouris | self.path,
|
1502 | 915b99b5 | Stavros Sachtouris | until=self['until'], delimiter=self['delimiter'])) |
1503 | 7493ccb6 | Stavros Sachtouris | else:
|
1504 | b4cf92b8 | Stavros Sachtouris | print('Aborted')
|
1505 | b4cf92b8 | Stavros Sachtouris | else:
|
1506 | 24ff0a35 | Stavros Sachtouris | if self['recursive']: |
1507 | de73876b | Stavros Sachtouris | ask_msg = 'Delete container contents'
|
1508 | de73876b | Stavros Sachtouris | else:
|
1509 | de73876b | Stavros Sachtouris | ask_msg = 'Delete container'
|
1510 | b4cf92b8 | Stavros Sachtouris | if self['yes'] or ask_user('%s %s ?' % (ask_msg, self.container)): |
1511 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.del_container( |
1512 | 915b99b5 | Stavros Sachtouris | until=self['until'], delimiter=self['delimiter'])) |
1513 | b4cf92b8 | Stavros Sachtouris | else:
|
1514 | b4cf92b8 | Stavros Sachtouris | print('Aborted')
|
1515 | b4cf92b8 | Stavros Sachtouris | |
1516 | edab7ba7 | Stavros Sachtouris | def main(self, container____path__=None): |
1517 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run(container____path__) |
1518 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1519 | 7493ccb6 | Stavros Sachtouris | |
1520 | 234954d1 | Stavros Sachtouris | |
1521 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1522 | 915b99b5 | Stavros Sachtouris | class file_purge(_file_container_command, _optional_output_cmd): |
1523 | 72952f4f | Stavros Sachtouris | """Delete a container and release related data blocks
|
1524 | 72952f4f | Stavros Sachtouris | Non-empty containers can not purged.
|
1525 | 72952f4f | Stavros Sachtouris | To purge a container with content:
|
1526 | 3ae60112 | Stavros Sachtouris | . /file delete -R <container>
|
1527 | 72952f4f | Stavros Sachtouris | . objects are deleted, but data blocks remain on server
|
1528 | 3ae60112 | Stavros Sachtouris | . /file purge <container>
|
1529 | 72952f4f | Stavros Sachtouris | . container and data blocks are released and deleted
|
1530 | 2fe2672e | Stavros Sachtouris | """
|
1531 | 234954d1 | Stavros Sachtouris | |
1532 | 7147e1ca | Stavros Sachtouris | arguments = dict(
|
1533 | 7147e1ca | Stavros Sachtouris | yes=FlagArgument('Do not prompt for permission', '--yes'), |
1534 | 915b99b5 | Stavros Sachtouris | force=FlagArgument('purge even if not empty', ('-F', '--force')) |
1535 | 7147e1ca | Stavros Sachtouris | ) |
1536 | 7147e1ca | Stavros Sachtouris | |
1537 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1538 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1539 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1540 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1541 | b4cf92b8 | Stavros Sachtouris | if self['yes'] or ask_user('Purge container %s?' % self.container): |
1542 | be4a8ccd | Stavros Sachtouris | try:
|
1543 | 5655d560 | Stavros Sachtouris | r = self.client.purge_container()
|
1544 | be4a8ccd | Stavros Sachtouris | except ClientError as ce: |
1545 | be4a8ccd | Stavros Sachtouris | if ce.status in (409,): |
1546 | be4a8ccd | Stavros Sachtouris | if self['force']: |
1547 | be4a8ccd | Stavros Sachtouris | self.client.del_container(delimiter='/') |
1548 | 5655d560 | Stavros Sachtouris | r = self.client.purge_container()
|
1549 | be4a8ccd | Stavros Sachtouris | else:
|
1550 | be4a8ccd | Stavros Sachtouris | raiseCLIError(ce, details=['Try -F to force-purge'])
|
1551 | be4a8ccd | Stavros Sachtouris | else:
|
1552 | be4a8ccd | Stavros Sachtouris | raise
|
1553 | 915b99b5 | Stavros Sachtouris | self._optional_output(r)
|
1554 | b4cf92b8 | Stavros Sachtouris | else:
|
1555 | b4cf92b8 | Stavros Sachtouris | print('Aborted')
|
1556 | b4cf92b8 | Stavros Sachtouris | |
1557 | 52edad0a | Stavros Sachtouris | def main(self, container=None): |
1558 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run(container) |
1559 | 52edad0a | Stavros Sachtouris | if container and self.container != container: |
1560 | 52edad0a | Stavros Sachtouris | raiseCLIError('Invalid container name %s' % container, details=[
|
1561 | 52edad0a | Stavros Sachtouris | 'Did you mean "%s" ?' % self.container, |
1562 | 52edad0a | Stavros Sachtouris | 'Use --container for names containing :'])
|
1563 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1564 | 7493ccb6 | Stavros Sachtouris | |
1565 | 234954d1 | Stavros Sachtouris | |
1566 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1567 | 3ae60112 | Stavros Sachtouris | class file_publish(_file_container_command): |
1568 | 2fe2672e | Stavros Sachtouris | """Publish the object and print the public url"""
|
1569 | 7493ccb6 | Stavros Sachtouris | |
1570 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1571 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1572 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1573 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1574 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1575 | fa382f9e | Stavros Sachtouris | print self.client.publish_object(self.path) |
1576 | b4cf92b8 | Stavros Sachtouris | |
1577 | 7493ccb6 | Stavros Sachtouris | def main(self, container___path): |
1578 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run( |
1579 | 201baa17 | Stavros Sachtouris | container___path, path_is_optional=False)
|
1580 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1581 | 7493ccb6 | Stavros Sachtouris | |
1582 | 234954d1 | Stavros Sachtouris | |
1583 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1584 | 915b99b5 | Stavros Sachtouris | class file_unpublish(_file_container_command, _optional_output_cmd): |
1585 | 7493ccb6 | Stavros Sachtouris | """Unpublish an object"""
|
1586 | 7493ccb6 | Stavros Sachtouris | |
1587 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1588 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1589 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1590 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1591 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1592 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.unpublish_object(self.path)) |
1593 | b4cf92b8 | Stavros Sachtouris | |
1594 | 7493ccb6 | Stavros Sachtouris | def main(self, container___path): |
1595 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run( |
1596 | 201baa17 | Stavros Sachtouris | container___path, path_is_optional=False)
|
1597 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1598 | 7493ccb6 | Stavros Sachtouris | |
1599 | 234954d1 | Stavros Sachtouris | |
1600 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1601 | 5655d560 | Stavros Sachtouris | class file_permissions(_pithos_init): |
1602 | 5655d560 | Stavros Sachtouris | """Manage user and group accessibility for objects
|
1603 | 5655d560 | Stavros Sachtouris | Permissions are lists of users and user groups. There are read and write
|
1604 | 72952f4f | Stavros Sachtouris | permissions. Users and groups with write permission have also read
|
1605 | 72952f4f | Stavros Sachtouris | permission.
|
1606 | 72952f4f | Stavros Sachtouris | """
|
1607 | 7493ccb6 | Stavros Sachtouris | |
1608 | 5655d560 | Stavros Sachtouris | |
1609 | 545c6c29 | Stavros Sachtouris | def print_permissions(permissions_dict): |
1610 | 545c6c29 | Stavros Sachtouris | expected_keys = ('read', 'write') |
1611 | 545c6c29 | Stavros Sachtouris | if set(permissions_dict).issubset(expected_keys): |
1612 | 545c6c29 | Stavros Sachtouris | print_dict(permissions_dict) |
1613 | 545c6c29 | Stavros Sachtouris | else:
|
1614 | 545c6c29 | Stavros Sachtouris | invalid_keys = set(permissions_dict.keys()).difference(expected_keys)
|
1615 | 545c6c29 | Stavros Sachtouris | raiseCLIError( |
1616 | 545c6c29 | Stavros Sachtouris | 'Illegal permission keys: %s' % ', '.join(invalid_keys), |
1617 | 545c6c29 | Stavros Sachtouris | importance=1, details=[
|
1618 | 545c6c29 | Stavros Sachtouris | 'Valid permission types: %s' % ' '.join(expected_keys)]) |
1619 | 545c6c29 | Stavros Sachtouris | |
1620 | 545c6c29 | Stavros Sachtouris | |
1621 | 5655d560 | Stavros Sachtouris | @command(pithos_cmds)
|
1622 | 545c6c29 | Stavros Sachtouris | class file_permissions_get(_file_container_command, _optional_json): |
1623 | 5655d560 | Stavros Sachtouris | """Get read and write permissions of an object"""
|
1624 | 5655d560 | Stavros Sachtouris | |
1625 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1626 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1627 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1628 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1629 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1630 | 545c6c29 | Stavros Sachtouris | self._print(
|
1631 | 545c6c29 | Stavros Sachtouris | self.client.get_object_sharing(self.path), print_permissions) |
1632 | b4cf92b8 | Stavros Sachtouris | |
1633 | 7493ccb6 | Stavros Sachtouris | def main(self, container___path): |
1634 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run( |
1635 | 201baa17 | Stavros Sachtouris | container___path, path_is_optional=False)
|
1636 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1637 | 7493ccb6 | Stavros Sachtouris | |
1638 | 234954d1 | Stavros Sachtouris | |
1639 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1640 | 915b99b5 | Stavros Sachtouris | class file_permissions_set(_file_container_command, _optional_output_cmd): |
1641 | 72952f4f | Stavros Sachtouris | """Set permissions for an object
|
1642 | 72952f4f | Stavros Sachtouris | New permissions overwrite existing permissions.
|
1643 | 72952f4f | Stavros Sachtouris | Permission format:
|
1644 | 72952f4f | Stavros Sachtouris | - read=<username>[,usergroup[,...]]
|
1645 | 72952f4f | Stavros Sachtouris | - write=<username>[,usegroup[,...]]
|
1646 | 72952f4f | Stavros Sachtouris | E.g. to give read permissions for file F to users A and B and write for C:
|
1647 | 5655d560 | Stavros Sachtouris | . /file permissions set F read=A,B write=C
|
1648 | 72952f4f | Stavros Sachtouris | """
|
1649 | 7493ccb6 | Stavros Sachtouris | |
1650 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1651 | 00336c85 | Stavros Sachtouris | def format_permission_dict(self, permissions): |
1652 | 7493ccb6 | Stavros Sachtouris | read = False
|
1653 | 7493ccb6 | Stavros Sachtouris | write = False
|
1654 | 3dabe5d2 | Stavros Sachtouris | for perms in permissions: |
1655 | 7493ccb6 | Stavros Sachtouris | splstr = perms.split('=')
|
1656 | 7493ccb6 | Stavros Sachtouris | if 'read' == splstr[0]: |
1657 | 24ff0a35 | Stavros Sachtouris | read = [ug.strip() for ug in splstr[1].split(',')] |
1658 | 7493ccb6 | Stavros Sachtouris | elif 'write' == splstr[0]: |
1659 | 24ff0a35 | Stavros Sachtouris | write = [ug.strip() for ug in splstr[1].split(',')] |
1660 | 7493ccb6 | Stavros Sachtouris | else:
|
1661 | 24ff0a35 | Stavros Sachtouris | msg = 'Usage:\tread=<groups,users> write=<groups,users>'
|
1662 | 24ff0a35 | Stavros Sachtouris | raiseCLIError(None, msg)
|
1663 | 234954d1 | Stavros Sachtouris | return (read, write)
|
1664 | 7493ccb6 | Stavros Sachtouris | |
1665 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1666 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1667 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1668 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1669 | b4cf92b8 | Stavros Sachtouris | def _run(self, read, write): |
1670 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.set_object_sharing( |
1671 | 201baa17 | Stavros Sachtouris | self.path, read_permission=read, write_permission=write))
|
1672 | b4cf92b8 | Stavros Sachtouris | |
1673 | 3dabe5d2 | Stavros Sachtouris | def main(self, container___path, *permissions): |
1674 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run( |
1675 | 201baa17 | Stavros Sachtouris | container___path, path_is_optional=False)
|
1676 | 201baa17 | Stavros Sachtouris | read, write = self.format_permission_dict(permissions)
|
1677 | b4cf92b8 | Stavros Sachtouris | self._run(read, write)
|
1678 | 7493ccb6 | Stavros Sachtouris | |
1679 | 234954d1 | Stavros Sachtouris | |
1680 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1681 | 915b99b5 | Stavros Sachtouris | class file_permissions_delete(_file_container_command, _optional_output_cmd): |
1682 | 72952f4f | Stavros Sachtouris | """Delete all permissions set on object
|
1683 | 5655d560 | Stavros Sachtouris | To modify permissions, use /file permissions set
|
1684 | 72952f4f | Stavros Sachtouris | """
|
1685 | 7493ccb6 | Stavros Sachtouris | |
1686 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1687 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1688 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1689 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1690 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1691 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.del_object_sharing(self.path)) |
1692 | b4cf92b8 | Stavros Sachtouris | |
1693 | 7493ccb6 | Stavros Sachtouris | def main(self, container___path): |
1694 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run( |
1695 | 201baa17 | Stavros Sachtouris | container___path, path_is_optional=False)
|
1696 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1697 | 7493ccb6 | Stavros Sachtouris | |
1698 | 234954d1 | Stavros Sachtouris | |
1699 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1700 | 545c6c29 | Stavros Sachtouris | class file_info(_file_container_command, _optional_json): |
1701 | 72952f4f | Stavros Sachtouris | """Get detailed information for user account, containers or objects
|
1702 | 3ae60112 | Stavros Sachtouris | to get account info: /file info
|
1703 | 3ae60112 | Stavros Sachtouris | to get container info: /file info <container>
|
1704 | 3ae60112 | Stavros Sachtouris | to get object info: /file info <container>:<path>
|
1705 | 72952f4f | Stavros Sachtouris | """
|
1706 | 7493ccb6 | Stavros Sachtouris | |
1707 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
1708 | 2fe2672e | Stavros Sachtouris | object_version=ValueArgument( |
1709 | 2fe2672e | Stavros Sachtouris | 'show specific version \ (applies only for objects)',
|
1710 | 545c6c29 | Stavros Sachtouris | ('-O', '--object-version')) |
1711 | 2fe2672e | Stavros Sachtouris | ) |
1712 | 6ac7f90f | Stavros Sachtouris | |
1713 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1714 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1715 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1716 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1717 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1718 | b4cf92b8 | Stavros Sachtouris | if self.container is None: |
1719 | b4cf92b8 | Stavros Sachtouris | r = self.client.get_account_info()
|
1720 | b4cf92b8 | Stavros Sachtouris | elif self.path is None: |
1721 | b4cf92b8 | Stavros Sachtouris | r = self.client.get_container_info(self.container) |
1722 | b4cf92b8 | Stavros Sachtouris | else:
|
1723 | b4cf92b8 | Stavros Sachtouris | r = self.client.get_object_info(
|
1724 | 201baa17 | Stavros Sachtouris | self.path, version=self['object_version']) |
1725 | 545c6c29 | Stavros Sachtouris | self._print(r, print_dict)
|
1726 | b4cf92b8 | Stavros Sachtouris | |
1727 | 7493ccb6 | Stavros Sachtouris | def main(self, container____path__=None): |
1728 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run(container____path__) |
1729 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1730 | 7493ccb6 | Stavros Sachtouris | |
1731 | 234954d1 | Stavros Sachtouris | |
1732 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1733 | 5655d560 | Stavros Sachtouris | class file_metadata(_pithos_init): |
1734 | 5655d560 | Stavros Sachtouris | """Metadata are attached on objects. They are formed as key:value pairs.
|
1735 | 5655d560 | Stavros Sachtouris | They can have arbitary values.
|
1736 | 5655d560 | Stavros Sachtouris | """
|
1737 | 5655d560 | Stavros Sachtouris | |
1738 | 5655d560 | Stavros Sachtouris | |
1739 | 5655d560 | Stavros Sachtouris | @command(pithos_cmds)
|
1740 | 545c6c29 | Stavros Sachtouris | class file_metadata_get(_file_container_command, _optional_json): |
1741 | 72952f4f | Stavros Sachtouris | """Get metadata for account, containers or objects"""
|
1742 | 7493ccb6 | Stavros Sachtouris | |
1743 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
1744 | f40f0cb7 | Stavros Sachtouris | detail=FlagArgument('show detailed output', ('-l', '--details')), |
1745 | 2fe2672e | Stavros Sachtouris | until=DateArgument('show metadata until then', '--until'), |
1746 | 2fe2672e | Stavros Sachtouris | object_version=ValueArgument( |
1747 | c626151a | Stavros Sachtouris | 'show specific version (applies only for objects)',
|
1748 | 545c6c29 | Stavros Sachtouris | ('-O', '--object-version')) |
1749 | 2fe2672e | Stavros Sachtouris | ) |
1750 | 7493ccb6 | Stavros Sachtouris | |
1751 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1752 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1753 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1754 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1755 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1756 | b4cf92b8 | Stavros Sachtouris | until = self['until'] |
1757 | 545c6c29 | Stavros Sachtouris | r = None
|
1758 | b4cf92b8 | Stavros Sachtouris | if self.container is None: |
1759 | b4cf92b8 | Stavros Sachtouris | if self['detail']: |
1760 | b4cf92b8 | Stavros Sachtouris | r = self.client.get_account_info(until=until)
|
1761 | 7493ccb6 | Stavros Sachtouris | else:
|
1762 | b4cf92b8 | Stavros Sachtouris | r = self.client.get_account_meta(until=until)
|
1763 | b4cf92b8 | Stavros Sachtouris | r = pretty_keys(r, '-')
|
1764 | b4cf92b8 | Stavros Sachtouris | elif self.path is None: |
1765 | b4cf92b8 | Stavros Sachtouris | if self['detail']: |
1766 | b4cf92b8 | Stavros Sachtouris | r = self.client.get_container_info(until=until)
|
1767 | b4cf92b8 | Stavros Sachtouris | else:
|
1768 | b4cf92b8 | Stavros Sachtouris | cmeta = self.client.get_container_meta(until=until)
|
1769 | b4cf92b8 | Stavros Sachtouris | ometa = self.client.get_container_object_meta(until=until)
|
1770 | b4cf92b8 | Stavros Sachtouris | r = {} |
1771 | b4cf92b8 | Stavros Sachtouris | if cmeta:
|
1772 | b4cf92b8 | Stavros Sachtouris | r['container-meta'] = pretty_keys(cmeta, '-') |
1773 | b4cf92b8 | Stavros Sachtouris | if ometa:
|
1774 | b4cf92b8 | Stavros Sachtouris | r['object-meta'] = pretty_keys(ometa, '-') |
1775 | b4cf92b8 | Stavros Sachtouris | else:
|
1776 | b4cf92b8 | Stavros Sachtouris | if self['detail']: |
1777 | de73876b | Stavros Sachtouris | r = self.client.get_object_info(
|
1778 | de73876b | Stavros Sachtouris | self.path,
|
1779 | b4cf92b8 | Stavros Sachtouris | version=self['object_version']) |
1780 | b4cf92b8 | Stavros Sachtouris | else:
|
1781 | de73876b | Stavros Sachtouris | r = self.client.get_object_meta(
|
1782 | de73876b | Stavros Sachtouris | self.path,
|
1783 | b4cf92b8 | Stavros Sachtouris | version=self['object_version']) |
1784 | b4cf92b8 | Stavros Sachtouris | r = pretty_keys(pretty_keys(r, '-'))
|
1785 | b4cf92b8 | Stavros Sachtouris | if r:
|
1786 | 545c6c29 | Stavros Sachtouris | self._print(r, print_dict)
|
1787 | b4cf92b8 | Stavros Sachtouris | |
1788 | b4cf92b8 | Stavros Sachtouris | def main(self, container____path__=None): |
1789 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run(container____path__) |
1790 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1791 | 7493ccb6 | Stavros Sachtouris | |
1792 | 234954d1 | Stavros Sachtouris | |
1793 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1794 | 915b99b5 | Stavros Sachtouris | class file_metadata_set(_file_container_command, _optional_output_cmd): |
1795 | 5655d560 | Stavros Sachtouris | """Set a piece of metadata for account, container or object"""
|
1796 | 7493ccb6 | Stavros Sachtouris | |
1797 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1798 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1799 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1800 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1801 | b4cf92b8 | Stavros Sachtouris | def _run(self, metakey, metaval): |
1802 | b4cf92b8 | Stavros Sachtouris | if not self.container: |
1803 | 915b99b5 | Stavros Sachtouris | r = self.client.set_account_meta({metakey: metaval})
|
1804 | b4cf92b8 | Stavros Sachtouris | elif not self.path: |
1805 | 915b99b5 | Stavros Sachtouris | r = self.client.set_container_meta({metakey: metaval})
|
1806 | b4cf92b8 | Stavros Sachtouris | else:
|
1807 | 915b99b5 | Stavros Sachtouris | r = self.client.set_object_meta(self.path, {metakey: metaval}) |
1808 | 915b99b5 | Stavros Sachtouris | self._optional_output(r)
|
1809 | b4cf92b8 | Stavros Sachtouris | |
1810 | b4cf92b8 | Stavros Sachtouris | def main(self, metakey, metaval, container____path__=None): |
1811 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run(container____path__) |
1812 | b4cf92b8 | Stavros Sachtouris | self._run(metakey=metakey, metaval=metaval)
|
1813 | 7493ccb6 | Stavros Sachtouris | |
1814 | 234954d1 | Stavros Sachtouris | |
1815 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1816 | 915b99b5 | Stavros Sachtouris | class file_metadata_delete(_file_container_command, _optional_output_cmd): |
1817 | 72952f4f | Stavros Sachtouris | """Delete metadata with given key from account, container or object
|
1818 | 5655d560 | Stavros Sachtouris | - to get metadata of current account: /file metadata get
|
1819 | 5655d560 | Stavros Sachtouris | - to get metadata of a container: /file metadata get <container>
|
1820 | 5655d560 | Stavros Sachtouris | - to get metadata of an object: /file metadata get <container>:<path>
|
1821 | 72952f4f | Stavros Sachtouris | """
|
1822 | 7493ccb6 | Stavros Sachtouris | |
1823 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1824 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1825 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1826 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
1827 | b4cf92b8 | Stavros Sachtouris | def _run(self, metakey): |
1828 | b4cf92b8 | Stavros Sachtouris | if self.container is None: |
1829 | 915b99b5 | Stavros Sachtouris | r = self.client.del_account_meta(metakey)
|
1830 | b4cf92b8 | Stavros Sachtouris | elif self.path is None: |
1831 | 915b99b5 | Stavros Sachtouris | r = self.client.del_container_meta(metakey)
|
1832 | b4cf92b8 | Stavros Sachtouris | else:
|
1833 | 915b99b5 | Stavros Sachtouris | r = self.client.del_object_meta(self.path, metakey) |
1834 | 915b99b5 | Stavros Sachtouris | self._optional_output(r)
|
1835 | b4cf92b8 | Stavros Sachtouris | |
1836 | 7493ccb6 | Stavros Sachtouris | def main(self, metakey, container____path__=None): |
1837 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run(container____path__) |
1838 | b4cf92b8 | Stavros Sachtouris | self._run(metakey)
|
1839 | 7493ccb6 | Stavros Sachtouris | |
1840 | 234954d1 | Stavros Sachtouris | |
1841 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1842 | 545c6c29 | Stavros Sachtouris | class file_quota(_file_account_command, _optional_json): |
1843 | 3ed6dbde | Stavros Sachtouris | """Get account quota"""
|
1844 | 001200c3 | Stavros Sachtouris | |
1845 | 001200c3 | Stavros Sachtouris | arguments = dict(
|
1846 | 001200c3 | Stavros Sachtouris | in_bytes=FlagArgument('Show result in bytes', ('-b', '--bytes')) |
1847 | de73876b | Stavros Sachtouris | ) |
1848 | 7493ccb6 | Stavros Sachtouris | |
1849 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1850 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1851 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1852 | 545c6c29 | Stavros Sachtouris | |
1853 | 545c6c29 | Stavros Sachtouris | def pretty_print(output): |
1854 | 545c6c29 | Stavros Sachtouris | if not self['in_bytes']: |
1855 | 545c6c29 | Stavros Sachtouris | for k in output: |
1856 | 545c6c29 | Stavros Sachtouris | output[k] = format_size(output[k]) |
1857 | 545c6c29 | Stavros Sachtouris | pretty_dict(output, '-')
|
1858 | 545c6c29 | Stavros Sachtouris | |
1859 | 545c6c29 | Stavros Sachtouris | self._print(self.client.get_account_quota(), pretty_print) |
1860 | 7493ccb6 | Stavros Sachtouris | |
1861 | 3ed6dbde | Stavros Sachtouris | def main(self, custom_uuid=None): |
1862 | 3ed6dbde | Stavros Sachtouris | super(self.__class__, self)._run(custom_account=custom_uuid) |
1863 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1864 | b4cf92b8 | Stavros Sachtouris | |
1865 | 234954d1 | Stavros Sachtouris | |
1866 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1867 | 326a79b9 | Stavros Sachtouris | class file_containerlimit(_pithos_init): |
1868 | 326a79b9 | Stavros Sachtouris | """Container size limit commands"""
|
1869 | 326a79b9 | Stavros Sachtouris | |
1870 | 326a79b9 | Stavros Sachtouris | |
1871 | 326a79b9 | Stavros Sachtouris | @command(pithos_cmds)
|
1872 | 545c6c29 | Stavros Sachtouris | class file_containerlimit_get(_file_container_command, _optional_json): |
1873 | 3ed6dbde | Stavros Sachtouris | """Get container size limit"""
|
1874 | 3ed6dbde | Stavros Sachtouris | |
1875 | 3ed6dbde | Stavros Sachtouris | arguments = dict(
|
1876 | 3ed6dbde | Stavros Sachtouris | in_bytes=FlagArgument('Show result in bytes', ('-b', '--bytes')) |
1877 | 3ed6dbde | Stavros Sachtouris | ) |
1878 | 3ed6dbde | Stavros Sachtouris | |
1879 | 3ed6dbde | Stavros Sachtouris | @errors.generic.all
|
1880 | 3ed6dbde | Stavros Sachtouris | @errors.pithos.container
|
1881 | 3ed6dbde | Stavros Sachtouris | def _run(self): |
1882 | 545c6c29 | Stavros Sachtouris | |
1883 | 545c6c29 | Stavros Sachtouris | def pretty_print(output): |
1884 | 545c6c29 | Stavros Sachtouris | if not self['in_bytes']: |
1885 | 545c6c29 | Stavros Sachtouris | for k, v in output.items(): |
1886 | 545c6c29 | Stavros Sachtouris | output[k] = 'unlimited' if '0' == v else format_size(v) |
1887 | 545c6c29 | Stavros Sachtouris | pretty_dict(output, '-')
|
1888 | 545c6c29 | Stavros Sachtouris | |
1889 | 545c6c29 | Stavros Sachtouris | self._print(
|
1890 | 545c6c29 | Stavros Sachtouris | self.client.get_container_limit(self.container), pretty_print) |
1891 | 3ed6dbde | Stavros Sachtouris | |
1892 | 3ed6dbde | Stavros Sachtouris | def main(self, container=None): |
1893 | 3ed6dbde | Stavros Sachtouris | super(self.__class__, self)._run() |
1894 | 3ed6dbde | Stavros Sachtouris | self.container = container
|
1895 | 3ed6dbde | Stavros Sachtouris | self._run()
|
1896 | 3ed6dbde | Stavros Sachtouris | |
1897 | 3ed6dbde | Stavros Sachtouris | |
1898 | 3ed6dbde | Stavros Sachtouris | @command(pithos_cmds)
|
1899 | 545c6c29 | Stavros Sachtouris | class file_containerlimit_set(_file_account_command, _optional_output_cmd): |
1900 | 326a79b9 | Stavros Sachtouris | """Set new storage limit for a container
|
1901 | 326a79b9 | Stavros Sachtouris | By default, the limit is set in bytes
|
1902 | 001200c3 | Stavros Sachtouris | Users may specify a different unit, e.g:
|
1903 | 326a79b9 | Stavros Sachtouris | /file containerlimit set 2.3GB mycontainer
|
1904 | 3ed6dbde | Stavros Sachtouris | Valid units: B, KiB (1024 B), KB (1000 B), MiB, MB, GiB, GB, TiB, TB
|
1905 | 9f783a51 | Stavros Sachtouris | To set container limit to "unlimited", use 0
|
1906 | 001200c3 | Stavros Sachtouris | """
|
1907 | 001200c3 | Stavros Sachtouris | |
1908 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1909 | 326a79b9 | Stavros Sachtouris | def _calculate_limit(self, user_input): |
1910 | 326a79b9 | Stavros Sachtouris | limit = 0
|
1911 | 001200c3 | Stavros Sachtouris | try:
|
1912 | 326a79b9 | Stavros Sachtouris | limit = int(user_input)
|
1913 | 001200c3 | Stavros Sachtouris | except ValueError: |
1914 | 001200c3 | Stavros Sachtouris | index = 0
|
1915 | 001200c3 | Stavros Sachtouris | digits = [str(num) for num in range(0, 10)] + ['.'] |
1916 | 001200c3 | Stavros Sachtouris | while user_input[index] in digits: |
1917 | 001200c3 | Stavros Sachtouris | index += 1
|
1918 | 326a79b9 | Stavros Sachtouris | limit = user_input[:index] |
1919 | 001200c3 | Stavros Sachtouris | format = user_input[index:] |
1920 | 001200c3 | Stavros Sachtouris | try:
|
1921 | 326a79b9 | Stavros Sachtouris | return to_bytes(limit, format)
|
1922 | 001200c3 | Stavros Sachtouris | except Exception as qe: |
1923 | de73876b | Stavros Sachtouris | msg = 'Failed to convert %s to bytes' % user_input,
|
1924 | de73876b | Stavros Sachtouris | raiseCLIError(qe, msg, details=[ |
1925 | 326a79b9 | Stavros Sachtouris | 'Syntax: containerlimit set <limit>[format] [container]',
|
1926 | 326a79b9 | Stavros Sachtouris | 'e.g.: containerlimit set 2.3GB mycontainer',
|
1927 | 326a79b9 | Stavros Sachtouris | 'Valid formats:',
|
1928 | de73876b | Stavros Sachtouris | '(*1024): B, KiB, MiB, GiB, TiB',
|
1929 | de73876b | Stavros Sachtouris | '(*1000): B, KB, MB, GB, TB'])
|
1930 | 326a79b9 | Stavros Sachtouris | return limit
|
1931 | 7493ccb6 | Stavros Sachtouris | |
1932 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1933 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1934 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1935 | 326a79b9 | Stavros Sachtouris | def _run(self, limit): |
1936 | b4cf92b8 | Stavros Sachtouris | if self.container: |
1937 | b4cf92b8 | Stavros Sachtouris | self.client.container = self.container |
1938 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.set_container_limit(limit)) |
1939 | b4cf92b8 | Stavros Sachtouris | |
1940 | 326a79b9 | Stavros Sachtouris | def main(self, limit, container=None): |
1941 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run() |
1942 | 326a79b9 | Stavros Sachtouris | limit = self._calculate_limit(limit)
|
1943 | b4cf92b8 | Stavros Sachtouris | self.container = container
|
1944 | 326a79b9 | Stavros Sachtouris | self._run(limit)
|
1945 | 7493ccb6 | Stavros Sachtouris | |
1946 | 234954d1 | Stavros Sachtouris | |
1947 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1948 | 915b99b5 | Stavros Sachtouris | class file_versioning(_pithos_init): |
1949 | 915b99b5 | Stavros Sachtouris | """Manage the versioning scheme of current pithos user account"""
|
1950 | 915b99b5 | Stavros Sachtouris | |
1951 | 915b99b5 | Stavros Sachtouris | |
1952 | 915b99b5 | Stavros Sachtouris | @command(pithos_cmds)
|
1953 | 545c6c29 | Stavros Sachtouris | class file_versioning_get(_file_account_command, _optional_json): |
1954 | 776eee69 | Stavros Sachtouris | """Get versioning for account or container"""
|
1955 | 7493ccb6 | Stavros Sachtouris | |
1956 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1957 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1958 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1959 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
1960 | 545c6c29 | Stavros Sachtouris | self._print(
|
1961 | df0045d8 | Stavros Sachtouris | self.client.get_container_versioning(self.container), print_dict) |
1962 | b4cf92b8 | Stavros Sachtouris | |
1963 | df0045d8 | Stavros Sachtouris | def main(self, container): |
1964 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run() |
1965 | b4cf92b8 | Stavros Sachtouris | self.container = container
|
1966 | b4cf92b8 | Stavros Sachtouris | self._run()
|
1967 | 7493ccb6 | Stavros Sachtouris | |
1968 | 234954d1 | Stavros Sachtouris | |
1969 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1970 | 915b99b5 | Stavros Sachtouris | class file_versioning_set(_file_account_command, _optional_output_cmd): |
1971 | 776eee69 | Stavros Sachtouris | """Set versioning mode (auto, none) for account or container"""
|
1972 | 7493ccb6 | Stavros Sachtouris | |
1973 | b4cf92b8 | Stavros Sachtouris | def _check_versioning(self, versioning): |
1974 | b4cf92b8 | Stavros Sachtouris | if versioning and versioning.lower() in ('auto', 'none'): |
1975 | b4cf92b8 | Stavros Sachtouris | return versioning.lower()
|
1976 | b4cf92b8 | Stavros Sachtouris | raiseCLIError('Invalid versioning %s' % versioning, details=[
|
1977 | b4cf92b8 | Stavros Sachtouris | 'Versioning can be auto or none'])
|
1978 | b4cf92b8 | Stavros Sachtouris | |
1979 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
1980 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
1981 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
1982 | b4cf92b8 | Stavros Sachtouris | def _run(self, versioning): |
1983 | df0045d8 | Stavros Sachtouris | self.client.container = self.container |
1984 | df0045d8 | Stavros Sachtouris | r = self.client.set_container_versioning(versioning)
|
1985 | 915b99b5 | Stavros Sachtouris | self._optional_output(r)
|
1986 | b4cf92b8 | Stavros Sachtouris | |
1987 | df0045d8 | Stavros Sachtouris | def main(self, versioning, container): |
1988 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run() |
1989 | b4cf92b8 | Stavros Sachtouris | self._run(self._check_versioning(versioning)) |
1990 | 7493ccb6 | Stavros Sachtouris | |
1991 | 234954d1 | Stavros Sachtouris | |
1992 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
1993 | 915b99b5 | Stavros Sachtouris | class file_group(_pithos_init): |
1994 | 915b99b5 | Stavros Sachtouris | """Manage access groups and group members"""
|
1995 | 915b99b5 | Stavros Sachtouris | |
1996 | 915b99b5 | Stavros Sachtouris | |
1997 | 915b99b5 | Stavros Sachtouris | @command(pithos_cmds)
|
1998 | 545c6c29 | Stavros Sachtouris | class file_group_list(_file_account_command, _optional_json): |
1999 | 545c6c29 | Stavros Sachtouris | """list all groups and group members"""
|
2000 | 7493ccb6 | Stavros Sachtouris | |
2001 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
2002 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
2003 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
2004 | 545c6c29 | Stavros Sachtouris | self._print(self.client.get_account_group(), pretty_dict, delim='-') |
2005 | b4cf92b8 | Stavros Sachtouris | |
2006 | 7493ccb6 | Stavros Sachtouris | def main(self): |
2007 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run() |
2008 | b4cf92b8 | Stavros Sachtouris | self._run()
|
2009 | 7493ccb6 | Stavros Sachtouris | |
2010 | 234954d1 | Stavros Sachtouris | |
2011 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
2012 | 915b99b5 | Stavros Sachtouris | class file_group_set(_file_account_command, _optional_output_cmd): |
2013 | 4fcc38a2 | Stavros Sachtouris | """Set a user group"""
|
2014 | 7493ccb6 | Stavros Sachtouris | |
2015 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
2016 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
2017 | b4cf92b8 | Stavros Sachtouris | def _run(self, groupname, *users): |
2018 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.set_account_group(groupname, users)) |
2019 | b4cf92b8 | Stavros Sachtouris | |
2020 | 7493ccb6 | Stavros Sachtouris | def main(self, groupname, *users): |
2021 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run() |
2022 | b4cf92b8 | Stavros Sachtouris | if users:
|
2023 | b4cf92b8 | Stavros Sachtouris | self._run(groupname, *users)
|
2024 | b4cf92b8 | Stavros Sachtouris | else:
|
2025 | b4cf92b8 | Stavros Sachtouris | raiseCLIError('No users to add in group %s' % groupname)
|
2026 | 7493ccb6 | Stavros Sachtouris | |
2027 | 234954d1 | Stavros Sachtouris | |
2028 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
2029 | 915b99b5 | Stavros Sachtouris | class file_group_delete(_file_account_command, _optional_output_cmd): |
2030 | 4fcc38a2 | Stavros Sachtouris | """Delete a user group"""
|
2031 | 7493ccb6 | Stavros Sachtouris | |
2032 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
2033 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
2034 | b4cf92b8 | Stavros Sachtouris | def _run(self, groupname): |
2035 | 915b99b5 | Stavros Sachtouris | self._optional_output(self.client.del_account_group(groupname)) |
2036 | b4cf92b8 | Stavros Sachtouris | |
2037 | 7493ccb6 | Stavros Sachtouris | def main(self, groupname): |
2038 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run() |
2039 | b4cf92b8 | Stavros Sachtouris | self._run(groupname)
|
2040 | a23f6ffe | Stavros Sachtouris | |
2041 | 234954d1 | Stavros Sachtouris | |
2042 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
2043 | 545c6c29 | Stavros Sachtouris | class file_sharers(_file_account_command, _optional_json): |
2044 | 4fcc38a2 | Stavros Sachtouris | """List the accounts that share objects with current user"""
|
2045 | a23f6ffe | Stavros Sachtouris | |
2046 | 2fe2672e | Stavros Sachtouris | arguments = dict(
|
2047 | f40f0cb7 | Stavros Sachtouris | detail=FlagArgument('show detailed output', ('-l', '--details')), |
2048 | 2fe2672e | Stavros Sachtouris | marker=ValueArgument('show output greater then marker', '--marker') |
2049 | 2fe2672e | Stavros Sachtouris | ) |
2050 | a23f6ffe | Stavros Sachtouris | |
2051 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
2052 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
2053 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
2054 | b4cf92b8 | Stavros Sachtouris | accounts = self.client.get_sharing_accounts(marker=self['marker']) |
2055 | 9d3cd179 | Stavros Sachtouris | uuids = [acc['name'] for acc in accounts] |
2056 | 9d3cd179 | Stavros Sachtouris | try:
|
2057 | 9d3cd179 | Stavros Sachtouris | astakos_responce = self.auth_base.post_user_catalogs(uuids)
|
2058 | 9d3cd179 | Stavros Sachtouris | usernames = astakos_responce.json |
2059 | 9d3cd179 | Stavros Sachtouris | r = usernames['uuid_catalog']
|
2060 | 9d3cd179 | Stavros Sachtouris | except Exception as e: |
2061 | 9d3cd179 | Stavros Sachtouris | print 'WARNING: failed to call user_catalogs, %s' % e |
2062 | 9d3cd179 | Stavros Sachtouris | r = dict(sharer_uuid=uuids)
|
2063 | 9d3cd179 | Stavros Sachtouris | usernames = accounts |
2064 | 545c6c29 | Stavros Sachtouris | if self['json_output'] or self['detail']: |
2065 | 9d3cd179 | Stavros Sachtouris | self._print(usernames)
|
2066 | 24ff0a35 | Stavros Sachtouris | else:
|
2067 | 9d3cd179 | Stavros Sachtouris | self._print(r, print_dict)
|
2068 | a23f6ffe | Stavros Sachtouris | |
2069 | b4cf92b8 | Stavros Sachtouris | def main(self): |
2070 | b4cf92b8 | Stavros Sachtouris | super(self.__class__, self)._run() |
2071 | b4cf92b8 | Stavros Sachtouris | self._run()
|
2072 | 38dc5d2f | Stavros Sachtouris | |
2073 | 234954d1 | Stavros Sachtouris | |
2074 | 545c6c29 | Stavros Sachtouris | def version_print(versions): |
2075 | 545c6c29 | Stavros Sachtouris | print_items([dict(id=vitem[0], created=strftime( |
2076 | 545c6c29 | Stavros Sachtouris | '%d-%m-%Y %H:%M:%S',
|
2077 | 545c6c29 | Stavros Sachtouris | localtime(float(vitem[1])))) for vitem in versions]) |
2078 | 545c6c29 | Stavros Sachtouris | |
2079 | 545c6c29 | Stavros Sachtouris | |
2080 | d486baec | Stavros Sachtouris | @command(pithos_cmds)
|
2081 | 545c6c29 | Stavros Sachtouris | class file_versions(_file_container_command, _optional_json): |
2082 | 4fcc38a2 | Stavros Sachtouris | """Get the list of object versions
|
2083 | 4fcc38a2 | Stavros Sachtouris | Deleted objects may still have versions that can be used to restore it and
|
2084 | 4fcc38a2 | Stavros Sachtouris | get information about its previous state.
|
2085 | 4fcc38a2 | Stavros Sachtouris | The version number can be used in a number of other commands, like info,
|
2086 | 4fcc38a2 | Stavros Sachtouris | copy, move, meta. See these commands for more information, e.g.
|
2087 | 3ae60112 | Stavros Sachtouris | /file info -h
|
2088 | 4fcc38a2 | Stavros Sachtouris | """
|
2089 | 38dc5d2f | Stavros Sachtouris | |
2090 | b4cf92b8 | Stavros Sachtouris | @errors.generic.all
|
2091 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.connection
|
2092 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.container
|
2093 | b4cf92b8 | Stavros Sachtouris | @errors.pithos.object_path
|
2094 | b4cf92b8 | Stavros Sachtouris | def _run(self): |
2095 | 545c6c29 | Stavros Sachtouris | self._print(
|
2096 | 545c6c29 | Stavros Sachtouris | self.client.get_object_versionlist(self.path), version_print) |
2097 | b4cf92b8 | Stavros Sachtouris | |
2098 | 38dc5d2f | Stavros Sachtouris | def main(self, container___path): |
2099 | 3ae60112 | Stavros Sachtouris | super(file_versions, self)._run( |
2100 | b4cf92b8 | Stavros Sachtouris | container___path, |
2101 | b4cf92b8 | Stavros Sachtouris | path_is_optional=False)
|
2102 | b4cf92b8 | Stavros Sachtouris | self._run() |