Mercurial > public > mercurial-scm > hg
diff 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 |
line wrap: on
line diff
--- a/mercurial/util.py Tue Sep 15 13:12:03 2015 -0700 +++ b/mercurial/util.py Tue Sep 15 17:35:32 2015 -0700 @@ -21,6 +21,8 @@ import os, time, datetime, calendar, textwrap, signal, collections import imp, socket, urllib import gc +import bz2 +import zlib if os.name == 'nt': import windows as platform @@ -2338,5 +2340,41 @@ yield path[:pos] pos = path.rfind('/', 0, pos) +# compression utility + +class nocompress(object): + def compress(self, x): + return x + def flush(self): + return "" + +compressors = { + 'UN': nocompress, + # lambda to prevent early import + 'BZ': lambda: bz2.BZ2Compressor(), + 'GZ': lambda: zlib.compressobj(), + } + +def _makedecompressor(decompcls): + def generator(f): + d = decompcls() + for chunk in filechunkiter(f): + yield d.decompress(chunk) + def func(fh): + return chunkbuffer(generator(fh)) + return func + +def _bz2(): + d = bz2.BZ2Decompressor() + # Bzip2 stream start with BZ, but we stripped it. + # we put it back for good measure. + d.decompress('BZ') + return d + +decompressors = {'UN': lambda fh: fh, + 'BZ': _makedecompressor(_bz2), + 'GZ': _makedecompressor(lambda: zlib.decompressobj()), + } + # convenient shortcut dst = debugstacktrace