commit: b29132aafc10f1a3758b5145a84e320502e6f7d4 Author: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org> AuthorDate: Thu May 7 17:53:39 2020 +0000 Commit: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org> CommitDate: Thu May 7 18:18:10 2020 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-mirrorstats.git/commit/?id=b29132aa
html/generate.py: improve cache file handling Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org> html/generate.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/html/generate.py b/html/generate.py index 8eb7622..8d79cbe 100755 --- a/html/generate.py +++ b/html/generate.py @@ -15,6 +15,8 @@ import datetime import socket +import os +import tempfile import urllib.request, json import xml.etree.ElementTree as ET import jinja2 @@ -81,8 +83,12 @@ def renderStatsTemplate(templateEnv, page): # read the cache -with open(cache_path) as json_file: - cache_data = json.load(json_file) +if os.path.exists(cache_path): + with open(cache_path, mode='rt') as json_file: + try: + cache_data = json.load(json_file) + except: + pass # # The all mirrors that are present in the given list @@ -169,8 +175,10 @@ template.stream(lastUpdate=lastUpdate).dump(html_folder + "help.html") # # write the cache # -with open(cache_path, 'w') as fp: - json.dump(cache_data, fp) +with tempfile.NamedTemporaryFile(dir=os.path.dirname(cache_path), delete=False, mode='wt') as fout: + json.dump(cache_data, fout) + os.chmod(fout.name, '0644') + os.replace(fout.name, cache_path) #
