summaryrefslogtreecommitdiff
path: root/python/skytools/gzlog.py
diff options
context:
space:
mode:
authorMarko Kreen2007-03-13 11:52:09 +0000
committerMarko Kreen2007-03-13 11:52:09 +0000
commit50abdba44a031ad40b1886f941479f203ca92039 (patch)
tree873e72d78cd48917b2907c4c63abf185649ebb54 /python/skytools/gzlog.py
final public releaseskytools_2_1
Diffstat (limited to 'python/skytools/gzlog.py')
-rw-r--r--python/skytools/gzlog.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/python/skytools/gzlog.py b/python/skytools/gzlog.py
new file mode 100644
index 00000000..558e2813
--- /dev/null
+++ b/python/skytools/gzlog.py
@@ -0,0 +1,39 @@
+
+"""Atomic append of gzipped data.
+
+The point is - if several gzip streams are concated, they
+are read back as one whose stream.
+"""
+
+import gzip
+from cStringIO import StringIO
+
+__all__ = ['gzip_append']
+
+#
+# gzip storage
+#
+def gzip_append(filename, data, level = 6):
+ """Append a block of data to file with safety checks."""
+
+ # compress data
+ buf = StringIO()
+ g = gzip.GzipFile(fileobj = buf, compresslevel = level, mode = "w")
+ g.write(data)
+ g.close()
+ zdata = buf.getvalue()
+
+ # append, safely
+ f = open(filename, "a+", 0)
+ f.seek(0, 2)
+ pos = f.tell()
+ try:
+ f.write(zdata)
+ f.close()
+ except Exception, ex:
+ # rollback on error
+ f.seek(pos, 0)
+ f.truncate()
+ f.close()
+ raise ex
+