From b29132aafc10f1a3758b5145a84e320502e6f7d4 Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Thu, 7 May 2020 10:53:39 -0700 Subject: html/generate.py: improve cache file handling Signed-off-by: Robin H. Johnson --- 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) # -- cgit v1.2.3-65-gdbad