/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

  • Committer: Robert Collins
  • Date: 2005-12-24 02:20:45 UTC
  • mto: (1185.50.57 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1550.
  • Revision ID: robertc@robertcollins.net-20051224022045-14efc8dfa0e1a4e9
Start tests for api usage.

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
 
import struct
21
 
import zlib
22
 
 
23
 
__all__ = ["chunks_to_gzip"]
24
 
 
25
 
 
26
 
def U32(i):
27
 
    """Return i as an unsigned integer, assuming it fits in 32 bits.
28
 
 
29
 
    If it's >= 2GB when viewed as a 32-bit unsigned int, return a long.
30
 
    """
31
 
    if i < 0:
32
 
        i += 1 << 32
33
 
    return i
34
 
 
35
 
 
36
 
def LOWU32(i):
37
 
    """Return the low-order 32 bits of an int, as a non-negative int."""
38
 
    return i & 0xFFFFFFFF
39
 
 
40
 
 
41
 
def chunks_to_gzip(chunks, factory=zlib.compressobj,
42
 
                   level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED,
43
 
                   width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
44
 
                   crc32=zlib.crc32):
45
 
    """Create a gzip file containing chunks and return its content.
46
 
 
47
 
    :param chunks: An iterable of strings. Each string can have arbitrary
48
 
        layout.
49
 
    """
50
 
    result = [
51
 
        b'\037\213'  # self.fileobj.write('\037\213')  # magic header
52
 
        b'\010'      # self.fileobj.write('\010')      # compression method
53
 
                     # fname = self.filename[:-3]
54
 
                     # flags = 0
55
 
                     # if fname:
56
 
                     #     flags = FNAME
57
 
        b'\x00'      # self.fileobj.write(chr(flags))
58
 
        b'\0\0\0\0'  # write32u(self.fileobj, long(time.time()))
59
 
        b'\002'      # self.fileobj.write('\002')
60
 
        b'\377'      # self.fileobj.write('\377')
61
 
                     # if fname:
62
 
        b''  # self.fileobj.write(fname + '\000')
63
 
        ]
64
 
    # using a compressobj avoids a small header and trailer that the compress()
65
 
    # utility function adds.
66
 
    compress = factory(level, method, width, mem, 0)
67
 
    crc = 0
68
 
    total_len = 0
69
 
    for chunk in chunks:
70
 
        crc = crc32(chunk, crc)
71
 
        total_len += len(chunk)
72
 
        zbytes = compress.compress(chunk)
73
 
        if zbytes:
74
 
            result.append(zbytes)
75
 
    result.append(compress.flush())
76
 
    # size may exceed 2GB, or even 4GB
77
 
    result.append(struct.pack("<LL", LOWU32(crc), LOWU32(total_len)))
78
 
    return result