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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
# Copyright (C) 2012-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/>.
import os
import sys
import zlib
import time
import json
import struct
import logging
from types import GeneratorType
from os.path import dirname, exists, join
from sugar_network import toolkit
from sugar_network.toolkit.router import File
from sugar_network.toolkit import http, coroutine, BUFFER_SIZE, enforce
_FILENAME_SUFFIX = '.parcel'
_RESERVED_DISK_SPACE = 1024 * 1024
_ZLIB_WBITS = 15
_ZLIB_WBITS_SIZE = 32768 # 2 ** 15
_logger = logging.getLogger('parcel')
def decode(stream, limit=None):
_logger.debug('Decode %r stream limit=%r', stream, limit)
stream = _UnzipStream(stream, limit)
header = stream.read_record()
packet = _DecodeIterator(stream)
while True:
packet.next()
if packet.name == 'last':
break
packet.props.update(header)
yield packet
def encode(packets, limit=None, header=None, compresslevel=6):
_logger.debug('Encode %r packets limit=%r header=%r',
packets, limit, header)
ostream = _ZipStream(compresslevel)
if limit is None:
limit = sys.maxint
if header is None:
header = {}
chunk = ostream.write_record(header)
if chunk:
yield chunk
for packet, props, content in packets:
if props is None:
props = {}
props['packet'] = packet
chunk = ostream.write_record(props)
if chunk:
yield chunk
if content is None:
continue
content = iter(content)
try:
finalizing = False
record = next(content)
while True:
if record is None:
finalizing = True
record = next(content)
continue
blob_len = 0
if isinstance(record, File) and record.path:
blob_len = record.size
chunk = ostream.write_record(record,
None if finalizing else limit - blob_len)
if chunk is None:
_logger.debug('Reach the encoding limit')
if not isinstance(content, GeneratorType):
raise StopIteration()
finalizing = True
record = content.throw(StopIteration())
continue
if chunk:
yield chunk
if blob_len:
with file(record.path, 'rb') as blob:
while True:
chunk = blob.read(BUFFER_SIZE)
if not chunk:
break
blob_len -= len(chunk)
if not blob_len:
chunk += '\n'
chunk = ostream.write(chunk)
if chunk:
yield chunk
enforce(blob_len == 0, EOFError, 'Blob size mismatch')
record = next(content)
except StopIteration:
pass
chunk = ostream.write_record({'packet': 'last'})
if chunk:
yield chunk
chunk = ostream.flush()
if chunk:
yield chunk
def decode_dir(root, recipient=None, session=None):
for root, __, files in os.walk(root):
for filename in files:
if not filename.endswith(_FILENAME_SUFFIX):
continue
with file(join(root, filename), 'rb') as parcel:
for packet in decode(parcel):
if recipient is not None and packet['from'] == recipient:
if session and packet['session'] == session:
_logger.debug('Skip the same session %r parcel',
parcel.name)
else:
_logger.debug('Remove outdated %r parcel',
parcel.name)
os.unlink(parcel.name)
break
yield packet
def encode_dir(packets, root=None, limit=None, path=None, sender=None,
header=None):
if path is None:
if not exists(root):
os.makedirs(root)
path = toolkit.unique_filename(root, toolkit.uuid() + _FILENAME_SUFFIX)
if limit <= 0:
stat = os.statvfs(dirname(path))
limit = stat.f_bfree * stat.f_frsize - _RESERVED_DISK_SPACE
if header is None:
header = {}
if sender is not None:
header['from'] = sender
_logger.debug('Creating %r parcel limit=%s header=%r', path, limit, header)
with toolkit.NamedTemporaryFile(dir=dirname(path)) as parcel:
for chunk in encode(packets, limit, header):
parcel.write(chunk)
coroutine.dispatch()
parcel.flush()
os.fsync(parcel.fileno())
os.rename(parcel.name, path)
class _DecodeIterator(object):
def __init__(self, stream):
self._stream = stream
self.props = {}
self._name = None
self._shift = True
@property
def name(self):
return self._name
def next(self):
if self._shift:
for __ in self:
pass
if self._name is None:
raise EOFError()
self._shift = True
def __repr__(self):
return '<Packet %r>' % self.props
def __getitem__(self, key):
return self.props.get(key)
def __iter__(self):
while True:
record = self._stream.read_record()
if record is None:
self._name = None
raise EOFError()
if 'packet' in record:
self._name = record['packet'] or ''
self.props = record
self._shift = False
break
blob_len = record.get('content-length')
if blob_len is None:
yield record
continue
blob_len = int(blob_len)
with toolkit.NamedTemporaryFile() as blob:
while blob_len:
chunk = self._stream.read(min(blob_len, BUFFER_SIZE))
enforce(chunk, 'Blob size mismatch')
blob.write(chunk)
blob_len -= len(chunk)
blob.flush()
yield File(blob.name, meta=record)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
pass
class _ZipStream(object):
def __init__(self, compresslevel=6):
self._zipper = zlib.compressobj(compresslevel,
zlib.DEFLATED, -_ZLIB_WBITS, zlib.DEF_MEM_LEVEL, 0)
self._offset = 0
self._size = 0
self._crc = zlib.crc32('') & 0xffffffffL
def write_record(self, record, limit=None):
chunk = json.dumps(record) + '\n'
if limit is not None and self._offset + len(chunk) > limit:
return None
return self.write(chunk)
def write(self, chunk):
self._size += len(chunk)
self._crc = zlib.crc32(chunk, self._crc) & 0xffffffffL
chunk = self._zipper.compress(chunk)
if self._offset == 0:
chunk = '\037\213' + '\010' + chr(0) + \
struct.pack('<L', long(time.time())) + \
'\002' + '\377' + \
chunk
self._offset = _ZLIB_WBITS_SIZE
if chunk:
self._offset += len(chunk)
return chunk
def flush(self):
chunk = self._zipper.flush() + \
struct.pack('<L', self._crc) + \
struct.pack('<L', self._size & 0xffffffffL)
self._offset += len(chunk)
return chunk
class _UnzipStream(object):
def __init__(self, stream, limit):
self._stream = stream
self._limit = limit
self._unzipper = zlib.decompressobj(-_ZLIB_WBITS)
self._crc = zlib.crc32('') & 0xffffffffL
self._size = 0
self._buffer = ''
if self._limit is not None:
self._limit -= 10
magic = stream.read(2)
enforce(magic == '\037\213', http.BadRequest,
'Not a gzipped file')
enforce(ord(stream.read(1)) == 8, http.BadRequest,
'Unknown compression method')
enforce(ord(stream.read(1)) == 0, http.BadRequest,
'Gzip flags should be empty')
stream.read(6) # Ignore the rest of header
def read_record(self):
while True:
parts = self._buffer.split('\n', 1)
if len(parts) == 1:
if self._read(BUFFER_SIZE):
continue
return None
result, self._buffer = parts
if not result:
continue
return json.loads(result)
def read(self, size):
while len(self._buffer) == 0 and self._read(size):
pass
size = min(size, len(self._buffer))
result = self._buffer[:size]
self._buffer = self._buffer[size:]
return result
def _read(self, size):
if self._limit is not None:
size = min(size, self._limit)
chunk = self._stream.read(size)
if chunk:
if self._limit is not None:
self._limit -= len(chunk)
self._add_to_buffer(self._unzipper.decompress(chunk))
return True
enforce(len(self._unzipper.unused_data) >= 8, http.BadRequest,
'Malformed gzipped file')
crc = struct.unpack('<I', self._unzipper.unused_data[:4])[0]
enforce(crc == self._crc, http.BadRequest, 'CRC check failed')
size = struct.unpack('<I', self._unzipper.unused_data[4:8])[0]
enforce(size == self._size, http.BadRequest, 'Incorrect length')
return self._add_to_buffer(self._unzipper.flush())
def _add_to_buffer(self, chunk):
if not chunk:
return False
self._buffer += chunk
self._crc = zlib.crc32(chunk, self._crc) & 0xffffffffL
self._size += len(chunk)
return True
|