1
2 """Atomic append of gzipped data.
3
4 The point is - if several gzip streams are concated, they
5 are read back as one whose stream.
6 """
7
8 import gzip
9 from cStringIO import StringIO
10
11 __all__ = ['gzip_append']
12
13
14
15
17 """Append a block of data to file with safety checks."""
18
19
20 buf = StringIO()
21 g = gzip.GzipFile(fileobj = buf, compresslevel = level, mode = "w")
22 g.write(data)
23 g.close()
24 zdata = buf.getvalue()
25
26
27 f = open(filename, "a+", 0)
28 f.seek(0, 2)
29 pos = f.tell()
30 try:
31 f.write(zdata)
32 f.close()
33 except Exception, ex:
34
35 f.seek(pos, 0)
36 f.truncate()
37 f.close()
38 raise ex
39