1
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# Copyright (C) 2014 Aleksey Lim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sugar_network import toolkit
from sugar_network.toolkit import http, enforce
class Digest(str):
pass
def post(content, meta=None):
# if fileobj is tmp then move files
pass
def update(digest, meta):
pass
def get(digest):
pass
def delete(digest):
pass
def path(digest):
pass
"""
def diff(volume, in_seq, out_seq=None, exclude_seq=None, layer=None,
fetch_blobs=False, ignore_documents=None, **kwargs):
if 'blob' in meta:
blob_path = meta.pop('blob')
yield {'guid': guid,
'diff': {prop: meta},
'blob_size': meta['blob_size'],
'blob': toolkit.iter_file(blob_path),
}
elif fetch_blobs and 'url' in meta:
url = meta.pop('url')
try:
blob = connection.request('GET', url,
allow_redirects=True,
# We need uncompressed size
headers={'Accept-Encoding': ''})
except Exception:
_logger.exception('Cannot fetch %r for %s:%s:%s',
url, resource, guid, prop)
is_the_only_seq = False
continue
yield {'guid': guid,
'diff': {prop: meta},
'blob_size':
int(blob.headers['Content-Length']),
'blob': blob.iter_content(toolkit.BUFFER_SIZE),
}
else:
'digest': hashlib.sha1(png.getvalue()).hexdigest(),
if value is None:
value = {'blob': None}
elif isinstance(value, basestring) or hasattr(value, 'read'):
value = _read_blob(request, prop, value)
blobs.append(value['blob'])
elif isinstance(value, dict):
enforce('url' in value or 'blob' in value, 'No bundle')
else:
raise RuntimeError('Incorrect BLOB value')
def _read_blob(request, prop, value):
digest = hashlib.sha1()
dst = toolkit.NamedTemporaryFile(delete=False)
try:
if isinstance(value, basestring):
digest.update(value)
dst.write(value)
else:
size = request.content_length or sys.maxint
while size > 0:
chunk = value.read(min(size, toolkit.BUFFER_SIZE))
if not chunk:
break
dst.write(chunk)
size -= len(chunk)
digest.update(chunk)
except Exception:
os.unlink(dst.name)
raise
finally:
dst.close()
if request.prop and request.content_type:
mime_type = request.content_type
else:
mime_type = prop.mime_type
return {'blob': dst.name,
'digest': digest.hexdigest(),
'mime_type': mime_type,
}
)
"""
|