~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tuned_gzip.py

  • Committer: Andrew Bennetts
  • Date: 2007-03-26 06:24:01 UTC
  • mto: This revision was merged to the branch mainline in revision 2376.
  • Revision ID: andrew.bennetts@canonical.com-20070326062401-k3nbefzje5332jaf
Deal with review comments from Robert:

  * Add my name to the NEWS file
  * Move the test case to a new module in branch_implementations
  * Remove revision_history cruft from identitymap and test_identitymap
  * Improve some docstrings

Also, this fixes a bug where revision_history was not returning a copy of the
cached data, allowing the cache to be corrupted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
# we want a \n preserved, break on \n only splitlines.
30
30
import bzrlib
31
31
 
32
 
__all__ = ["GzipFile", "bytes_to_gzip"]
33
 
 
34
 
 
35
 
def bytes_to_gzip(bytes, factory=zlib.compressobj,
36
 
    level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED,
37
 
    width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
38
 
    crc32=zlib.crc32):
39
 
    """Create a gzip file containing bytes and return its content."""
40
 
    result = [
41
 
        '\037\213'  # self.fileobj.write('\037\213')  # magic header
42
 
        '\010'      # self.fileobj.write('\010')      # compression method
43
 
                    # fname = self.filename[:-3]
44
 
                    # flags = 0
45
 
                    # if fname:
46
 
                    #     flags = FNAME
47
 
        '\x00'      # self.fileobj.write(chr(flags))
48
 
        '\0\0\0\0'  # write32u(self.fileobj, long(time.time()))
49
 
        '\002'      # self.fileobj.write('\002')
50
 
        '\377'      # self.fileobj.write('\377')
51
 
                    # if fname:
52
 
        ''          #     self.fileobj.write(fname + '\000')
53
 
        ]
54
 
    # using a compressobj avoids a small header and trailer that the compress()
55
 
    # utility function adds.
56
 
    compress = factory(level, method, width, mem, 0)
57
 
    result.append(compress.compress(bytes))
58
 
    result.append(compress.flush())
59
 
    result.append(struct.pack("<L", LOWU32(crc32(bytes))))
60
 
    # size may exceed 2GB, or even 4GB
61
 
    result.append(struct.pack("<L", LOWU32(len(bytes))))
62
 
    return ''.join(result)
 
32
__all__ = ["GzipFile"]
63
33
 
64
34
 
65
35
class GzipFile(gzip.GzipFile):
149
119
 
150
120
        if buf == "":
151
121
            self._add_read_data(self.decompress.flush())
152
 
            if len(self.decompress.unused_data) < 8:
153
 
                raise AssertionError("what does flush do?")
 
122
            assert len(self.decompress.unused_data) >= 8, "what does flush do?"
154
123
            self._gzip_tail = self.decompress.unused_data[0:8]
155
124
            self._read_eof()
156
125
            # tell the driving read() call we have stuffed all the data
176
145
                self._gzip_tail = self.decompress.unused_data[0:8]
177
146
            elif seek_length < 0:
178
147
                # we haven't read enough to check the checksum.
179
 
                if not (-8 < seek_length):
180
 
                    raise AssertionError("too great a seek")
 
148
                assert -8 < seek_length, "too great a seek."
181
149
                buf = self.fileobj.read(-seek_length)
182
150
                self._gzip_tail = self.decompress.unused_data + buf
183
151
            else:
202
170
        # We then check the that the computed CRC and size of the
203
171
        # uncompressed data matches the stored values.  Note that the size
204
172
        # stored is the true file size mod 2**32.
205
 
        if not (len(self._gzip_tail) == 8):
206
 
            raise AssertionError("gzip trailer is incorrect length.")
 
173
        assert len(self._gzip_tail) == 8, "gzip trailer is incorrect length."
207
174
        crc32, isize = struct.unpack("<LL", self._gzip_tail)
208
175
        # note that isize is unsigned - it can exceed 2GB
209
176
        if crc32 != U32(self.crc):