From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id B496313835A for ; Thu, 7 May 2020 18:18:44 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id ED03FE0874; Thu, 7 May 2020 18:18:43 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id D4980E0874 for ; Thu, 7 May 2020 18:18:43 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id D06F634F36D for ; Thu, 7 May 2020 18:18:42 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 6AC2E204 for ; Thu, 7 May 2020 18:18:40 +0000 (UTC) From: "Robin H. Johnson" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Robin H. Johnson" Message-ID: <1588875490.b29132aafc10f1a3758b5145a84e320502e6f7d4.robbat2@gentoo> Subject: [gentoo-commits] proj/gentoo-mirrorstats:master commit in: html/ X-VCS-Repository: proj/gentoo-mirrorstats X-VCS-Files: html/generate.py X-VCS-Directories: html/ X-VCS-Committer: robbat2 X-VCS-Committer-Name: Robin H. Johnson X-VCS-Revision: b29132aafc10f1a3758b5145a84e320502e6f7d4 X-VCS-Branch: master Date: Thu, 7 May 2020 18:18:40 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 299dcfbd-9ba4-4cbe-b690-16702ea35382 X-Archives-Hash: aed543119077bcbb044f94302150ae33 commit: b29132aafc10f1a3758b5145a84e320502e6f7d4 Author: Robin H. Johnson gentoo org> AuthorDate: Thu May 7 17:53:39 2020 +0000 Commit: Robin H. Johnson gentoo 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 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) #