comparison mercurial/util.py @ 26266:1e042e31bd0c

changegroup: move all compressions utilities in util We'll reuse the compression for other things (next target bundle2), so let's make it more accessible and organised.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Tue, 15 Sep 2015 17:35:32 -0700
parents c5b2074ae8c0
children eca468b8fae4
comparison
equal deleted inserted replaced
26265:077f20eed4b2 26266:1e042e31bd0c
19 import errno, shutil, sys, tempfile, traceback 19 import errno, shutil, sys, tempfile, traceback
20 import re as remod 20 import re as remod
21 import os, time, datetime, calendar, textwrap, signal, collections 21 import os, time, datetime, calendar, textwrap, signal, collections
22 import imp, socket, urllib 22 import imp, socket, urllib
23 import gc 23 import gc
24 import bz2
25 import zlib
24 26
25 if os.name == 'nt': 27 if os.name == 'nt':
26 import windows as platform 28 import windows as platform
27 else: 29 else:
28 import posix as platform 30 import posix as platform
2336 pos = path.rfind('/') 2338 pos = path.rfind('/')
2337 while pos != -1: 2339 while pos != -1:
2338 yield path[:pos] 2340 yield path[:pos]
2339 pos = path.rfind('/', 0, pos) 2341 pos = path.rfind('/', 0, pos)
2340 2342
2343 # compression utility
2344
2345 class nocompress(object):
2346 def compress(self, x):
2347 return x
2348 def flush(self):
2349 return ""
2350
2351 compressors = {
2352 'UN': nocompress,
2353 # lambda to prevent early import
2354 'BZ': lambda: bz2.BZ2Compressor(),
2355 'GZ': lambda: zlib.compressobj(),
2356 }
2357
2358 def _makedecompressor(decompcls):
2359 def generator(f):
2360 d = decompcls()
2361 for chunk in filechunkiter(f):
2362 yield d.decompress(chunk)
2363 def func(fh):
2364 return chunkbuffer(generator(fh))
2365 return func
2366
2367 def _bz2():
2368 d = bz2.BZ2Decompressor()
2369 # Bzip2 stream start with BZ, but we stripped it.
2370 # we put it back for good measure.
2371 d.decompress('BZ')
2372 return d
2373
2374 decompressors = {'UN': lambda fh: fh,
2375 'BZ': _makedecompressor(_bz2),
2376 'GZ': _makedecompressor(lambda: zlib.decompressobj()),
2377 }
2378
2341 # convenient shortcut 2379 # convenient shortcut
2342 dst = debugstacktrace 2380 dst = debugstacktrace