[Python-apps-commits] r5970 - in packages/gtg/trunk/debian (3 files)

dktrkranz at users.alioth.debian.org dktrkranz at users.alioth.debian.org
Sat Aug 7 11:11:58 UTC 2010


    Date: Saturday, August 7, 2010 @ 11:11:56
  Author: dktrkranz
Revision: 5970

Use temp files to temporarily store GTG status, to avoid
generating empty XML files (Closes: #590043)

Added:
  packages/gtg/trunk/debian/patches/xml_temp_file.patch
Modified:
  packages/gtg/trunk/debian/changelog
  packages/gtg/trunk/debian/patches/series

Modified: packages/gtg/trunk/debian/changelog
===================================================================
--- packages/gtg/trunk/debian/changelog	2010-08-07 11:03:48 UTC (rev 5969)
+++ packages/gtg/trunk/debian/changelog	2010-08-07 11:11:56 UTC (rev 5970)
@@ -1,9 +1,12 @@
-gtg (0.2.4-3) unstable; urgency=low
+gtg (0.2.4-3) UNRELEASED; urgency=low
 
+  * debian/patches/xml_temp_file.patch:
+    - Use temp files to temporarily store GTG status, to avoid
+      generating empty XML files (Closes: #590043).
   * debian/control:
     - Bump Standards-Version to 3.9.1, no changes required.
 
- -- Luca Falavigna <dktrkranz at debian.org>  Sat, 07 Aug 2010 13:03:03 +0200
+ -- Luca Falavigna <dktrkranz at debian.org>  Sat, 07 Aug 2010 13:10:14 +0200
 
 gtg (0.2.4-2) unstable; urgency=low
 

Modified: packages/gtg/trunk/debian/patches/series
===================================================================
--- packages/gtg/trunk/debian/patches/series	2010-08-07 11:03:48 UTC (rev 5969)
+++ packages/gtg/trunk/debian/patches/series	2010-08-07 11:11:56 UTC (rev 5970)
@@ -1,2 +1,3 @@
 test_executable.patch
 reaper_hang.patch
+xml_temp_file.patch

Added: packages/gtg/trunk/debian/patches/xml_temp_file.patch
===================================================================
--- packages/gtg/trunk/debian/patches/xml_temp_file.patch	                        (rev 0)
+++ packages/gtg/trunk/debian/patches/xml_temp_file.patch	2010-08-07 11:11:56 UTC (rev 5970)
@@ -0,0 +1,129 @@
+Use temp files to temporarily store GTG status, to avoid generating empty XML files.
+
+Index: gtg-0.2.4/GTG/tools/cleanxml.py
+===================================================================
+--- gtg-0.2.4.orig/GTG/tools/cleanxml.py	2010-08-07 13:05:44.582562884 +0200
++++ gtg-0.2.4/GTG/tools/cleanxml.py	2010-08-07 13:05:41.226574167 +0200
+@@ -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




More information about the Python-apps-commits mailing list