summaryrefslogtreecommitdiff
path: root/morphlib/cachedir.py
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2012-02-22 16:04:42 +0000
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2012-02-27 15:26:55 +0000
commit42a9f54d191940dd1a155e91363fb5041ca63c05 (patch)
tree2efe04c8420a0ea02dfeabf1cc9ea168f1fbc7c4 /morphlib/cachedir.py
parent6cffb0dfb9a2be837bbb9b3b8de26806bfd0360f (diff)
downloadmorph-42a9f54d191940dd1a155e91363fb5041ca63c05.tar.gz
Create chunks, strata in cache via temporary files
This avoids problems with files with the right names but partial content, if morph is killed in the middle of writing the file.
Diffstat (limited to 'morphlib/cachedir.py')
-rw-r--r--morphlib/cachedir.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/morphlib/cachedir.py b/morphlib/cachedir.py
index cc528a43..d0763c4c 100644
--- a/morphlib/cachedir.py
+++ b/morphlib/cachedir.py
@@ -17,6 +17,8 @@
import hashlib
import os
+import morphlib
+
class CacheDir(object):
@@ -62,3 +64,18 @@ class CacheDir(object):
return os.path.join(self.dirname, key + suffix)
+ def open(self, relative_name):
+ '''Open a file for writing in the cache.
+
+ The file will be written with a temporary name, and renamed to
+ the final name when the file is closed. Additionally, if the
+ caller decides, mid-writing, that they don't want to write the
+ file after all (e.g., a log file), then the ``abort`` method
+ in the returned file handle can be called to remove the
+ temporary file.
+
+ '''
+
+ path = os.path.join(self.dirname, relative_name)
+ return morphlib.savefile.SaveFile(path, 'w')
+