ia64/xen-unstable

changeset 4675:6639dd8a166b

bitkeeper revision 1.1327.2.12 (4270a5f3EYQTq9cfFkGDYjjJ7ckjuw)

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@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Thu Apr 28 08:59:31 2005 +0000 (2005-04-28)
parents 7719c5e6954d
children 20d715ae2dfb
files tools/python/xen/xend/XendDB.py
line diff
     1.1 --- a/tools/python/xen/xend/XendDB.py	Wed Apr 27 16:06:43 2005 +0000
     1.2 +++ b/tools/python/xen/xend/XendDB.py	Thu Apr 28 08:59:31 2005 +0000
     1.3 @@ -20,6 +20,12 @@ class XendDB:
     1.4              self.dbpath = os.path.join(self.dbpath, path)
     1.5          pass
     1.6  
     1.7 +    def listdir(self, dpath):
     1.8 +        try:
     1.9 +            return dircache.listdir(dpath)
    1.10 +        except:
    1.11 +            return []
    1.12 +
    1.13      def filepath(self, path):
    1.14          return os.path.join(self.dbpath, path)
    1.15          
    1.16 @@ -52,21 +58,37 @@ class XendDB:
    1.17          return self.savefile(fpath, sxpr)
    1.18      
    1.19      def savefile(self, fpath, sxpr):
    1.20 +        backup = False
    1.21          fdir = os.path.dirname(fpath)
    1.22          if not os.path.isdir(fdir):
    1.23              os.makedirs(fdir)
    1.24 +        if os.path.exists(fpath):
    1.25 +            backup = True
    1.26 +            real_fpath = fpath
    1.27 +            fpath += ".new."
    1.28 +            
    1.29          fout = file(fpath, "wb+")
    1.30          try:
    1.31 -            t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    1.32 -            fout.write("# %s %s\n" % (fpath, t))
    1.33 -            sxp.show(sxpr, out=fout)
    1.34 -        finally:
    1.35 -            fout.close()
    1.36 +            try:
    1.37 +                t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    1.38 +                fout.write("# %s %s\n" % (fpath, t))
    1.39 +                sxp.show(sxpr, out=fout)
    1.40 +            finally:
    1.41 +                fout.close()
    1.42 +        except:
    1.43 +            if backup:
    1.44 +                try:
    1.45 +                    os.unlink(fpath)
    1.46 +                except:
    1.47 +                    pass
    1.48 +                raise
    1.49 +        if backup:
    1.50 +            os.rename(fpath, real_fpath)
    1.51  
    1.52      def fetchall(self, path):
    1.53          dpath = self.filepath(path)
    1.54          d = {}
    1.55 -        for k in dircache.listdir(dpath):
    1.56 +        for k in self.listdir(dpath):
    1.57              try:
    1.58                  v = self.fetchfile(os.path.join(dpath, k))
    1.59                  d[k] = v
    1.60 @@ -84,8 +106,7 @@ class XendDB:
    1.61  
    1.62      def ls(self, path):
    1.63          dpath = self.filepath(path)
    1.64 -        return dircache.listdir(dpath)
    1.65 -            
    1.66 +        return self.listdir(dpath)
    1.67          
    1.68  
    1.69