From 976a9bcf02620a4b1aec2a180efbc0d741a212ce Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 12 Jan 2022 01:43:19 -0800 Subject: [PATCH] Write all files to temporary files, renaming afterwards This ensures that the webserver will only ever serve a complete file, and never one that the generator is in the process of creating. --- html_generator.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/html_generator.py b/html_generator.py index 7d2c318..e337f08 100644 --- a/html_generator.py +++ b/html_generator.py @@ -212,10 +212,11 @@ def overview(hunt, puzzles, rounds): end = ['\n', '\n'] html = start + expanding + columns + end file = hunt_file(hunt, "index.html") - f = open(file, "w") + f = open(file + ".tmp", "w") for line in html: f.write(line) f.close() + os.rename(file + ".tmp", file) return None def round_overview(hunt, rnd, puzzles): @@ -286,10 +287,11 @@ def round_overview(hunt, rnd, puzzles): '\n'] html = start + puzzle_list + end file = hunt_file(hunt, "{}_round.html".format(filename_from_name(rnd))) - f = open(file, "w") + f = open(file + ".tmp", "w") for line in html: f.write(line) f.close() + os.rename(file + ".tmp", file) return None def puzzle_overview(hunt, puzzle): @@ -347,9 +349,11 @@ def puzzle_overview(hunt, puzzle): '\n', '\n'] file = hunt_file(hunt, "{}.html".format(filename_from_name(name))) - f = open(file, "w") + f = open(file + ".tmp", "w") for line in html: f.write(line) + f.close() + os.rename(file + ".tmp", file) return None def puzzle_lists(hunt, puzzles, filt): @@ -438,22 +442,25 @@ def puzzle_lists(hunt, puzzles, filt): '\n'] if filt == "All": file1 = hunt_file(hunt, 'all.html') - f = open(file1, "w") + f = open(file1 + ".tmp", "w") for line in start + unsolved_code + solved_code + end: f.write(line) f.close() + os.rename(file1 + ".tmp", file1) elif filt == "Solved": file2 = hunt_file(hunt, 'solved.html') - f = open(file2, 'w') + f = open(file2 + ".tmp", 'w') for line in start + solved_code + end: f.write(line) f.close() + os.rename(file2 + ".tmp", file2) elif filt == "Unsolved": file3 = hunt_file(hunt, 'unsolved.html') - f = open(file3, 'w') + f = open(file3 + ".tmp", 'w') for line in start + unsolved_code + end: f.write(line) f.close() + os.rename(file3 + ".tmp", file3) return None def generate_for_hunt_id(table, hunt_id): -- 2.43.0