__init__.py 44.8 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
    def purge_container(self, container=None):
75
76
        """Delete an empty container and destroy associated blocks
        """
77
78
79
        cnt_back_up = self.container
        try:
            self.container = container or cnt_back_up
80
            self.container_delete(until=unicode(time()))
81
82
        finally:
            self.container = cnt_back_up
83

84
85
86
87
88
89
90
91
92
93
    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):
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        """
        :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)
115
116

        :returns: (dict) created object metadata
117
        """
118
        self._assert_container()
119
120
121
122
123
124
125

        if withHashFile:
            data = f.read()
            try:
                import json
                data = json.dumps(json.loads(data))
            except ValueError:
126
                raise ClientError('"%s" is not json-formated' % f.name, 1)
127
            except SyntaxError:
128
129
                msg = '"%s" is not a valid hashmap file' % f.name
                raise ClientError(msg, 1)
130
            f = StringIO(data)
131
        else:
132
            data = f.read(size) if size else f.read()
133
        r = self.object_put(
134
            obj,
135
136
137
138
139
140
141
142
            data=data,
            etag=etag,
            content_encoding=content_encoding,
            content_disposition=content_disposition,
            content_type=content_type,
            permissions=sharing,
            public=public,
            success=201)
143
        return r.headers
144

145
146
147
148
149
150
151
152
    def create_object_by_manifestation(
            self, obj,
            etag=None,
            content_encoding=None,
            content_disposition=None,
            content_type=None,
            sharing=None,
            public=None):
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        """
        :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)
168
169

        :returns: (dict) created object metadata
170
        """
171
        self._assert_container()
172
        r = self.object_put(
173
            obj,
174
175
176
177
178
179
180
181
            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))
182
        return r.headers
183

184
    # upload_* auxiliary methods
185
    def _put_block_async(self, data, hash):
186
187
188
189
190
        event = SilentEvent(method=self._put_block, data=data, hash=hash)
        event.start()
        return event

    def _put_block(self, data, hash):
191
192
        r = self.container_post(
            update=True,
193
194
195
196
197
198
            content_type='application/octet-stream',
            content_length=len(data),
            data=data,
            format='json')
        assert r.json[0] == hash, 'Local hash does not match server'

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    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()
216
217
        blocksize = int(meta['x-container-block-size'])
        blockhash = meta['x-container-block-hash']
218
        size = size if size is not None else fstat(fileobj.fileno()).st_size
219
        nblocks = 1 + (size - 1) // blocksize
220
221
        return (blocksize, blockhash, size, nblocks)

222
    def _create_object_or_get_missing_hashes(
223
224
225
226
227
            self, obj, json,
            size=None,
            format='json',
            hashmap=True,
            content_type=None,
228
229
            if_etag_match=None,
            if_etag_not_match=None,
230
231
232
233
234
235
236
            content_encoding=None,
            content_disposition=None,
            permissions=None,
            public=None,
            success=(201, 409)):
        r = self.object_put(
            obj,
237
238
239
240
            format='json',
            hashmap=True,
            content_type=content_type,
            json=json,
241
242
            if_etag_match=if_etag_match,
            if_etag_not_match=if_etag_not_match,
243
244
245
246
            content_encoding=content_encoding,
            content_disposition=content_disposition,
            permissions=permissions,
            public=public,
247
            success=success)
248
        return (None if r.status_code == 201 else r.json), r.headers
249

250
    def _calculate_blocks_for_upload(
251
252
            self, blocksize, blockhash, size, nblocks, hashes, hmap, fileobj,
            hash_cb=None):
253
        offset = 0
254
255
256
257
258
        if hash_cb:
            hash_gen = hash_cb(nblocks)
            hash_gen.next()

        for i in range(nblocks):
259
            block = fileobj.read(min(blocksize, size - offset))
260
            bytes = len(block)
261
            hash = _pithos_hash(block, blockhash)
262
            hashes.append(hash)
263
            hmap[hash] = (offset, bytes)
264
265
266
            offset += bytes
            if hash_cb:
                hash_gen.next()
Stavros Sachtouris's avatar
Stavros Sachtouris committed
267
        msg = 'Failed to calculate uploaded blocks:'
268
        ' Offset and object size do not match'
Stavros Sachtouris's avatar
Stavros Sachtouris committed
269
        assert offset == size, msg
270

271
272
    def _upload_missing_blocks(self, missing, hmap, fileobj, upload_gen=None):
        """upload missing blocks asynchronously"""
273

274
        self._init_thread_limit()
275

276
        flying = []
277
        failures = []
278
        for hash in missing:
279
            offset, bytes = hmap[hash]
280
281
            fileobj.seek(offset)
            data = fileobj.read(bytes)
282
            r = self._put_block_async(data, hash)
283
            flying.append(r)
284
285
            unfinished = self._watch_thread_limit(flying)
            for thread in set(flying).difference(unfinished):
286
287
                if thread.exception:
                    failures.append(thread)
288
                    if isinstance(
289
290
291
                            thread.exception,
                            ClientError) and thread.exception.status == 502:
                        self.POOLSIZE = self._thread_limit
292
                elif thread.isAlive():
293
                    flying.append(thread)
294
295
296
297
298
                elif upload_gen:
                    try:
                        upload_gen.next()
                    except:
                        pass
299
300
301
302
            flying = unfinished

        for thread in flying:
            thread.join()
303
304
            if thread.exception:
                failures.append(thread)
305
306
307
308
309
            elif upload_gen:
                try:
                    upload_gen.next()
                except:
                    pass
310

311
        return [failure.kwargs['hash'] for failure in failures]
312

313
314
315
316
317
318
    def upload_object(
            self, obj, f,
            size=None,
            hash_cb=None,
            upload_cb=None,
            etag=None,
319
            if_etag_match=None,
320
            if_not_exist=None,
321
322
323
324
            content_encoding=None,
            content_disposition=None,
            content_type=None,
            sharing=None,
325
326
            public=None,
            container_info_cache=None):
327
328
329
330
331
332
333
334
335
336
337
338
        """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)

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

342
343
344
345
346
        :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.

347
348
349
350
351
352
353
354
355
356
        :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)
357
358
359

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

363
364
365
        block_info = (
            blocksize, blockhash, size, nblocks) = self._get_file_block_info(
                f, size, container_info_cache)
366
        (hashes, hmap, offset) = ([], {}, 0)
367
        if not content_type:
368
            content_type = 'application/octet-stream'
369

370
        self._calculate_blocks_for_upload(
371
            *block_info,
372
373
374
            hashes=hashes,
            hmap=hmap,
            fileobj=f,
375
376
377
            hash_cb=hash_cb)

        hashmap = dict(bytes=size, hashes=hashes)
378
        missing, obj_headers = self._create_object_or_get_missing_hashes(
379
            obj, hashmap,
380
381
            content_type=content_type,
            size=size,
382
383
            if_etag_match=if_etag_match,
            if_etag_not_match='*' if if_not_exist else None,
384
385
386
387
            content_encoding=content_encoding,
            content_disposition=content_disposition,
            permissions=sharing,
            public=public)
388
389

        if missing is None:
390
            return obj_headers
391

392
393
        if upload_cb:
            upload_gen = upload_cb(len(missing))
394
395
396
397
398
            for i in range(len(missing), len(hashmap['hashes']) + 1):
                try:
                    upload_gen.next()
                except:
                    upload_gen = None
399
400
401
        else:
            upload_gen = None

402
        retries = 7
403
        try:
404
            while retries:
405
                sendlog.info('%s blocks missing' % len(missing))
406
407
408
409
410
                num_of_blocks = len(missing)
                missing = self._upload_missing_blocks(
                    missing,
                    hmap,
                    f,
411
                    upload_gen)
412
413
414
415
416
                if missing:
                    if num_of_blocks == len(missing):
                        retries -= 1
                    else:
                        num_of_blocks = len(missing)
417
418
419
                else:
                    break
            if missing:
420
421
422
                raise ClientError(
                    '%s blocks failed to upload' % len(missing),
                    details=['%s' % thread.exception for thread in missing])
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
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
        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'

        num_of_blocks, blockmod = size / blocksize, size % blocksize
        num_of_blocks += (1 if blockmod else 0) if num_of_blocks else blockmod

501
        hashes = []
502
503
504
505
        hmap = {}
        for blockid in range(num_of_blocks):
            start = blockid * blocksize
            block = input_str[start: (start + blocksize)]
506
            hashes.append(_pithos_hash(block, blockhash))
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
            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:
            self.progress_bar_gen = upload_cb(num_of_blocks)
            for i in range(num_of_blocks + 1 - num_of_missing):
                self._cb_next()

529
530
        tries = 7
        old_failures = 0
531
        try:
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
            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()
550
                    if thread.exception:
551
552
553
554
555
556
557
558
559
560
                        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])
561
        except KeyboardInterrupt:
562
            sendlog.info('- - - wait for threads to finish')
563
564
565
566
            for thread in activethreads():
                thread.join()
            raise

567
        r = self.object_put(
568
            obj,
569
570
571
            format='json',
            hashmap=True,
            content_type=content_type,
572
            if_etag_match=if_etag_match,
573
574
            if_etag_not_match='*' if if_not_exist else None,
            etag=etag,
575
            json=hashmap,
576
577
            permissions=sharing,
            public=public,
578
            success=201)
579
        return r.headers
580

581
    # download_* auxiliary methods
Stavros Sachtouris's avatar
Stavros Sachtouris committed
582
    def _get_remote_blocks_info(self, obj, **restargs):
583
        #retrieve object hashmap
584
        myrange = restargs.pop('data_range', None)
585
        hashmap = self.get_object_hashmap(obj, **restargs)
586
        restargs['data_range'] = myrange
587
588
589
        blocksize = int(hashmap['block_size'])
        blockhash = hashmap['block_hash']
        total_size = hashmap['bytes']
Stavros Sachtouris's avatar
Stavros Sachtouris committed
590
        #assert total_size/blocksize + 1 == len(hashmap['hashes'])
591
        map_dict = {}
Stavros Sachtouris's avatar
Stavros Sachtouris committed
592
        for i, h in enumerate(hashmap['hashes']):
593
594
595
596
597
            #  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
598
        return (blocksize, blockhash, total_size, hashmap['hashes'], map_dict)
599

600
601
602
    def _dump_blocks_sync(
            self, obj, remote_hashes, blocksize, total_size, dst, range,
            **args):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
603
        for blockid, blockhash in enumerate(remote_hashes):
604
605
606
607
608
609
610
611
612
613
            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
614

615
616
    def _get_block_async(self, obj, **args):
        event = SilentEvent(self.object_get, obj, success=(200, 206), **args)
617
618
        event.start()
        return event
619

620
621
622
623
624
625
626
    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())

627
    def _thread2file(self, flying, blockids, local_file, offset=0, **restargs):
628
        """write the results of a greenleted rest call to a file
629
630
631
632
633

        :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
        """
634
        for key, g in flying.items():
635
636
637
638
639
640
641
            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
642
643
                local_file.write(block)
                self._cb_next()
644
645
            flying.pop(key)
            blockids.pop(key)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
646
647
        local_file.flush()

648
649
650
    def _dump_blocks_async(
            self, obj, remote_hashes, blocksize, total_size, local_file,
            blockhash=None, resume=False, filerange=None, **restargs):
651
        file_size = fstat(local_file.fileno()).st_size if resume else 0
652
653
        flying = dict()
        blockid_dict = dict()
654
655
656
        offset = 0
        if filerange is not None:
            rstart = int(filerange.split('-')[0])
657
            offset = rstart if blocksize > rstart else rstart % blocksize
658

659
        self._init_thread_limit()
660
        for block_hash, blockids in remote_hashes.items():
661
662
663
664
665
666
667
            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]
668
                self._watch_thread_limit(flying.values())
669
670
                self._thread2file(
                    flying, blockid_dict, local_file, offset,
671
                    **restargs)
672
673
                end = total_size - 1 if (
                    key + blocksize > total_size) else key + blocksize - 1
674
                start, end = _range_up(key, end, filerange)
675
676
677
678
679
                if start == end:
                    self._cb_next()
                    continue
                restargs['async_headers'] = {
                    'Range': 'bytes=%s-%s' % (start, end)}
680
681
                flying[key] = self._get_block_async(obj, **restargs)
                blockid_dict[key] = unsaved
Stavros Sachtouris's avatar
Stavros Sachtouris committed
682

683
684
        for thread in flying.values():
            thread.join()
685
        self._thread2file(flying, blockid_dict, local_file, offset, **restargs)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
686

687
688
689
690
691
692
693
694
695
696
    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):
697
        """Download an object (multiple connections, random blocks)
698
699
700
701
702
703
704
705
706
707
708

        :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

709
        :param range_str: (str) from, to are file positions (int) in bytes
710
711
712
713
714
715
716

        :param if_match: (str)

        :param if_none_match: (str)

        :param if_modified_since: (str) formated date

717
        :param if_unmodified_since: (str) formated date"""
718
719
720
        restargs = dict(
            version=version,
            data_range=None if range_str is None else 'bytes=%s' % range_str,
Stavros Sachtouris's avatar
Stavros Sachtouris committed
721
722
723
724
725
            if_match=if_match,
            if_none_match=if_none_match,
            if_modified_since=if_modified_since,
            if_unmodified_since=if_unmodified_since)

726
727
        (
            blocksize,
Stavros Sachtouris's avatar
Stavros Sachtouris committed
728
729
            blockhash,
            total_size,
730
            hash_list,
Stavros Sachtouris's avatar
Stavros Sachtouris committed
731
732
733
734
            remote_hashes) = self._get_remote_blocks_info(obj, **restargs)
        assert total_size >= 0

        if download_cb:
735
            self.progress_bar_gen = download_cb(len(hash_list))
Stavros Sachtouris's avatar
Stavros Sachtouris committed
736
737
738
            self._cb_next()

        if dst.isatty():
739
740
            self._dump_blocks_sync(
                obj,
741
742
743
744
                hash_list,
                blocksize,
                total_size,
                dst,
745
                range_str,
746
                **restargs)
747
        else:
748
749
            self._dump_blocks_async(
                obj,
750
751
752
753
754
755
                remote_hashes,
                blocksize,
                total_size,
                dst,
                blockhash,
                resume,
756
                range_str,
757
                **restargs)
758
            if not range_str:
759
                dst.truncate(total_size)
760

Stavros Sachtouris's avatar
Stavros Sachtouris committed
761
762
        self._complete_cb()

763
764
765
766
767
768
769
770
771
    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):
772
773
        """Download an object to a string (multiple connections). This method
        uses threads for http requests, but stores all content in memory.
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812

        :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()

813
814
815
816
        num_of_blocks = len(remote_hashes)
        ret = [''] * num_of_blocks
        self._init_thread_limit()
        flying = dict()
817
818
819
820
821
        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)
822
823
824
825
826
827
828
829
830
831
832
833
834
835
            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)
836

Stavros Sachtouris's avatar
Stavros Sachtouris committed
837
    #Command Progress Bar method
838
    def _cb_next(self, step=1):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
839
840
        if hasattr(self, 'progress_bar_gen'):
            try:
841
842
                for i in xrange(step):
                    self.progress_bar_gen.next()
Stavros Sachtouris's avatar
Stavros Sachtouris committed
843
844
            except:
                pass
845

Stavros Sachtouris's avatar
Stavros Sachtouris committed
846
847
848
    def _complete_cb(self):
        while True:
            try:
849
                self.progress_bar_gen.next()
Stavros Sachtouris's avatar
Stavros Sachtouris committed
850
851
            except:
                break
Stavros Sachtouris's avatar
Stavros Sachtouris committed
852

853
854
855
856
857
858
859
860
    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):
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
        """
        :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)
        """
877
        try:
878
879
            r = self.object_get(
                obj,
880
881
882
883
884
885
886
                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)
887
888
889
890
        except ClientError as err:
            if err.status == 304 or err.status == 412:
                return {}
            raise
891
        return r.json
892

893
    def set_account_group(self, group, usernames):
894
895
896
897
898
        """
        :param group: (str)

        :param usernames: (list)
        """
899
        self.account_post(update=True, groups={group: usernames})
Stavros Sachtouris's avatar
Stavros Sachtouris committed
900

901
    def del_account_group(self, group):
902
903
904
        """
        :param group: (str)
        """
905
        self.account_post(update=True, groups={group: []})
906

Stavros Sachtouris's avatar
Stavros Sachtouris committed
907
    def get_account_info(self, until=None):
908
909
910
911
912
        """
        :param until: (str) formated date

        :returns: (dict)
        """
Stavros Sachtouris's avatar
Stavros Sachtouris committed
913
        r = self.account_head(until=until)
914
        if r.status_code == 401:
915
            raise ClientError("No authorization", status=401)
916
        return r.headers
917

Stavros Sachtouris's avatar
Stavros Sachtouris committed
918
    def get_account_quota(self):
919
920
921
        """
        :returns: (dict)
        """
922
923
        return filter_in(
            self.get_account_info(),
924
925
            'X-Account-Policy-Quota',
            exactMatch=True)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
926
927

    def get_account_versioning(self):
928
929
930
        """
        :returns: (dict)
        """
931
932
        return filter_in(
            self.get_account_info(),
933
934
            'X-Account-Policy-Versioning',
            exactMatch=True)
935

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

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

944
    def get_account_group(self):
945
946
947
        """
        :returns: (dict)
        """
948
949
        return filter_in(self.get_account_info(), 'X-Account-Group-')

950
    def set_account_meta(self, metapairs):
951
952
953
        """
        :param metapairs: (dict) {key1:val1, key2:val2, ...}
        """
954
        assert(type(metapairs) is dict)
955
        self.account_post(update=True, metadata=metapairs)
956

957
    def del_account_meta(self, metakey):
958
959
960
        """
        :param metakey: (str) metadatum key
        """
961
        self.account_post(update=True, metadata={metakey: ''})
962

963
    """
Stavros Sachtouris's avatar
Stavros Sachtouris committed
964
    def set_account_quota(self, quota):
965
        ""
966
        :param quota: (int)
967
        ""
968
        self.account_post(update=True, quota=quota)
969
    """
Stavros Sachtouris's avatar
Stavros Sachtouris committed
970
971

    def set_account_versioning(self, versioning):
972
973
974
        """
        "param versioning: (str)
        """
975
        self.account_post(update=True, versioning=versioning)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
976

977
    def list_containers(self):
978
979
980
        """
        :returns: (dict)
        """
981
        r = self.account_get()
982
        return r.json
983

984
    def del_container(self, until=None, delimiter=None):
985
986
987
        """
        :param until: (str) formated date

988
        :param delimiter: (str) with / empty container
989
990
991
992
993

        :raises ClientError: 404 Container does not exist

        :raises ClientError: 409 Container is not empty
        """
994
        self._assert_container()
995
996
        r = self.container_delete(
            until=until,
997
998
            delimiter=delimiter,
            success=(204, 404, 409))
999
        if r.status_code == 404:
1000
1001
            raise ClientError(
                'Container "%s" does not exist' % self.container,
1002
                r.status_code)
1003
        elif r.status_code == 409:
1004
1005
            raise ClientError(
                'Container "%s" is not empty' % self.container,
1006
                r.status_code)
1007

1008
    def get_container_versioning(self, container=None):
1009
1010
1011
1012
1013
        """
        :param container: (str)

        :returns: (dict)
        """
1014
1015
1016
1017
1018
1019
1020
1021
        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
1022

1023
    def get_container_limit(self, container=None):
1024
1025
1026
1027
1028
        """
        :param container: (str)

        :returns: (dict)
        """
1029
1030
1031
1032
1033
1034
1035
1036
        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
1037

1038
    def get_container_info(self, until=None):
1039
1040
1041
1042
        """
        :param until: (str) formated date

        :returns: (dict)
1043
1044

        :raises ClientError: 404 Container not found
1045
        """
1046
1047
1048
1049
1050
        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
1051
1052
        return r.headers

1053
    def get_container_meta(self, until=None):
1054
1055
1056
1057
1058
        """
        :param until: (str) formated date

        :returns: (dict)
        """
1059
1060
        return filter_in(
            self.get_container_info(until=until),
1061
            'X-Container-Meta')
1062

1063
    def get_container_object_meta(self, until=None):
1064
1065
1066
1067
1068
        """
        :param until: (str) formated date

        :returns: (dict)
        """
1069
1070
        return filter_in(
            self.get_container_info(until=until),
1071
            'X-Container-Object-Meta')
1072

1073
    def set_container_meta(self, metapairs):
1074
1075
1076
        """
        :param metapairs: (dict) {key1:val1, key2:val2, ...}
        """
1077
        assert(type(metapairs) is dict)
1078
        self.container_post(update=True, metadata=metapairs)
1079

Stavros Sachtouris's avatar
Stavros Sachtouris committed
1080
    def del_container_meta(self, metakey):
1081
1082
1083
        """
        :param metakey: (str) metadatum key
        """
1084
        self.container_post(update=True, metadata={metakey: ''})
1085

1086
    def set_container_limit(self, limit):
1087
        """
1088
        :param limit: (int)
1089
        """
1090
        self.container_post(update=True, quota=limit)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
1091
1092

    def set_container_versioning(self, versioning):
1093
1094
1095
        """
        :param versioning: (str)
        """
1096
        self.container_post(update=True, versioning=versioning)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
1097

1098
    def del_object(self, obj, until=None, delimiter=None):
1099
1100
1101
1102
1103
1104
1105
        """
        :param obj: (str) remote object path

        :param until: (str) formated date

        :param delimiter: (str)
        """
1106
        self._assert_container()
1107
        self.object_delete(obj, until=until, delimiter=delimiter)
1108

1109
1110
1111
1112
1113
1114
    def set_object_meta(self, obj, metapairs):
        """
        :param obj: (str) remote object path

        :param metapairs: (dict) {key1:val1, key2:val2, ...}
        """