- display command usage instead of client usage
[pithos] / pithos / lib / client.py
1 # Copyright 2011 GRNET S.A. All rights reserved.
2
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10
11 #   2. Redistributions in binary form must reproduce the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer in the documentation and/or other materials
14 #      provided with the distribution.
15
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
33
34 from httplib import HTTPConnection, HTTP
35 from sys import stdin
36
37 import json
38 import types
39 import socket
40 import pithos.api.faults
41
42 ERROR_CODES = {304:'Not Modified',
43                400:'Bad Request',
44                401:'Unauthorized',
45                404:'Not Found',
46                409:'Conflict',
47                411:'Length Required',
48                412:'Precondition Failed',
49                416:'Range Not Satisfiable',
50                422:'Unprocessable Entity',
51                503:'Service Unavailable'}
52
53 class Fault(Exception):
54     def __init__(self, data='', status=None):
55         if data == '' and status in ERROR_CODES.keys():
56             data = ERROR_CODES[status]
57         Exception.__init__(self, data)
58         self.data = data
59         self.status = status
60
61 class Client(object):
62     def __init__(self, host, account, api='v1', verbose=False, debug=False):
63         """`host` can also include a port, e.g '127.0.0.1:8000'."""
64         
65         self.host = host
66         self.account = account
67         self.api = api
68         self.verbose = verbose or debug
69         self.debug = debug
70     
71     def _chunked_transfer(self, path, method='PUT', f=stdin, headers=None,
72                           blocksize=1024):
73         http = HTTPConnection(self.host)
74         
75         # write header
76         path = '/%s/%s%s' % (self.api, self.account, path)
77         http.putrequest(method, path)
78         http.putheader('Content-Type', 'application/octet-stream')
79         http.putheader('Transfer-Encoding', 'chunked')
80         if headers:
81             for header,value in headers.items():
82                 http.putheader(header, value)
83         http.endheaders()
84         
85         # write body
86         data = ''
87         while True:
88             if f.closed:
89                 break
90             block = f.read(blocksize)
91             if block == '':
92                 break
93             data = '%s\r\n%s\r\n' % (hex(len(block)), block)
94             try:
95                 http.send(data)
96             except:
97                 #retry
98                 http.send(data)
99         data = '0x0\r\n'
100         try:
101             http.send(data)
102         except:
103             #retry
104             http.send(data)
105         
106         # get response
107         resp = http.getresponse()
108         
109         headers = dict(resp.getheaders())
110         
111         if self.verbose:
112             print '%d %s' % (resp.status, resp.reason)
113             for key, val in headers.items():
114                 print '%s: %s' % (key.capitalize(), val)
115             print
116         
117         length = resp.getheader('Content-length', None)
118         data = resp.read(length)
119         if self.debug:
120             print data
121             print
122         
123         if int(resp.status) in ERROR_CODES.keys():
124             raise Fault(data, int(resp.status))
125         
126         #print '*',  resp.status, headers, data
127         return resp.status, headers, data
128     
129     def req(self, method, path, body=None, headers=None, format='text',
130             params=None):
131         full_path = '/%s/%s%s?format=%s' % (self.api, self.account, path,
132                                             format)
133         if params:
134             for k,v in params.items():
135                 if v:
136                     full_path = '%s&%s=%s' %(full_path, k, v)
137         conn = HTTPConnection(self.host)
138         
139         #encode whitespace
140         full_path = full_path.replace(' ', '%20')
141         
142         kwargs = {}
143         kwargs['headers'] = headers or {}
144         if not headers or \
145         'Transfer-Encoding' not in headers \
146         or headers['Transfer-Encoding'] != 'chunked':
147             kwargs['headers']['Content-Length'] = len(body) if body else 0
148         if body:
149             kwargs['body'] = body
150             kwargs['headers']['Content-Type'] = 'application/octet-stream'
151         #print '****', method, full_path, kwargs
152         try:
153             conn.request(method, full_path, **kwargs)
154         except socket.error, e:
155             raise Fault(status=503)
156             
157         resp = conn.getresponse()
158         headers = dict(resp.getheaders())
159         
160         if self.verbose:
161             print '%d %s' % (resp.status, resp.reason)
162             for key, val in headers.items():
163                 print '%s: %s' % (key.capitalize(), val)
164             print
165         
166         length = resp.getheader('Content-length', None)
167         data = resp.read(length)
168         if self.debug:
169             print data
170             print
171         
172         if int(resp.status) in ERROR_CODES.keys():
173             raise Fault(data, int(resp.status))
174         
175         #print '*',  resp.status, headers, data
176         return resp.status, headers, data
177     
178     def delete(self, path, format='text'):
179         return self.req('DELETE', path, format=format)
180     
181     def get(self, path, format='text', headers=None, params=None):
182         return self.req('GET', path, headers=headers, format=format,
183                         params=params)
184     
185     def head(self, path, format='text', params=None):
186         return self.req('HEAD', path, format=format, params=params)
187     
188     def post(self, path, body=None, format='text', headers=None):
189         return self.req('POST', path, body, headers=headers, format=format)
190     
191     def put(self, path, body=None, format='text', headers=None):
192         return self.req('PUT', path, body, headers=headers, format=format)
193     
194     def _list(self, path, detail=False, params=None, headers=None):
195         format = 'json' if detail else 'text'
196         status, headers, data = self.get(path, format=format, headers=headers,
197                                          params=params)
198         if detail:
199             data = json.loads(data)
200         else:
201             data = data.strip().split('\n')
202         return data
203     
204     def _get_metadata(self, path, prefix=None, params=None):
205         status, headers, data = self.head(path, params=params)
206         prefixlen = len(prefix) if prefix else 0
207         meta = {}
208         for key, val in headers.items():
209             if prefix and not key.startswith(prefix):
210                 continue
211             elif prefix and key.startswith(prefix):
212                 key = key[prefixlen:]
213             meta[key] = val
214         return meta
215     
216     def _update_metadata(self, path, entity, **meta):
217         """
218          adds new and updates the values of previously set metadata
219         """
220         prefix = 'x-%s-meta-' % entity
221         prev_meta = self._get_metadata(path, prefix)
222         prev_meta.update(meta)
223         headers = {}
224         for key, val in prev_meta.items():
225             key = '%s%s' % (prefix, key)
226             key = '-'.join(elem.capitalize() for elem in key.split('-'))
227             headers[key] = val
228         self.post(path, headers=headers)
229     
230     def _delete_metadata(self, path, entity, meta=[]):
231         """
232         delete previously set metadata
233         """
234         prefix = 'x-%s-meta-' % entity
235         prev_meta = self._get_metadata(path, prefix)
236         headers = {}
237         for key, val in prev_meta.items():
238             if key in meta:
239                 continue
240             key = '%s%s' % (prefix, key)
241             key = '-'.join(elem.capitalize() for elem in key.split('-'))
242             headers[key] = val
243         self.post(path, headers=headers)
244     
245     # Storage Account Services
246     
247     def list_containers(self, detail=False, params=None, headers=None):
248         return self._list('', detail, params, headers)
249     
250     def account_metadata(self, restricted=False, until=None):
251         prefix = 'x-account-meta-' if restricted else None
252         params = {'until':until} if until else None
253         return self._get_metadata('', prefix, params=params)
254     
255     def update_account_metadata(self, **meta):
256         self._update_metadata('', 'account', **meta)
257         
258     def delete_account_metadata(self, meta=[]):
259         self._delete_metadata('', 'account', meta)
260     
261     def set_account_groups(self, groups):
262         headers = {}
263         for key, val in groups.items():
264             headers['X-Account-Group-%s' % key.capitalize()] = val
265         self.post('', headers=headers)
266     
267     # Storage Container Services
268     
269     def _filter(self, l, d):
270         """
271         filter out from l elements having the metadata values provided
272         """
273         ll = l
274         for elem in l:
275             if type(elem) == types.DictionaryType:
276                 for key in d.keys():
277                     k = 'x_object_meta_%s' % key
278                     if k in elem.keys() and elem[k] == d[key]:
279                         ll.remove(elem)
280                         break
281         return ll
282     
283     def _filter_trashed(self, l):
284         return self._filter(l, {'trash':'true'})
285     
286     def list_objects(self, container, detail=False, params=None, headers=None,
287                      include_trashed=False):
288         l = self._list('/' + container, detail, params, headers)
289         if not include_trashed:
290             l = self._filter_trashed(l)
291         return l
292     
293     def create_container(self, container, headers=None):
294         status, header, data = self.put('/' + container, headers=headers)
295         if status == 202:
296             return False
297         elif status != 201:
298             raise Fault(data, int(status))
299         return True
300     
301     def delete_container(self, container):
302         self.delete('/' + container)
303     
304     def retrieve_container_metadata(self, container, restricted=False,
305                                     until=None):
306         prefix = 'x-container-meta-' if restricted else None
307         params = {'until':until} if until else None
308         return self._get_metadata('/%s' % container, prefix, params=params)
309     
310     def update_container_metadata(self, container, **meta):
311         self._update_metadata('/' + container, 'container', **meta)
312         
313     def delete_container_metadata(self, container, meta=[]):
314         path = '/%s' % (container)
315         self._delete_metadata(path, 'container', meta)
316     
317     # Storage Object Services
318     
319     def retrieve_object(self, container, object, detail=False, headers=None,
320                         version=None):
321         path = '/%s/%s' % (container, object)
322         format = 'json' if detail else 'text'
323         params = {'version':version} if version else None 
324         status, headers, data = self.get(path, format, headers, params)
325         return data
326     
327     def create_directory_marker(self, container, object):
328         if not object:
329             raise Fault('Directory markers have to be nested in a container')
330         h = {'Content-Type':'application/directory'}
331         self.create_object(container, object, f=None, headers=h)
332     
333     def create_object(self, container, object, f=stdin, chunked=False,
334                       blocksize=1024, headers=None):
335         """
336         creates an object
337         if f is None then creates a zero length object
338         if f is stdin or chunked is set then performs chunked transfer 
339         """
340         path = '/%s/%s' % (container, object)
341         if not chunked and f != stdin:
342             data = f.read() if f else None
343             return self.put(path, data, headers=headers)
344         else:
345             return self._chunked_transfer(path, 'PUT', f, headers=headers,
346                                    blocksize=1024)
347     
348     def update_object(self, container, object, f=stdin, chunked=False,
349                       blocksize=1024, headers=None):
350         path = '/%s/%s' % (container, object)
351         if not chunked and f != stdin:
352             data = f.read() if f else None
353             self.post(path, data, headers=headers)
354         else:
355             self._chunked_transfer(path, 'POST', f, headers=headers,
356                                    blocksize=1024)
357     
358     def _change_obj_location(self, src_container, src_object, dst_container,
359                              dst_object, remove=False, headers=None):
360         path = '/%s/%s' % (dst_container, dst_object)
361         if not headers:
362             headers = {}
363         if remove:
364             headers['X-Move-From'] = '/%s/%s' % (src_container, src_object)
365         else:
366             headers['X-Copy-From'] = '/%s/%s' % (src_container, src_object)
367         headers['Content-Length'] = 0
368         self.put(path, headers=headers)
369     
370     def copy_object(self, src_container, src_object, dst_container,
371                              dst_object, headers=None):
372         self._change_obj_location(src_container, src_object,
373                                    dst_container, dst_object,
374                                    headers=headers)
375     
376     def move_object(self, src_container, src_object, dst_container,
377                              dst_object, headers=None):
378         self._change_obj_location(src_container, src_object,
379                                    dst_container, dst_object, True, headers)
380     
381     def delete_object(self, container, object):
382         self.delete('/%s/%s' % (container, object))
383     
384     def retrieve_object_metadata(self, container, object, restricted=False,
385                                  version=None):
386         path = '/%s/%s' % (container, object)
387         prefix = 'x-object-meta-' if restricted else None
388         params = {'version':version} if version else None
389         return self._get_metadata(path, prefix, params=params)
390     
391     def update_object_metadata(self, container, object, **meta):
392         path = '/%s/%s' % (container, object)
393         self._update_metadata(path, 'object', **meta)
394     
395     def delete_object_metadata(self, container, object, meta=[]):
396         path = '/%s/%s' % (container, object)
397         self._delete_metadata(path, 'object', meta)
398     
399     def trash_object(self, container, object):
400         """
401         trashes an object
402         actually resets all object metadata with trash = true 
403         """
404         path = '/%s/%s' % (container, object)
405         meta = {'trash':'true'}
406         self._update_metadata(path, 'object', **meta)
407     
408     def restore_object(self, container, object):
409         """
410         restores a trashed object
411         actualy removes trash object metadata info
412         """
413         self.delete_object_metadata(container, object, ['trash'])
414