]> git.cworth.org Git - turbot-web/blobdiff - html_generator.py
Write all files to temporary files, renaming afterwards
[turbot-web] / html_generator.py
index 43d33702f81105f996ad6161174b0a1e47b216f0..e337f08034dce8d0f0a19909b2c78351f96d1bdd 100644 (file)
@@ -18,6 +18,7 @@ import boto3
 from boto3.dynamodb.conditions import Key
 import os
 import re
+import sys
 
 WEBROOT = "/srv/halibut.cworth.org/www"
 
@@ -211,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):
@@ -285,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):
@@ -346,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):
@@ -395,7 +400,7 @@ def puzzle_lists(hunt, puzzles, filt):
             meta = ''
         slack_url = channel_url(puzzle['channel_id'])
         if 'rounds' in puzzle:
-            round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
+            round_url = link(website + filename_from_name(puzzle['rounds'][0]) + "_round.html", puzzle['rounds'][0])
         else:
             round_url = ''
         #assuming one round per puzzle for now
@@ -416,7 +421,7 @@ def puzzle_lists(hunt, puzzles, filt):
             meta = ''
         slack_url = channel_url(puzzle['channel_id'])
         if 'rounds' in puzzle:
-            round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
+            round_url = link(website + filename_from_name(puzzle['rounds'][0]) + "_round.html", puzzle['rounds'][0])
         else:
             round_url = ''
         #assuming one round per puzzle for now
@@ -437,42 +442,60 @@ 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
 
-# Initialize AWS resources to talk to database
+def generate_for_hunt_id(table, hunt_id):
+    hunt, puzzles, rounds = hunt_info(table, hunt_id)
+
+    # Create a directory for the hunt in the WEBROOT
+    root = hunt_file(hunt, "")
+    try:
+        os.mkdir(root)
+    except FileExistsError:
+        #  We're happy as a clam if the directory already exists
+        pass
+
+    overview(hunt, puzzles, rounds)
+    for rnd in rounds:
+        round_overview(hunt, rnd, puzzles)
+        for puzzle in puzzles:
+            puzzle_overview(hunt, puzzle)
+            puzzle_lists(hunt, puzzles, "All")
+            puzzle_lists(hunt, puzzles, "Solved")
+            puzzle_lists(hunt, puzzles, "Unsolved")
+
+# Initialize AWS resources to talk to the database
 db = boto3.resource('dynamodb')
 table = db.Table("turbot")
-hunt, puzzles, rounds = hunt_info(table, "mh2021")
-
-# Create a directory for the hunt in the WEBROOT
-root = hunt_file(hunt, "")
-try:
-    os.mkdir(root)
-except FileExistsError:
-    #  We're happy as a clam if the directory already exists
-    pass
-
-overview(hunt, puzzles, rounds)
-for rnd in rounds:
-    round_overview(hunt, rnd, puzzles)
-for puzzle in puzzles:
-    puzzle_overview(hunt, puzzle)
-puzzle_lists(hunt, puzzles, "All")
-puzzle_lists(hunt, puzzles, "Solved")
-puzzle_lists(hunt, puzzles, "Unsolved")
+
+def usage():
+    print("Usage: {} hunt_id [...]")
+    print("")
+    print("Generates pages (under {}) ".format(WEBROOT))
+    print("for the specified hunt_id(s).")
+
+if len(sys.argv) < 2:
+    usage()
+    sys.exit(1)
+
+for hunt_id in sys.argv[1:]:
+    generate_for_hunt_id(table, hunt_id)