← Back to team overview

gtg team mailing list archive

[Merge] lp:~epdmitry/gtg/develop into lp:gtg

 

Dmitry Ermolov has proposed merging lp:~epdmitry/gtg/develop into lp:gtg.

Requested reviews:
  Gtg developers (gtg)


I observed a couple of GTG crashes, when writing of xml data files was not completed and GTG failed to restart, because it couldn't read this corrupted files.

I propose to change GTG.tools.cleanxml.savexml function so it writes data to temporary file and rename this temporary file when writing is successfully completed.
-- 
https://code.launchpad.net/~epdmitry/gtg/develop/+merge/26972
Your team Gtg developers is requested to review the proposed merge of lp:~epdmitry/gtg/develop into lp:gtg.
=== modified file 'GTG/tools/cleanxml.py'
--- GTG/tools/cleanxml.py	2010-05-26 09:54:42 +0000
+++ GTG/tools/cleanxml.py	2010-06-07 17:35:36 +0000
@@ -111,44 +111,40 @@
     
 #write a XML doc to a file
 def savexml(zefile,doc,backup=False):
-#    print "writing %s file (%s)" %(zefile,opt)
     tmpfile = zefile+'__'
     try:
-        if os.path.exists(zefile):
-            os.rename(zefile, tmpfile)
-        f = open(zefile, mode='w+')
         pretty = doc.toprettyxml(tab, enter).encode("utf-8")
-        if f and pretty:
+        if not pretty:
+            print "no pretty xml"
+            return False
+
+        # write data to temporary file,
+        # rename file after it is successfuly written
+        with open(tmpfile, mode='w+') as f:
             bwritten = os.write(f.fileno(), pretty)
-            if bwritten != len(pretty):
-                print "error writing file %s" % zefile
-                f.close()
-                return False
-            f.close()
-            
-            if os.path.exists(tmpfile):
-                os.unlink(tmpfile)
-                
-            if backup :
-                #We will now backup the file
-                backup_nbr = BACKUP_NBR
-                #We keep BACKUP_NBR versions of the file
-                #The 0 is the youngest one
-                while backup_nbr > 0 :
-                    older = "%s.bak.%s" %(zefile,backup_nbr)
-                    backup_nbr -= 1
-                    newer = "%s.bak.%s" %(zefile,backup_nbr)
-                    if os.path.exists(newer) :
-                        shutil.move(newer,older)
-                #The bak.0 is always a fresh copy of the closed file
-                #So that it's not touched in case of bad opening next time
-                current = "%s.bak.0" %(zefile)
-                shutil.copy(zefile,current)
-            
-            return True
-        else:
-            print "no file %s or no pretty xml"%zefile
+
+        if bwritten != len(pretty):
+            print "error writing file %s" % zefile
             return False
+
+        os.rename(tmpfile, zefile)
+
+        if backup :
+            #We will now backup the file
+            backup_nbr = BACKUP_NBR
+            #We keep BACKUP_NBR versions of the file
+            #The 0 is the youngest one
+            while backup_nbr > 0 :
+                older = "%s.bak.%s" %(zefile,backup_nbr)
+                backup_nbr -= 1
+                newer = "%s.bak.%s" %(zefile,backup_nbr)
+                if os.path.exists(newer) :
+                    os.rename(newer,older)
+            #The bak.0 is always a fresh copy of the closed file
+            #So that it's not touched in case of bad opening next time
+            current = "%s.bak.0" % (zefile)
+            shutil.copy(tmpfile,current)
+        return True
     except IOError, msg:
         print msg
         return False