← Back to team overview

gtg team mailing list archive

[Merge] lp:~mkevac/gtg/main into lp:gtg

 

Marko Kevac has proposed merging lp:~mkevac/gtg/main into lp:gtg.

Requested reviews:
  Gtg developers (gtg)
Related bugs:
  #579189 tags.xml is sometimes deleted
  https://bugs.launchpad.net/bugs/579189


Fixing bug 579189
-- 
https://code.launchpad.net/~mkevac/gtg/main/+merge/25609
Your team Gtg developers is requested to review the proposed merge of lp:~mkevac/gtg/main into lp:gtg.
=== modified file 'AUTHORS'
--- AUTHORS	2010-04-30 19:48:11 +0000
+++ AUTHORS	2010-05-19 14:11:31 +0000
@@ -66,3 +66,4 @@
 ----------
 * Izidor Matušov <izidor.matusov@xxxxxxxxx>
 * Kees Cook <kees@xxxxxxxxxx>
+* Marko Kevac <marko@xxxxxxxxx>

=== modified file 'CHANGELOG'
--- CHANGELOG	2010-04-07 15:02:38 +0000
+++ CHANGELOG	2010-05-19 14:11:31 +0000
@@ -1,5 +1,6 @@
 ????-??-?? Getting Things GNOME! ?.?.?
     * Added link to web documentation in Help menu, by Ronan Jouchet
+    * Fixed bug with data consistency #579189, by Marko Kevac
 
 2010-03-01 Getting Things GNOME! 0.2.2
     * Autostart on login, by Luca Invernizzi

=== modified file 'GTG/info.py'
--- GTG/info.py	2010-03-13 20:06:55 +0000
+++ GTG/info.py	2010-05-19 14:11:31 +0000
@@ -32,6 +32,7 @@
                "\tBryce Harrington <bryce@xxxxxxxxxxxxxxxxxxx>", \
                "\tChris Johnston <chrisjohnston@xxxxxxxxxx>", \
                "\tDavid Planella <david.planella@xxxxxxxxxx>", \
+               "\tMarko Kevac <marko@xxxxxxxxx>", \
 ]
 ARTISTS     = ["Kalle Persson <kalle@xxxxxxxxxxxxxxx>", \
                 "Bertrand Rousseau <bertrand.rousseau@xxxxxxxxx>"]

=== modified file 'GTG/tools/cleanxml.py'
--- GTG/tools/cleanxml.py	2010-02-28 14:25:23 +0000
+++ GTG/tools/cleanxml.py	2010-05-19 14:11:31 +0000
@@ -63,27 +63,40 @@
 #This function open an XML file if it exists and return the XML object
 #If the file doesn't exist, it is created with an empty XML tree    
 def openxmlfile(zefile,root ):
-    try :
-        if os.path.exists(zefile) :
-            #We should be more defensive here
-            doc = xml.dom.minidom.parse(zefile)
-            cleanDoc(doc,tab,enter)
-            #We should be more defensive here
-            xmlproject = doc.getElementsByTagName(root)[0]
-        #the file didn't exist, create it now
-        else :
+    tmpfile = zefile+'__'
+    
+    try:
+        if os.path.exists(zefile):
+            f = open(zefile, "r")
+        elif os.path.exists(tmpfile):
+            print "Something happened last time we tried to write file. Temp file found, using it as normal."
+            os.rename(tmpfile, zefile)
+            f = open(zefile, "r")
+        else:
+            # Creating empty file
             doc,xmlproject = emptydoc(root)
-            #then we create the file
-            f = open(zefile, mode='a+')
-            f.write(doc.toxml().encode("utf-8"))
-            f.close()
+            newfile = savexml(zefile, doc) # use our function to save file
+            if not newfile:
+                sys.exit(1)
+            return openxmlfile(zefile, root) # recursive call
+
+        doc = xml.dom.minidom.parse(f)
+        cleanDoc(doc,tab,enter)
+        xmlproject = doc.getElementsByTagName(root)[0]
+        f.close()
         return doc,xmlproject
     except IOError, msg:
         print msg
         sys.exit(1)
-
+        
     except xml.parsers.expat.ExpatError, msg:
+        f.close()
         print "Error parsing XML file %s: %s" %(zefile, msg)
+        if os.path.exists(tmpfile):
+            print "Something happened last time we tried to write file. Temp file found, using it as normal."
+            os.rename(tmpfile, zefile)
+            # Ok, try one more time now
+            return openxmlfile(zefile, root)
         sys.exit(1)
 
 
@@ -95,26 +108,44 @@
     return doc, rootproject
     
 #write a XML doc to a file
-def savexml(zefile,doc,backup=False) :
-    f = open(zefile, mode='w+')
-    pretty = doc.toprettyxml(tab,enter)
-    if f and pretty:
-        f.write(pretty.encode("utf-8"))
-        f.close()
-        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)
-    else:
-        print "no file %s or no pretty xml"%zefile
+def savexml(zefile,doc,backup=False):
+    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:
+            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
+            return False
+    except IOError, msg:
+        print msg
+        return False