diff options
author | Robin H. Johnson <robbat2@gentoo.org> | 2020-05-07 10:53:39 -0700 |
---|---|---|
committer | Robin H. Johnson <robbat2@gentoo.org> | 2020-05-07 11:18:10 -0700 |
commit | b29132aafc10f1a3758b5145a84e320502e6f7d4 (patch) | |
tree | d23257d038873df1a9a4812309d75ab20ce46aaf | |
parent | html/generate.py: disable success print spam (diff) | |
download | gentoo-mirrorstats-b29132aafc10f1a3758b5145a84e320502e6f7d4.tar.gz gentoo-mirrorstats-b29132aafc10f1a3758b5145a84e320502e6f7d4.tar.bz2 gentoo-mirrorstats-b29132aafc10f1a3758b5145a84e320502e6f7d4.zip |
html/generate.py: improve cache file handling
Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
-rwxr-xr-x | html/generate.py | 16 |
1 files 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) # |