]> git.cworth.org Git - turbot-web/commitdiff
Write all files to temporary files, renaming afterwards
authorCarl Worth <cworth@cworth.org>
Wed, 12 Jan 2022 09:43:19 +0000 (01:43 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 12 Jan 2022 09:43:19 +0000 (01:43 -0800)
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

index 7d2c318fbd09293fccec6e371c1f1b76ade0efd9..e337f08034dce8d0f0a19909b2c78351f96d1bdd 100644 (file)
@@ -212,10 +212,11 @@ def overview(hunt, puzzles, rounds):
     end = ['</body>\n', '</html>\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):
     '</html>\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):
      '</body>\n',
      '</html>\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):
     '</html>\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):