/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5590.1.2 by John Arbash Meinel
Change tuned_gzip.GzipFile to be deprecated
1
# Copyright (C) 2006-2011 Canonical Ltd
1641.1.1 by Robert Collins
* Various microoptimisations to knit and gzip - reducing function call
2
# Written by Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1641.1.1 by Robert Collins
* Various microoptimisations to knit and gzip - reducing function call
17
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
18
"""Legacy breezy specific gzip tunings."""
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
19
6379.6.1 by Jelmer Vernooij
Import absolute_import in a few places.
20
from __future__ import absolute_import
21
1641.1.1 by Robert Collins
* Various microoptimisations to knit and gzip - reducing function call
22
import struct
23
import zlib
24
6833.6.1 by Jelmer Vernooij
Remove bytes_to_gzip; work with chunks instead.
25
__all__ = ["chunks_to_gzip"]
2817.3.1 by Robert Collins
* New helper ``bzrlib.tuned_gzip.bytes_to_gzip`` which takes a byte string
26
27
3734.2.1 by Vincent Ladeuil
Fix U32, LOWU32 disapearance in python-2.6.
28
def U32(i):
29
    """Return i as an unsigned integer, assuming it fits in 32 bits.
30
31
    If it's >= 2GB when viewed as a 32-bit unsigned int, return a long.
32
    """
33
    if i < 0:
6621.4.2 by Martin
Make tuned_gzip and tests Python 3 compatible
34
        i += 1 << 32
3734.2.1 by Vincent Ladeuil
Fix U32, LOWU32 disapearance in python-2.6.
35
    return i
36
37
38
def LOWU32(i):
39
    """Return the low-order 32 bits of an int, as a non-negative int."""
6621.4.2 by Martin
Make tuned_gzip and tests Python 3 compatible
40
    return i & 0xFFFFFFFF
3734.2.1 by Vincent Ladeuil
Fix U32, LOWU32 disapearance in python-2.6.
41
42
4398.8.2 by John Arbash Meinel
Add a chunks_to_gzip function.
43
def chunks_to_gzip(chunks, factory=zlib.compressobj,
44
    level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED,
45
    width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
46
    crc32=zlib.crc32):
47
    """Create a gzip file containing chunks and return its content.
48
49
    :param chunks: An iterable of strings. Each string can have arbitrary
50
        layout.
51
    """
2817.3.1 by Robert Collins
* New helper ``bzrlib.tuned_gzip.bytes_to_gzip`` which takes a byte string
52
    result = [
6621.4.2 by Martin
Make tuned_gzip and tests Python 3 compatible
53
        b'\037\213'  # self.fileobj.write('\037\213')  # magic header
54
        b'\010'      # self.fileobj.write('\010')      # compression method
55
                     # fname = self.filename[:-3]
56
                     # flags = 0
57
                     # if fname:
58
                     #     flags = FNAME
59
        b'\x00'      # self.fileobj.write(chr(flags))
60
        b'\0\0\0\0'  # write32u(self.fileobj, long(time.time()))
61
        b'\002'      # self.fileobj.write('\002')
62
        b'\377'      # self.fileobj.write('\377')
63
                     # if fname:
64
        b''          #     self.fileobj.write(fname + '\000')
2817.3.1 by Robert Collins
* New helper ``bzrlib.tuned_gzip.bytes_to_gzip`` which takes a byte string
65
        ]
66
    # using a compressobj avoids a small header and trailer that the compress()
67
    # utility function adds.
68
    compress = factory(level, method, width, mem, 0)
4398.8.2 by John Arbash Meinel
Add a chunks_to_gzip function.
69
    crc = 0
70
    total_len = 0
71
    for chunk in chunks:
72
        crc = crc32(chunk, crc)
73
        total_len += len(chunk)
74
        zbytes = compress.compress(chunk)
75
        if zbytes:
76
            result.append(zbytes)
2817.3.1 by Robert Collins
* New helper ``bzrlib.tuned_gzip.bytes_to_gzip`` which takes a byte string
77
    result.append(compress.flush())
78
    # size may exceed 2GB, or even 4GB
4398.8.2 by John Arbash Meinel
Add a chunks_to_gzip function.
79
    result.append(struct.pack("<LL", LOWU32(crc), LOWU32(total_len)))
6855.2.2 by Jelmer Vernooij
Format strings are bytes.
80
    return result