From 98a5b4383191bc3403944a44bf58a579b885da23 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Sun, 27 Dec 2020 18:06:43 +0100 Subject: [PATCH] Update descriptions and list only if changed Use the new replace_file_from_string() function to replace these files conditionally. Even if they're small... --- gitdump.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/gitdump.py b/gitdump.py index 188abdc..26e1f1b 100644 --- a/gitdump.py +++ b/gitdump.py @@ -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 -- 2.39.5