Update descriptions and list only if changed
authorMagnus Hagander <magnus@hagander.net>
Sun, 27 Dec 2020 17:06:43 +0000 (18:06 +0100)
committerMagnus Hagander <magnus@hagander.net>
Sun, 27 Dec 2020 17:06:43 +0000 (18:06 +0100)
Use the new replace_file_from_string() function to replace these files
conditionally. Even if they're small...

gitdump.py

index 188abdc76971bf3de464bcd057f5250b9a4710c4..26e1f1b9447ebcc79bf1ee13fb38fa85da2faed8 100644 (file)
@@ -12,6 +12,7 @@ This means:
 
 import os
 import shutil
+import io
 import psycopg2
 import configparser
 import urllib.parse
@@ -28,6 +29,7 @@ def replace_file_from_string(fn, s):
 
     with open("{}.tmp".format(fn), "w") as f:
         f.write(s)
+    os.chmod("{}.tmp".format(fn), 0o644)
     os.rename("{}.tmp".format(fn), fn)
 
 
@@ -68,7 +70,8 @@ SELECT name,anonymous,web,description,initialclone,tabwidth,
             (SELECT * FROM remoterepositories WHERE remoterepositories.id=r.remoterepository_id)
          THEN 1 ELSE 0 END
 FROM repositories AS r WHERE approved ORDER BY name""")
-        f = open("%s.tmp" % self.conf.get("paths", "gitweblist"), "w")
+
+        s = io.StringIO()
 
         webrepos = []
 
@@ -116,10 +119,11 @@ FROM repositories AS r WHERE approved ORDER BY name""")
 
             # Check for publishing options here
             if web:
-                f.write("%s.git %s\n" % (urllib.parse.quote_plus(name), urllib.parse.quote_plus(owner)))
-                df = open("%s/description" % repopath, "w")
-                df.write(description)
-                df.close()
+                s.write("%s.git %s\n" % (urllib.parse.quote_plus(name), urllib.parse.quote_plus(owner)))
+                replace_file_from_string(
+                    "%s/description" % repopath,
+                    description,
+                )
                 # Check if we need to change the tab width (default is 8)
                 repoconf = configparser.ConfigParser()
                 repoconf.read("%s/config" % repopath)
@@ -156,9 +160,10 @@ FROM repositories AS r WHERE approved ORDER BY name""")
                 if os.path.isfile(anonfile):
                     os.remove(anonfile)
 
-        f.close()
-        os.chmod("%s.tmp" % self.conf.get("paths", "gitweblist"), 0o644)
-        os.rename("%s.tmp" % self.conf.get("paths", "gitweblist"), self.conf.get("paths", "gitweblist"))
+        replace_file_from_string(
+            self.conf.get("paths", "gitweblist"),
+            s.getvalue(),
+        )
 
         if webrepos:
             changed = False