[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] Fix problem with Python 2.4 caused by change in error behaviour



ChangeSet 1.1327.2.12, 2005/04/28 09:59:31+01:00, mjw@xxxxxxxxxxxxxxxxxxx

        Fix problem with Python 2.4 caused by change in error behaviour
        of dircache.listdir. In 2.4 it throws an error on non-existent
        directories instead of returning an empty list.
        See 'Porting to 2.4' in the Python release notes.
        
        Signed-off-by: Mike Wray <mike.wray@xxxxxx>



 XendDB.py |   37 +++++++++++++++++++++++++++++--------
 1 files changed, 29 insertions(+), 8 deletions(-)


diff -Nru a/tools/python/xen/xend/XendDB.py b/tools/python/xen/xend/XendDB.py
--- a/tools/python/xen/xend/XendDB.py   2005-05-13 16:04:40 -04:00
+++ b/tools/python/xen/xend/XendDB.py   2005-05-13 16:04:40 -04:00
@@ -20,6 +20,12 @@
             self.dbpath = os.path.join(self.dbpath, path)
         pass
 
+    def listdir(self, dpath):
+        try:
+            return dircache.listdir(dpath)
+        except:
+            return []
+
     def filepath(self, path):
         return os.path.join(self.dbpath, path)
         
@@ -52,21 +58,37 @@
         return self.savefile(fpath, sxpr)
     
     def savefile(self, fpath, sxpr):
+        backup = False
         fdir = os.path.dirname(fpath)
         if not os.path.isdir(fdir):
             os.makedirs(fdir)
+        if os.path.exists(fpath):
+            backup = True
+            real_fpath = fpath
+            fpath += ".new."
+            
         fout = file(fpath, "wb+")
         try:
-            t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
-            fout.write("# %s %s\n" % (fpath, t))
-            sxp.show(sxpr, out=fout)
-        finally:
-            fout.close()
+            try:
+                t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
+                fout.write("# %s %s\n" % (fpath, t))
+                sxp.show(sxpr, out=fout)
+            finally:
+                fout.close()
+        except:
+            if backup:
+                try:
+                    os.unlink(fpath)
+                except:
+                    pass
+                raise
+        if backup:
+            os.rename(fpath, real_fpath)
 
     def fetchall(self, path):
         dpath = self.filepath(path)
         d = {}
-        for k in dircache.listdir(dpath):
+        for k in self.listdir(dpath):
             try:
                 v = self.fetchfile(os.path.join(dpath, k))
                 d[k] = v
@@ -84,8 +106,7 @@
 
     def ls(self, path):
         dpath = self.filepath(path)
-        return dircache.listdir(dpath)
-            
+        return self.listdir(dpath)
         
 
         

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.