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