~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tuned_gzip.py

  • Committer: Jelmer Vernooij
  • Date: 2009-06-09 00:59:51 UTC
  • mto: (4443.1.1 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 4444.
  • Revision ID: jelmer@samba.org-20090609005951-apv900cdk35o2ygh
Move squashing of XML-invalid characters to XMLSerializer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
# Written by Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
27
27
import zlib
28
28
 
29
29
# we want a \n preserved, break on \n only splitlines.
30
 
from bzrlib import symbol_versioning
 
30
import bzrlib
31
31
 
32
32
__all__ = ["GzipFile", "bytes_to_gzip"]
33
33
 
52
52
    width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
53
53
    crc32=zlib.crc32):
54
54
    """Create a gzip file containing bytes and return its content."""
55
 
    return chunks_to_gzip([bytes])
56
 
 
57
 
 
58
 
def chunks_to_gzip(chunks, factory=zlib.compressobj,
59
 
    level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED,
60
 
    width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
61
 
    crc32=zlib.crc32):
62
 
    """Create a gzip file containing chunks and return its content.
63
 
 
64
 
    :param chunks: An iterable of strings. Each string can have arbitrary
65
 
        layout.
66
 
    """
67
55
    result = [
68
56
        '\037\213'  # self.fileobj.write('\037\213')  # magic header
69
57
        '\010'      # self.fileobj.write('\010')      # compression method
81
69
    # using a compressobj avoids a small header and trailer that the compress()
82
70
    # utility function adds.
83
71
    compress = factory(level, method, width, mem, 0)
84
 
    crc = 0
85
 
    total_len = 0
86
 
    for chunk in chunks:
87
 
        crc = crc32(chunk, crc)
88
 
        total_len += len(chunk)
89
 
        zbytes = compress.compress(chunk)
90
 
        if zbytes:
91
 
            result.append(zbytes)
 
72
    result.append(compress.compress(bytes))
92
73
    result.append(compress.flush())
 
74
    result.append(struct.pack("<L", LOWU32(crc32(bytes))))
93
75
    # size may exceed 2GB, or even 4GB
94
 
    result.append(struct.pack("<LL", LOWU32(crc), LOWU32(total_len)))
 
76
    result.append(struct.pack("<L", LOWU32(len(bytes))))
95
77
    return ''.join(result)
96
78
 
97
79
 
118
100
    Yes, its only 1.6 seconds, but they add up.
119
101
    """
120
102
 
121
 
    def __init__(self, *args, **kwargs):
122
 
        symbol_versioning.warn(
123
 
            symbol_versioning.deprecated_in((2, 3, 0))
124
 
            % 'bzrlib.tuned_gzip.GzipFile',
125
 
            DeprecationWarning, stacklevel=2)
126
 
        gzip.GzipFile.__init__(self, *args, **kwargs)
127
 
 
128
103
    def _add_read_data(self, data):
129
104
        # 4169 calls in 183
130
105
        # temp var for len(data) and switch to +='s.
402
377
        # (4 seconds to 1 seconds for the sample upgrades I was testing).
403
378
        self.write(''.join(lines))
404
379
 
405
 
    if sys.version_info > (2, 7):
406
 
        # As of Python 2.7 the crc32 must be positive when close is called
407
 
        def close(self):
408
 
            if self.fileobj is None:
409
 
                return
410
 
            if self.mode == gzip.WRITE:
411
 
                self.crc &= 0xFFFFFFFFL
412
 
            gzip.GzipFile.close(self)
413
380