Package skytools :: Module gzlog
[frames] | no frames]

Source Code for Module skytools.gzlog

 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  # gzip storage 
15  # 
16 -def gzip_append(filename, data, level = 6):
17 """Append a block of data to file with safety checks.""" 18 19 # compress data 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 # append, safely 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 # rollback on error 35 f.seek(pos, 0) 36 f.truncate() 37 f.close() 38 raise ex
39