__init__.py 47.4 KB
Newer Older
Stavros Sachtouris's avatar
Stavros Sachtouris committed
1
# Copyright 2011-2013 GRNET S.A. All rights reserved.
Giorgos Verigakis's avatar
Giorgos Verigakis committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

34
from threading import enumerate as activethreads
35

36
from os import fstat
37
from hashlib import new as newhashlib
38
from time import time
39
from StringIO import StringIO
Giorgos Verigakis's avatar
Giorgos Verigakis committed
40

41
from binascii import hexlify
Giorgos Verigakis's avatar
Giorgos Verigakis committed
42

43
from kamaki.clients import SilentEvent, sendlog
44
from kamaki.clients.pithos.rest_api import PithosRestClient
45
46
from kamaki.clients.storage import ClientError
from kamaki.clients.utils import path4url, filter_in
Giorgos Verigakis's avatar
Giorgos Verigakis committed
47

48

49
def _pithos_hash(block, blockhash):
50
    h = newhashlib(blockhash)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
51
52
53
    h.update(block.rstrip('\x00'))
    return h.hexdigest()

54

55
56
57
58
59
def _range_up(start, end, a_range):
    if a_range:
        (rstart, rend) = a_range.split('-')
        (rstart, rend) = (int(rstart), int(rend))
        if rstart > end or rend < start:
60
            return (0, 0)
61
62
63
64
65
66
        if rstart > start:
            start = rstart
        if rend < end:
            end = rend
    return (start, end)

67

68
class PithosClient(PithosRestClient):
69
    """Synnefo Pithos+ API client"""
Giorgos Verigakis's avatar
Giorgos Verigakis committed
70

71
72
    def __init__(self, base_url, token, account=None, container=None):
        super(PithosClient, self).__init__(base_url, token, account, container)
73

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    def create_container(
            self,
            container=None, sizelimit=None, versioning=None, metadata=None):
        """
        :param container: (str) if not given, self.container is used instead

        :param sizelimit: (int) container total size limit in bytes

        :param versioning: (str) can be auto or whatever supported by server

        :param metadata: (dict) Custom user-defined metadata of the form
            { 'name1': 'value1', 'name2': 'value2', ... }

        :returns: (dict) response headers
        """
        cnt_back_up = self.container
        try:
            self.container = container or cnt_back_up
            r = self.container_put(
                quota=sizelimit, versioning=versioning, metadata=metadata)
            return r.headers
        finally:
            self.container = cnt_back_up

98
    def purge_container(self, container=None):
99
100
        """Delete an empty container and destroy associated blocks
        """
101
102
103
        cnt_back_up = self.container
        try:
            self.container = container or cnt_back_up
104
            r = self.container_delete(until=unicode(time()))
105
106
        finally:
            self.container = cnt_back_up
107
        return r.headers
108

109
110
111
112
113
114
115
116
117
118
    def upload_object_unchunked(
            self, obj, f,
            withHashFile=False,
            size=None,
            etag=None,
            content_encoding=None,
            content_disposition=None,
            content_type=None,
            sharing=None,
            public=None):
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
        """
        :param obj: (str) remote object path

        :param f: open file descriptor

        :param withHashFile: (bool)

        :param size: (int) size of data to upload

        :param etag: (str)

        :param content_encoding: (str)

        :param content_disposition: (str)

        :param content_type: (str)

        :param sharing: {'read':[user and/or grp names],
            'write':[usr and/or grp names]}

        :param public: (bool)
140
141

        :returns: (dict) created object metadata
142
        """
143
        self._assert_container()
144
145
146
147
148
149
150

        if withHashFile:
            data = f.read()
            try:
                import json
                data = json.dumps(json.loads(data))
            except ValueError:
151
                raise ClientError('"%s" is not json-formated' % f.name, 1)
152
            except SyntaxError:
153
154
                msg = '"%s" is not a valid hashmap file' % f.name
                raise ClientError(msg, 1)
155
            f = StringIO(data)
156
        else:
157
            data = f.read(size) if size else f.read()
158
        r = self.object_put(
159
            obj,
160
161
162
163
164
165
166
167
            data=data,
            etag=etag,
            content_encoding=content_encoding,
            content_disposition=content_disposition,
            content_type=content_type,
            permissions=sharing,
            public=public,
            success=201)
168
        return r.headers
169

170
171
172
173
174
175
176
177
    def create_object_by_manifestation(
            self, obj,
            etag=None,
            content_encoding=None,
            content_disposition=None,
            content_type=None,
            sharing=None,
            public=None):
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
        """
        :param obj: (str) remote object path

        :param etag: (str)

        :param content_encoding: (str)

        :param content_disposition: (str)

        :param content_type: (str)

        :param sharing: {'read':[user and/or grp names],
            'write':[usr and/or grp names]}

        :param public: (bool)
193
194

        :returns: (dict) created object metadata
195
        """
196
        self._assert_container()
197
        r = self.object_put(
198
            obj,
199
200
201
202
203
204
205
206
            content_length=0,
            etag=etag,
            content_encoding=content_encoding,
            content_disposition=content_disposition,
            content_type=content_type,
            permissions=sharing,
            public=public,
            manifest='%s/%s' % (self.container, obj))
207
        return r.headers
208

209
    # upload_* auxiliary methods
210
    def _put_block_async(self, data, hash):
211
212
213
214
215
        event = SilentEvent(method=self._put_block, data=data, hash=hash)
        event.start()
        return event

    def _put_block(self, data, hash):
216
217
        r = self.container_post(
            update=True,
218
219
220
221
222
223
            content_type='application/octet-stream',
            content_length=len(data),
            data=data,
            format='json')
        assert r.json[0] == hash, 'Local hash does not match server'

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    def _get_file_block_info(self, fileobj, size=None, cache=None):
        """
        :param fileobj: (file descriptor) source

        :param size: (int) size of data to upload from source

        :param cache: (dict) if provided, cache container info response to
        avoid redundant calls
        """
        if isinstance(cache, dict):
            try:
                meta = cache[self.container]
            except KeyError:
                meta = self.get_container_info()
                cache[self.container] = meta
        else:
            meta = self.get_container_info()
241
242
        blocksize = int(meta['x-container-block-size'])
        blockhash = meta['x-container-block-hash']
243
        size = size if size is not None else fstat(fileobj.fileno()).st_size
244
        nblocks = 1 + (size - 1) // blocksize
245
246
        return (blocksize, blockhash, size, nblocks)

247
    def _create_object_or_get_missing_hashes(
248
249
250
251
252
            self, obj, json,
            size=None,
            format='json',
            hashmap=True,
            content_type=None,
253
254
            if_etag_match=None,
            if_etag_not_match=None,
255
256
257
258
259
260
261
            content_encoding=None,
            content_disposition=None,
            permissions=None,
            public=None,
            success=(201, 409)):
        r = self.object_put(
            obj,
262
263
264
265
            format='json',
            hashmap=True,
            content_type=content_type,
            json=json,
266
267
            if_etag_match=if_etag_match,
            if_etag_not_match=if_etag_not_match,
268
269
270
271
            content_encoding=content_encoding,
            content_disposition=content_disposition,
            permissions=permissions,
            public=public,
272
            success=success)
273
        return (None if r.status_code == 201 else r.json), r.headers
274

275
    def _calculate_blocks_for_upload(
276
277
            self, blocksize, blockhash, size, nblocks, hashes, hmap, fileobj,
            hash_cb=None):
278
        offset = 0
279
280
281
282
283
        if hash_cb:
            hash_gen = hash_cb(nblocks)
            hash_gen.next()

        for i in range(nblocks):
284
            block = fileobj.read(min(blocksize, size - offset))
285
            bytes = len(block)
286
            hash = _pithos_hash(block, blockhash)
287
            hashes.append(hash)
288
            hmap[hash] = (offset, bytes)
289
290
291
            offset += bytes
            if hash_cb:
                hash_gen.next()
Stavros Sachtouris's avatar
Stavros Sachtouris committed
292
        msg = 'Failed to calculate uploaded blocks:'
293
        ' Offset and object size do not match'
Stavros Sachtouris's avatar
Stavros Sachtouris committed
294
        assert offset == size, msg
295

296
297
    def _upload_missing_blocks(self, missing, hmap, fileobj, upload_gen=None):
        """upload missing blocks asynchronously"""
298

299
        self._init_thread_limit()
300

301
        flying = []
302
        failures = []
303
        for hash in missing:
304
            offset, bytes = hmap[hash]
305
306
            fileobj.seek(offset)
            data = fileobj.read(bytes)
307
            r = self._put_block_async(data, hash)
308
            flying.append(r)
309
310
            unfinished = self._watch_thread_limit(flying)
            for thread in set(flying).difference(unfinished):
311
312
                if thread.exception:
                    failures.append(thread)
313
                    if isinstance(
314
315
316
                            thread.exception,
                            ClientError) and thread.exception.status == 502:
                        self.POOLSIZE = self._thread_limit
317
                elif thread.isAlive():
318
                    flying.append(thread)
319
320
321
322
323
                elif upload_gen:
                    try:
                        upload_gen.next()
                    except:
                        pass
324
325
326
327
            flying = unfinished

        for thread in flying:
            thread.join()
328
329
            if thread.exception:
                failures.append(thread)
330
331
332
333
334
            elif upload_gen:
                try:
                    upload_gen.next()
                except:
                    pass
335

336
        return [failure.kwargs['hash'] for failure in failures]
337

338
339
340
341
342
343
    def upload_object(
            self, obj, f,
            size=None,
            hash_cb=None,
            upload_cb=None,
            etag=None,
344
            if_etag_match=None,
345
            if_not_exist=None,
346
347
348
349
            content_encoding=None,
            content_disposition=None,
            content_type=None,
            sharing=None,
350
351
            public=None,
            container_info_cache=None):
352
353
354
355
356
357
358
359
360
361
362
363
        """Upload an object using multiple connections (threads)

        :param obj: (str) remote object path

        :param f: open file descriptor (rb)

        :param hash_cb: optional progress.bar object for calculating hashes

        :param upload_cb: optional progress.bar object for uploading

        :param etag: (str)

364
365
366
        :param if_etag_match: (str) Push that value to if-match header at file
            creation

367
368
369
370
371
        :param if_not_exist: (bool) If true, the file will be uploaded ONLY if
            it does not exist remotely, otherwise the operation will fail.
            Involves the case of an object with the same path is created while
            the object is being uploaded.

372
373
374
375
376
377
378
379
380
381
        :param content_encoding: (str)

        :param content_disposition: (str)

        :param content_type: (str)

        :param sharing: {'read':[user and/or grp names],
            'write':[usr and/or grp names]}

        :param public: (bool)
382
383
384

        :param container_info_cache: (dict) if given, avoid redundant calls to
        server for container info (block size and hash information)
385
        """
386
        self._assert_container()
387

388
389
390
        block_info = (
            blocksize, blockhash, size, nblocks) = self._get_file_block_info(
                f, size, container_info_cache)
391
        (hashes, hmap, offset) = ([], {}, 0)
392
        if not content_type:
393
            content_type = 'application/octet-stream'
394

395
        self._calculate_blocks_for_upload(
396
            *block_info,
397
398
399
            hashes=hashes,
            hmap=hmap,
            fileobj=f,
400
401
402
            hash_cb=hash_cb)

        hashmap = dict(bytes=size, hashes=hashes)
403
        missing, obj_headers = self._create_object_or_get_missing_hashes(
404
            obj, hashmap,
405
406
            content_type=content_type,
            size=size,
407
408
            if_etag_match=if_etag_match,
            if_etag_not_match='*' if if_not_exist else None,
409
410
411
412
            content_encoding=content_encoding,
            content_disposition=content_disposition,
            permissions=sharing,
            public=public)
413
414

        if missing is None:
415
            return obj_headers
416

417
418
        if upload_cb:
            upload_gen = upload_cb(len(missing))
419
420
421
422
423
            for i in range(len(missing), len(hashmap['hashes']) + 1):
                try:
                    upload_gen.next()
                except:
                    upload_gen = None
424
425
426
        else:
            upload_gen = None

427
        retries = 7
428
        try:
429
            while retries:
430
                sendlog.info('%s blocks missing' % len(missing))
431
432
433
434
435
                num_of_blocks = len(missing)
                missing = self._upload_missing_blocks(
                    missing,
                    hmap,
                    f,
436
                    upload_gen)
437
438
439
440
441
                if missing:
                    if num_of_blocks == len(missing):
                        retries -= 1
                    else:
                        num_of_blocks = len(missing)
442
443
444
                else:
                    break
            if missing:
445
446
447
                raise ClientError(
                    '%s blocks failed to upload' % len(missing),
                    details=['%s' % thread.exception for thread in missing])
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
        except KeyboardInterrupt:
            sendlog.info('- - - wait for threads to finish')
            for thread in activethreads():
                thread.join()
            raise

        r = self.object_put(
            obj,
            format='json',
            hashmap=True,
            content_type=content_type,
            if_etag_match=if_etag_match,
            if_etag_not_match='*' if if_not_exist else None,
            etag=etag,
            json=hashmap,
            permissions=sharing,
            public=public,
            success=201)
        return r.headers

    def upload_from_string(
            self, obj, input_str,
            hash_cb=None,
            upload_cb=None,
            etag=None,
            if_etag_match=None,
            if_not_exist=None,
            content_encoding=None,
            content_disposition=None,
            content_type=None,
            sharing=None,
            public=None,
            container_info_cache=None):
        """Upload an object using multiple connections (threads)

        :param obj: (str) remote object path

        :param input_str: (str) upload content

        :param hash_cb: optional progress.bar object for calculating hashes

        :param upload_cb: optional progress.bar object for uploading

        :param etag: (str)

        :param if_etag_match: (str) Push that value to if-match header at file
            creation

        :param if_not_exist: (bool) If true, the file will be uploaded ONLY if
            it does not exist remotely, otherwise the operation will fail.
            Involves the case of an object with the same path is created while
            the object is being uploaded.

        :param content_encoding: (str)

        :param content_disposition: (str)

        :param content_type: (str)

        :param sharing: {'read':[user and/or grp names],
            'write':[usr and/or grp names]}

        :param public: (bool)

        :param container_info_cache: (dict) if given, avoid redundant calls to
        server for container info (block size and hash information)
        """
        self._assert_container()

        blocksize, blockhash, size, nblocks = self._get_file_block_info(
                fileobj=None, size=len(input_str), cache=container_info_cache)
        (hashes, hmap, offset) = ([], {}, 0)
        if not content_type:
            content_type = 'application/octet-stream'

523
        hashes = []
524
        hmap = {}
525
        for blockid in range(nblocks):
526
527
            start = blockid * blocksize
            block = input_str[start: (start + blocksize)]
528
            hashes.append(_pithos_hash(block, blockhash))
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
            hmap[hashes[blockid]] = (start, block)

        hashmap = dict(bytes=size, hashes=hashes)
        missing, obj_headers = self._create_object_or_get_missing_hashes(
            obj, hashmap,
            content_type=content_type,
            size=size,
            if_etag_match=if_etag_match,
            if_etag_not_match='*' if if_not_exist else None,
            content_encoding=content_encoding,
            content_disposition=content_disposition,
            permissions=sharing,
            public=public)
        if missing is None:
            return obj_headers
        num_of_missing = len(missing)

        if upload_cb:
547
548
            self.progress_bar_gen = upload_cb(nblocks)
            for i in range(nblocks + 1 - num_of_missing):
549
550
                self._cb_next()

551
552
        tries = 7
        old_failures = 0
553
        try:
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
            while tries and missing:
                flying = []
                failures = []
                for hash in missing:
                    offset, block = hmap[hash]
                    bird = self._put_block_async(block, hash)
                    flying.append(bird)
                    unfinished = self._watch_thread_limit(flying)
                    for thread in set(flying).difference(unfinished):
                        if thread.exception:
                            failures.append(thread.kwargs['hash'])
                        if thread.isAlive():
                            flying.append(thread)
                        else:
                            self._cb_next()
                    flying = unfinished
                for thread in flying:
                    thread.join()
572
                    if thread.exception:
573
574
575
576
577
578
579
580
581
582
                        failures.append(thread.kwargs['hash'])
                    self._cb_next()
                missing = failures
                if missing and len(missing) == old_failures:
                    tries -= 1
                old_failures = len(missing)
            if missing:
                raise ClientError(
                    '%s blocks failed to upload' % len(missing),
                    details=['%s' % thread.exception for thread in missing])
583
        except KeyboardInterrupt:
584
            sendlog.info('- - - wait for threads to finish')
585
586
587
588
            for thread in activethreads():
                thread.join()
            raise

589
        r = self.object_put(
590
            obj,
591
592
593
            format='json',
            hashmap=True,
            content_type=content_type,
594
            if_etag_match=if_etag_match,
595
596
            if_etag_not_match='*' if if_not_exist else None,
            etag=etag,
597
            json=hashmap,
598
599
            permissions=sharing,
            public=public,
600
            success=201)
601
        return r.headers
602

603
    # download_* auxiliary methods
Stavros Sachtouris's avatar
Stavros Sachtouris committed
604
    def _get_remote_blocks_info(self, obj, **restargs):
605
        #retrieve object hashmap
606
        myrange = restargs.pop('data_range', None)
607
        hashmap = self.get_object_hashmap(obj, **restargs)
608
        restargs['data_range'] = myrange
609
610
611
        blocksize = int(hashmap['block_size'])
        blockhash = hashmap['block_hash']
        total_size = hashmap['bytes']
Stavros Sachtouris's avatar
Stavros Sachtouris committed
612
        #assert total_size/blocksize + 1 == len(hashmap['hashes'])
613
        map_dict = {}
Stavros Sachtouris's avatar
Stavros Sachtouris committed
614
        for i, h in enumerate(hashmap['hashes']):
615
616
617
618
619
            #  map_dict[h] = i   CHAGE
            if h in map_dict:
                map_dict[h].append(i)
            else:
                map_dict[h] = [i]
Stavros Sachtouris's avatar
Stavros Sachtouris committed
620
        return (blocksize, blockhash, total_size, hashmap['hashes'], map_dict)
621

622
623
624
    def _dump_blocks_sync(
            self, obj, remote_hashes, blocksize, total_size, dst, range,
            **args):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
625
        for blockid, blockhash in enumerate(remote_hashes):
626
627
628
629
630
631
632
633
634
635
            if blockhash:
                start = blocksize * blockid
                is_last = start + blocksize > total_size
                end = (total_size - 1) if is_last else (start + blocksize - 1)
                (start, end) = _range_up(start, end, range)
                args['data_range'] = 'bytes=%s-%s' % (start, end)
                r = self.object_get(obj, success=(200, 206), **args)
                self._cb_next()
                dst.write(r.content)
                dst.flush()
Stavros Sachtouris's avatar
Stavros Sachtouris committed
636

637
638
    def _get_block_async(self, obj, **args):
        event = SilentEvent(self.object_get, obj, success=(200, 206), **args)
639
640
        event.start()
        return event
641

642
643
644
645
646
647
648
    def _hash_from_file(self, fp, start, size, blockhash):
        fp.seek(start)
        block = fp.read(size)
        h = newhashlib(blockhash)
        h.update(block.strip('\x00'))
        return hexlify(h.digest())

649
    def _thread2file(self, flying, blockids, local_file, offset=0, **restargs):
650
        """write the results of a greenleted rest call to a file
651
652
653
654
655

        :param offset: the offset of the file up to blocksize
        - e.g. if the range is 10-100, all blocks will be written to
        normal_position - 10
        """
656
        for key, g in flying.items():
657
658
659
660
661
662
663
            if g.isAlive():
                continue
            if g.exception:
                raise g.exception
            block = g.value.content
            for block_start in blockids[key]:
                local_file.seek(block_start + offset)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
664
665
                local_file.write(block)
                self._cb_next()
666
667
            flying.pop(key)
            blockids.pop(key)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
668
669
        local_file.flush()

670
671
672
    def _dump_blocks_async(
            self, obj, remote_hashes, blocksize, total_size, local_file,
            blockhash=None, resume=False, filerange=None, **restargs):
673
        file_size = fstat(local_file.fileno()).st_size if resume else 0
674
675
        flying = dict()
        blockid_dict = dict()
676
677
678
        offset = 0
        if filerange is not None:
            rstart = int(filerange.split('-')[0])
679
            offset = rstart if blocksize > rstart else rstart % blocksize
680

681
        self._init_thread_limit()
682
        for block_hash, blockids in remote_hashes.items():
683
684
685
686
687
688
689
            blockids = [blk * blocksize for blk in blockids]
            unsaved = [blk for blk in blockids if not (
                blk < file_size and block_hash == self._hash_from_file(
                        local_file, blk, blocksize, blockhash))]
            self._cb_next(len(blockids) - len(unsaved))
            if unsaved:
                key = unsaved[0]
690
                self._watch_thread_limit(flying.values())
691
692
                self._thread2file(
                    flying, blockid_dict, local_file, offset,
693
                    **restargs)
694
695
                end = total_size - 1 if (
                    key + blocksize > total_size) else key + blocksize - 1
696
                start, end = _range_up(key, end, filerange)
697
698
699
700
701
                if start == end:
                    self._cb_next()
                    continue
                restargs['async_headers'] = {
                    'Range': 'bytes=%s-%s' % (start, end)}
702
703
                flying[key] = self._get_block_async(obj, **restargs)
                blockid_dict[key] = unsaved
Stavros Sachtouris's avatar
Stavros Sachtouris committed
704

705
706
        for thread in flying.values():
            thread.join()
707
        self._thread2file(flying, blockid_dict, local_file, offset, **restargs)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
708

709
710
711
712
713
714
715
716
717
718
    def download_object(
            self, obj, dst,
            download_cb=None,
            version=None,
            resume=False,
            range_str=None,
            if_match=None,
            if_none_match=None,
            if_modified_since=None,
            if_unmodified_since=None):
719
        """Download an object (multiple connections, random blocks)
720
721
722
723
724
725
726
727
728
729
730

        :param obj: (str) remote object path

        :param dst: open file descriptor (wb+)

        :param download_cb: optional progress.bar object for downloading

        :param version: (str) file version

        :param resume: (bool) if set, preserve already downloaded file parts

731
        :param range_str: (str) from, to are file positions (int) in bytes
732
733
734
735
736
737
738

        :param if_match: (str)

        :param if_none_match: (str)

        :param if_modified_since: (str) formated date

739
        :param if_unmodified_since: (str) formated date"""
740
741
742
        restargs = dict(
            version=version,
            data_range=None if range_str is None else 'bytes=%s' % range_str,
Stavros Sachtouris's avatar
Stavros Sachtouris committed
743
744
745
746
747
            if_match=if_match,
            if_none_match=if_none_match,
            if_modified_since=if_modified_since,
            if_unmodified_since=if_unmodified_since)

748
749
        (
            blocksize,
Stavros Sachtouris's avatar
Stavros Sachtouris committed
750
751
            blockhash,
            total_size,
752
            hash_list,
Stavros Sachtouris's avatar
Stavros Sachtouris committed
753
754
755
756
            remote_hashes) = self._get_remote_blocks_info(obj, **restargs)
        assert total_size >= 0

        if download_cb:
757
            self.progress_bar_gen = download_cb(len(hash_list))
Stavros Sachtouris's avatar
Stavros Sachtouris committed
758
759
760
            self._cb_next()

        if dst.isatty():
761
762
            self._dump_blocks_sync(
                obj,
763
764
765
766
                hash_list,
                blocksize,
                total_size,
                dst,
767
                range_str,
768
                **restargs)
769
        else:
770
771
            self._dump_blocks_async(
                obj,
772
773
774
775
776
777
                remote_hashes,
                blocksize,
                total_size,
                dst,
                blockhash,
                resume,
778
                range_str,
779
                **restargs)
780
            if not range_str:
781
                dst.truncate(total_size)
782

Stavros Sachtouris's avatar
Stavros Sachtouris committed
783
784
        self._complete_cb()

785
786
787
788
789
790
791
792
793
    def download_to_string(
            self, obj,
            download_cb=None,
            version=None,
            range_str=None,
            if_match=None,
            if_none_match=None,
            if_modified_since=None,
            if_unmodified_since=None):
794
795
        """Download an object to a string (multiple connections). This method
        uses threads for http requests, but stores all content in memory.
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834

        :param obj: (str) remote object path

        :param download_cb: optional progress.bar object for downloading

        :param version: (str) file version

        :param range_str: (str) from, to are file positions (int) in bytes

        :param if_match: (str)

        :param if_none_match: (str)

        :param if_modified_since: (str) formated date

        :param if_unmodified_since: (str) formated date

        :returns: (str) the whole object contents
        """
        restargs = dict(
            version=version,
            data_range=None if range_str is None else 'bytes=%s' % range_str,
            if_match=if_match,
            if_none_match=if_none_match,
            if_modified_since=if_modified_since,
            if_unmodified_since=if_unmodified_since)

        (
            blocksize,
            blockhash,
            total_size,
            hash_list,
            remote_hashes) = self._get_remote_blocks_info(obj, **restargs)
        assert total_size >= 0

        if download_cb:
            self.progress_bar_gen = download_cb(len(hash_list))
            self._cb_next()

835
836
837
838
        num_of_blocks = len(remote_hashes)
        ret = [''] * num_of_blocks
        self._init_thread_limit()
        flying = dict()
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
        try:
            for blockid, blockhash in enumerate(remote_hashes):
                start = blocksize * blockid
                is_last = start + blocksize > total_size
                end = (total_size - 1) if is_last else (start + blocksize - 1)
                (start, end) = _range_up(start, end, range_str)
                if start < end:
                    self._watch_thread_limit(flying.values())
                    flying[blockid] = self._get_block_async(obj, **restargs)
                for runid, thread in flying.items():
                    if (blockid + 1) == num_of_blocks:
                        thread.join()
                    elif thread.isAlive():
                        continue
                    if thread.exception:
                        raise thread.exception
                    ret[runid] = thread.value.content
                    self._cb_next()
                    flying.pop(runid)
            return ''.join(ret)
        except KeyboardInterrupt:
            sendlog.info('- - - wait for threads to finish')
            for thread in activethreads():
                thread.join()
863

Stavros Sachtouris's avatar
Stavros Sachtouris committed
864
    #Command Progress Bar method
865
    def _cb_next(self, step=1):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
866
867
        if hasattr(self, 'progress_bar_gen'):
            try:
868
869
                for i in xrange(step):
                    self.progress_bar_gen.next()
Stavros Sachtouris's avatar
Stavros Sachtouris committed
870
871
            except:
                pass
872

Stavros Sachtouris's avatar
Stavros Sachtouris committed
873
874
875
    def _complete_cb(self):
        while True:
            try:
876
                self.progress_bar_gen.next()
Stavros Sachtouris's avatar
Stavros Sachtouris committed
877
878
            except:
                break
Stavros Sachtouris's avatar
Stavros Sachtouris committed
879

880
881
882
883
884
885
886
887
    def get_object_hashmap(
            self, obj,
            version=None,
            if_match=None,
            if_none_match=None,
            if_modified_since=None,
            if_unmodified_since=None,
            data_range=None):
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
        """
        :param obj: (str) remote object path

        :param if_match: (str)

        :param if_none_match: (str)

        :param if_modified_since: (str) formated date

        :param if_unmodified_since: (str) formated date

        :param data_range: (str) from-to where from and to are integers
            denoting file positions in bytes

        :returns: (list)
        """
904
        try:
905
906
            r = self.object_get(
                obj,
907
908
909
910
911
912
913
                hashmap=True,
                version=version,
                if_etag_match=if_match,
                if_etag_not_match=if_none_match,
                if_modified_since=if_modified_since,
                if_unmodified_since=if_unmodified_since,
                data_range=data_range)
914
915
916
917
        except ClientError as err:
            if err.status == 304 or err.status == 412:
                return {}
            raise
918
        return r.json
919

920
    def set_account_group(self, group, usernames):
921
922
923
924
925
        """
        :param group: (str)

        :param usernames: (list)
        """
926
927
        r = self.account_post(update=True, groups={group: usernames})
        return r
Stavros Sachtouris's avatar
Stavros Sachtouris committed
928

929
    def del_account_group(self, group):
930
931
932
        """
        :param group: (str)
        """
933
        self.account_post(update=True, groups={group: []})
934

Stavros Sachtouris's avatar
Stavros Sachtouris committed
935
    def get_account_info(self, until=None):
936
937
938
939
940
        """
        :param until: (str) formated date

        :returns: (dict)
        """
Stavros Sachtouris's avatar
Stavros Sachtouris committed
941
        r = self.account_head(until=until)
942
        if r.status_code == 401:
943
            raise ClientError("No authorization", status=401)
944
        return r.headers
945

Stavros Sachtouris's avatar
Stavros Sachtouris committed
946
    def get_account_quota(self):
947
948
949
        """
        :returns: (dict)
        """
950
951
        return filter_in(
            self.get_account_info(),
952
953
            'X-Account-Policy-Quota',
            exactMatch=True)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
954
955

    def get_account_versioning(self):
956
957
958
        """
        :returns: (dict)
        """
959
960
        return filter_in(
            self.get_account_info(),
961
962
            'X-Account-Policy-Versioning',
            exactMatch=True)
963

Stavros Sachtouris's avatar
Stavros Sachtouris committed
964
    def get_account_meta(self, until=None):
965
966
967
968
969
        """
        :meta until: (str) formated date

        :returns: (dict)
        """
970
        return filter_in(self.get_account_info(until=until), 'X-Account-Meta-')
971

972
    def get_account_group(self):
973
974
975
        """
        :returns: (dict)
        """
976
977
        return filter_in(self.get_account_info(), 'X-Account-Group-')

978
    def set_account_meta(self, metapairs):
979
980
981
        """
        :param metapairs: (dict) {key1:val1, key2:val2, ...}
        """
982
        assert(type(metapairs) is dict)
983
984
        r = self.account_post(update=True, metadata=metapairs)
        return r.headers
985

986
    def del_account_meta(self, metakey):
987
988
989
        """
        :param metakey: (str) metadatum key
        """
990
991
        r = self.account_post(update=True, metadata={metakey: ''})
        return r.headers
992

993
    """
Stavros Sachtouris's avatar
Stavros Sachtouris committed
994
    def set_account_quota(self, quota):
995
        ""
996
        :param quota: (int)
997
        ""
998
        self.account_post(update=True, quota=quota)
999
    """
Stavros Sachtouris's avatar
Stavros Sachtouris committed
1000
1001

    def set_account_versioning(self, versioning):
1002
1003
1004
        """
        "param versioning: (str)
        """
1005
1006
        r = self.account_post(update=True, versioning=versioning)
        return r.headers
Stavros Sachtouris's avatar
Stavros Sachtouris committed
1007

1008
    def list_containers(self):
1009
1010
1011
        """
        :returns: (dict)
        """
1012
        r = self.account_get()
1013
        return r.json
1014

1015
    def del_container(self, until=None, delimiter=None):
1016
1017
1018
        """
        :param until: (str) formated date

1019
        :param delimiter: (str) with / empty container
1020
1021
1022
1023
1024

        :raises ClientError: 404 Container does not exist

        :raises ClientError: 409 Container is not empty
        """
1025
        self._assert_container()
1026
1027
        r = self.container_delete(
            until=until,
1028
1029
            delimiter=delimiter,
            success=(204, 404, 409))
1030
        if r.status_code == 404:
1031
1032
            raise ClientError(
                'Container "%s" does not exist' % self.container,
1033
                r.status_code)
1034
        elif r.status_code == 409:
1035
1036
            raise ClientError(
                'Container "%s" is not empty' % self.container,
1037
                r.status_code)
1038
        return r.headers
1039

1040
    def get_container_versioning(self, container=None):
1041
1042
1043
1044
1045
        """
        :param container: (str)

        :returns: (dict)
        """
1046
1047
1048
1049
1050
1051
1052
1053
        cnt_back_up = self.container
        try:
            self.container = container or cnt_back_up
            return filter_in(
                self.get_container_info(),
                'X-Container-Policy-Versioning')
        finally:
            self.container = cnt_back_up
Stavros Sachtouris's avatar
Stavros Sachtouris committed
1054

1055
    def get_container_limit(self, container=None):
1056
1057
1058
1059
1060
        """
        :param container: (str)

        :returns: (dict)
        """
1061
1062
1063
1064
1065
1066
1067
1068
        cnt_back_up = self.container
        try:
            self.container = container or cnt_back_up
            return filter_in(
                self.get_container_info(),
                'X-Container-Policy-Quota')
        finally:
            self.container = cnt_back_up
1069

1070
    def get_container_info(self, until=None):
1071
1072
1073
1074
        """
        :param until: (str) formated date

        :returns: (dict)
1075
1076

        :raises ClientError: 404 Container not found
1077
        """
1078
1079
1080
1081
1082
        try:
            r = self.container_head(until=until)
        except ClientError as err:
            err.details.append('for container %s' % self.container)
            raise err
Stavros Sachtouris's avatar
Stavros Sachtouris committed
1083
1084
        return r.headers

1085
    def get_container_meta(self, until=None):
1086
1087
1088
1089
1090
        """
        :param until: (str) formated date

        :returns: (dict)
        """
1091
1092
        return filter_in(
            self.get_container_info(until=until),
1093
            'X-Container-Meta')
1094

1095
    def get_container_object_meta(self, until=None):
1096
1097
1098
1099
1100
        """
        :param until: (str) formated date

        :returns: (dict)
        """
1101
1102
        return filter_in(
            self.get_container_info(until=until),
1103
            'X-Container-Object-Meta')
1104

1105
    def set_container_meta(self, metapairs):
1106
1107
1108
        """
        :param metapairs: (dict) {key1:val1, key2:val2, ...}
        """
1109
        assert(type(metapairs) is dict)
1110
1111
        r = self.container_post(update=True, metadata=metapairs)
        return r.headers
1112

Stavros Sachtouris's avatar
Stavros Sachtouris committed
1113
    def del_container_meta(self, metakey):
1114
1115
1116
        """
        :param metakey: (str) metadatum key
        """
1117
1118
        r = self.container_post(update=True, metadata={metakey: ''})
        return r.headers
1119

1120
    def set_container_limit(self, limit):
1121
        """
1122
        :param limit: (int)
1123
        """
1124
1125
        r = self.container_post(update=True, quota=limit)
        return r.headers
Stavros Sachtouris's avatar
Stavros Sachtouris committed
1126
1127

    def set_c