/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tuned_gzip.py

[merge] robertc's integration, updated tests to check for retcode=3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
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
16
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 
 
18
 
"""Legacy breezy specific gzip tunings."""
19
 
 
20
 
from __future__ import absolute_import
21
 
 
22
 
import struct
23
 
import zlib
24
 
 
25
 
__all__ = ["chunks_to_gzip"]
26
 
 
27
 
 
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:
34
 
        i += 1 << 32
35
 
    return i
36
 
 
37
 
 
38
 
def LOWU32(i):
39
 
    """Return the low-order 32 bits of an int, as a non-negative int."""
40
 
    return i & 0xFFFFFFFF
41
 
 
42
 
 
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
 
    """
52
 
    result = [
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')
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)
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)
77
 
    result.append(compress.flush())
78
 
    # size may exceed 2GB, or even 4GB
79
 
    result.append(struct.pack("<LL", LOWU32(crc), LOWU32(total_len)))
80
 
    return result