Revision 2715ade4 snf-pithos-tools/pithos/tools/lib/transfer.py
b/snf-pithos-tools/pithos/tools/lib/transfer.py | ||
---|---|---|
1 | 1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved. |
2 |
#
|
|
2 |
# |
|
3 | 3 |
# Redistribution and use in source and binary forms, with or |
4 | 4 |
# without modification, are permitted provided that the following |
5 | 5 |
# conditions are met: |
6 |
#
|
|
6 |
# |
|
7 | 7 |
# 1. Redistributions of source code must retain the above |
8 | 8 |
# copyright notice, this list of conditions and the following |
9 | 9 |
# disclaimer. |
10 |
#
|
|
10 |
# |
|
11 | 11 |
# 2. Redistributions in binary form must reproduce the above |
12 | 12 |
# copyright notice, this list of conditions and the following |
13 | 13 |
# disclaimer in the documentation and/or other materials |
14 | 14 |
# provided with the distribution. |
15 |
#
|
|
15 |
# |
|
16 | 16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
17 | 17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | 18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
... | ... | |
25 | 25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
26 | 26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | 27 |
# POSSIBILITY OF SUCH DAMAGE. |
28 |
#
|
|
28 |
# |
|
29 | 29 |
# The views and conclusions contained in the software and |
30 | 30 |
# documentation are those of the authors and should not be |
31 | 31 |
# interpreted as representing official policies, either expressed |
... | ... | |
42 | 42 |
|
43 | 43 |
from progress.bar import IncrementalBar |
44 | 44 |
|
45 |
|
|
45 | 46 |
def upload(client, path, container, prefix, name=None, mimetype=None): |
46 |
|
|
47 |
|
|
47 | 48 |
meta = client.retrieve_container_metadata(container) |
48 | 49 |
blocksize = int(meta['x-container-block-size']) |
49 | 50 |
blockhash = meta['x-container-block-hash'] |
50 |
|
|
51 |
|
|
51 | 52 |
size = os.path.getsize(path) |
52 | 53 |
hashes = HashMap(blocksize, blockhash) |
53 | 54 |
hashes.load(open(path)) |
54 | 55 |
map = {'bytes': size, 'hashes': [hexlify(x) for x in hashes]} |
55 |
|
|
56 |
|
|
56 | 57 |
objectname = name if name else os.path.split(path)[-1] |
57 | 58 |
object = prefix + objectname |
58 |
kwargs = {'mimetype':mimetype} if mimetype else {} |
|
59 |
kwargs = {'mimetype': mimetype} if mimetype else {}
|
|
59 | 60 |
v = None |
60 | 61 |
try: |
61 | 62 |
v = client.create_object_by_hashmap(container, object, map, **kwargs) |
... | ... | |
64 | 65 |
raise |
65 | 66 |
else: |
66 | 67 |
return v |
67 |
|
|
68 |
if type(fault.data) == types.StringType:
|
|
68 |
|
|
69 |
if isinstance(fault.data, types.StringType):
|
|
69 | 70 |
missing = json.loads(fault.data) |
70 |
elif type(fault.data) == types.ListType:
|
|
71 |
elif isinstance(fault.data, types.ListType):
|
|
71 | 72 |
missing = fault.data |
72 |
|
|
73 |
|
|
73 | 74 |
if '' in missing: |
74 | 75 |
del missing[missing.index(''):] |
75 |
|
|
76 |
|
|
76 | 77 |
bar = IncrementalBar('Uploading', max=len(missing)) |
77 | 78 |
bar.suffix = '%(percent).1f%% - %(eta)ds' |
78 | 79 |
with open(path) as fp: |
... | ... | |
83 | 84 |
client.update_container_data(container, StringIO(block)) |
84 | 85 |
bar.next() |
85 | 86 |
bar.finish() |
86 |
|
|
87 |
|
|
87 | 88 |
return client.create_object_by_hashmap(container, object, map, **kwargs) |
88 | 89 |
|
90 |
|
|
89 | 91 |
def download(client, container, object, path): |
90 |
|
|
92 |
|
|
91 | 93 |
res = client.retrieve_object_hashmap(container, object) |
92 | 94 |
blocksize = int(res['block_size']) |
93 | 95 |
blockhash = res['block_hash'] |
94 | 96 |
bytes = res['bytes'] |
95 | 97 |
map = res['hashes'] |
96 |
|
|
98 |
|
|
97 | 99 |
if os.path.exists(path): |
98 | 100 |
h = HashMap(blocksize, blockhash) |
99 | 101 |
h.load(open(path)) |
... | ... | |
101 | 103 |
else: |
102 | 104 |
open(path, 'w').close() # Create an empty file |
103 | 105 |
hashes = [] |
104 |
|
|
106 |
|
|
105 | 107 |
with open(path, 'a+') as fp: |
106 | 108 |
if bytes != 0: |
107 | 109 |
for i, h in enumerate(map): |
... | ... | |
109 | 111 |
continue |
110 | 112 |
start = i * blocksize |
111 | 113 |
end = '' if i == len(map) - 1 else ((i + 1) * blocksize) - 1 |
112 |
data = client.retrieve_object(container, object, range='bytes=%s-%s' % (start, end)) |
|
114 |
data = client.retrieve_object( |
|
115 |
container, object, range='bytes=%s-%s' % (start, end)) |
|
113 | 116 |
if i != len(map) - 1: |
114 | 117 |
data += (blocksize - len(data)) * '\x00' |
115 | 118 |
fp.seek(start) |
Also available in: Unified diff