configurationengine/source/cone/storage/zipstorage.py
changeset 5 d2c80f5cab53
parent 3 e7e0ae78773e
--- a/configurationengine/source/cone/storage/zipstorage.py	Wed Sep 08 12:20:56 2010 +0300
+++ b/configurationengine/source/cone/storage/zipstorage.py	Thu Oct 21 16:36:53 2010 +0300
@@ -55,6 +55,7 @@
         self.persistentmodule = persistentconfml
         self.compression = zipfile.ZIP_DEFLATED
         self.modified = False
+        self.cache = {}
         self.logger = logging.getLogger('cone')
         self.logger.debug("ZipStorage path %s open in mode %s" % (path,self.mode))
         try:
@@ -284,23 +285,31 @@
             self.zipfile.close()
             # Recreate the zip file if the zip has been modified to make a zip without 
             # duplicate local file entries
-            if self.modified:
-                oldfile = None
-                newzipfile = None
-                fh, tmp_path = tempfile.mkstemp(suffix='.zip')
-                shutil.move(self.path, tmp_path)
-                oldfile = zipfile.ZipFile(tmp_path,"r")
-                newzipfile = zipfile.ZipFile(self.path,"w",self.compression)
-                for fileinfo in oldfile.infolist():
-                    newzipfile.writestr(fileinfo, oldfile.read(fileinfo.filename))
-                if oldfile: oldfile.close()
-                if newzipfile: newzipfile.close()
-                os.close(fh)
-                os.unlink(tmp_path)
-            self.zipfile = None
         else:
             raise exceptions.StorageException('Storage %s has been already closed!' % self.path)
 
+        if self.modified or self.cache:
+            logging.getLogger('cone').debug("Recreating the ZIP output file")
+            oldfile = None
+            newzipfile = None
+            fh, tmp_path = tempfile.mkstemp(suffix='.zip')
+            shutil.move(self.path, tmp_path)
+            oldfile = zipfile.ZipFile(tmp_path,"r")
+            newzipfile = zipfile.ZipFile(self.path,"w",self.compression)
+            for fileinfo in oldfile.infolist():
+                if fileinfo.filename not in self.cache.keys():
+                    newzipfile.writestr(fileinfo, oldfile.read(fileinfo.filename))
+            for filename in sorted(self.cache.keys()):
+                logging.getLogger('cone').debug("Adding pre-cached file %s." % filename)
+                newzipfile.write(self.cache[filename], arcname=filename)
+            if oldfile: oldfile.close()
+            if newzipfile: newzipfile.close()
+            os.close(fh)
+            os.unlink(tmp_path)
+            logging.getLogger('cone').debug("Recreating the ZIP output file completed.")
+        
+        self.zipfile = None
+
     def unload(self, path, object):
         """
         Dump a given object to the storage (reference is fetched from the object)
@@ -383,4 +392,4 @@
         if self.content_info == None:
             self.content_info = api.make_content_info(self, self.handle.getvalue())
         
-        return self.content_info
\ No newline at end of file
+        return self.content_info