Gentoo Archives: gentoo-commits

From: "Robin H. Johnson" <robbat2@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/gentoo-mirrorstats:master commit in: html/
Date: Thu, 07 May 2020 18:18:44
Message-Id: 1588875490.b29132aafc10f1a3758b5145a84e320502e6f7d4.robbat2@gentoo
1 commit: b29132aafc10f1a3758b5145a84e320502e6f7d4
2 Author: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
3 AuthorDate: Thu May 7 17:53:39 2020 +0000
4 Commit: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
5 CommitDate: Thu May 7 18:18:10 2020 +0000
6 URL: https://gitweb.gentoo.org/proj/gentoo-mirrorstats.git/commit/?id=b29132aa
7
8 html/generate.py: improve cache file handling
9
10 Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org>
11
12 html/generate.py | 16 ++++++++++++----
13 1 file changed, 12 insertions(+), 4 deletions(-)
14
15 diff --git a/html/generate.py b/html/generate.py
16 index 8eb7622..8d79cbe 100755
17 --- a/html/generate.py
18 +++ b/html/generate.py
19 @@ -15,6 +15,8 @@
20
21 import datetime
22 import socket
23 +import os
24 +import tempfile
25 import urllib.request, json
26 import xml.etree.ElementTree as ET
27 import jinja2
28 @@ -81,8 +83,12 @@ def renderStatsTemplate(templateEnv, page):
29
30
31 # read the cache
32 -with open(cache_path) as json_file:
33 - cache_data = json.load(json_file)
34 +if os.path.exists(cache_path):
35 + with open(cache_path, mode='rt') as json_file:
36 + try:
37 + cache_data = json.load(json_file)
38 + except:
39 + pass
40
41 #
42 # The all mirrors that are present in the given list
43 @@ -169,8 +175,10 @@ template.stream(lastUpdate=lastUpdate).dump(html_folder + "help.html")
44 #
45 # write the cache
46 #
47 -with open(cache_path, 'w') as fp:
48 - json.dump(cache_data, fp)
49 +with tempfile.NamedTemporaryFile(dir=os.path.dirname(cache_path), delete=False, mode='wt') as fout:
50 + json.dump(cache_data, fout)
51 + os.chmod(fout.name, '0644')
52 + os.replace(fout.name, cache_path)
53
54
55 #