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 |
|
1641.1.1
by Robert Collins
* Various microoptimisations to knit and gzip - reducing function call |
20 |
import struct |
21 |
import zlib |
|
22 |
||
6833.6.1
by Jelmer Vernooij
Remove bytes_to_gzip; work with chunks instead. |
23 |
__all__ = ["chunks_to_gzip"] |
2817.3.1
by Robert Collins
* New helper ``bzrlib.tuned_gzip.bytes_to_gzip`` which takes a byte string |
24 |
|
25 |
||
3734.2.1
by Vincent Ladeuil
Fix U32, LOWU32 disapearance in python-2.6. |
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: |
|
6621.4.2
by Martin
Make tuned_gzip and tests Python 3 compatible |
32 |
i += 1 << 32 |
3734.2.1
by Vincent Ladeuil
Fix U32, LOWU32 disapearance in python-2.6. |
33 |
return i |
34 |
||
35 |
||
36 |
def LOWU32(i): |
|
37 |
"""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 |
38 |
return i & 0xFFFFFFFF |
3734.2.1
by Vincent Ladeuil
Fix U32, LOWU32 disapearance in python-2.6. |
39 |
|
40 |
||
4398.8.2
by John Arbash Meinel
Add a chunks_to_gzip function. |
41 |
def chunks_to_gzip(chunks, factory=zlib.compressobj, |
7143.15.2
by Jelmer Vernooij
Run autopep8. |
42 |
level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED, |
43 |
width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL, |
|
44 |
crc32=zlib.crc32): |
|
4398.8.2
by John Arbash Meinel
Add a chunks_to_gzip function. |
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 |
"""
|
|
2817.3.1
by Robert Collins
* New helper ``bzrlib.tuned_gzip.bytes_to_gzip`` which takes a byte string |
50 |
result = [ |
6621.4.2
by Martin
Make tuned_gzip and tests Python 3 compatible |
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:
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
62 |
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 |
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) |
|
4398.8.2
by John Arbash Meinel
Add a chunks_to_gzip function. |
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) |
|
2817.3.1
by Robert Collins
* New helper ``bzrlib.tuned_gzip.bytes_to_gzip`` which takes a byte string |
75 |
result.append(compress.flush()) |
76 |
# size may exceed 2GB, or even 4GB
|
|
4398.8.2
by John Arbash Meinel
Add a chunks_to_gzip function. |
77 |
result.append(struct.pack("<LL", LOWU32(crc), LOWU32(total_len))) |
6855.2.2
by Jelmer Vernooij
Format strings are bytes. |
78 |
return result |