/ challenges-old / string-solutions
Sign in
challenges-old / string-solutions
string-solutions / web / build.py
49 lines · 2.0 KBBlameHistoryRaw
1"""Assemble this repository's static site: a manifest of the submissions, the solution files, and the viewer page."""
2from __future__ import annotations
4import argparse
5import json
6import shutil
7from pathlib import Path
9ROOT = Path(__file__).resolve().parents[1]
12def main():
13 ap = argparse.ArgumentParser()
14 ap.add_argument("--out", type=Path, default=ROOT / "site")
15 args = ap.parse_args()
16 out: Path = args.out
17 if out.exists():
18 shutil.rmtree(out)
19 out.mkdir(parents=True)
20 manifest = {"challenges": {}}
21 for p in sorted((ROOT / "solutions").glob("*/*.json")):
22 sub = json.loads(p.read_text())
23 cid = p.parent.name
24 entry = manifest["challenges"].setdefault(cid, {"submissions": []})
25 rel_dir = p.parent.relative_to(ROOT)
26 for sol in sub["solutions"]:
27 sol["output"] = f"{rel_dir.as_posix()}/{sol['output']}"
28 if "fields" in sol and not sol["fields"]["path"].startswith("http"):
29 sol["fields"]["path"] = f"{rel_dir.as_posix()}/{sol['fields']['path']}"
30 entry["submissions"].append(sub)
31 shutil.copytree(ROOT / "solutions", out / "solutions")
32 shutil.copyfile(ROOT / "web" / "index.html", out / "index.html")
33 shutil.copyfile(ROOT / "web" / "viewer.js", out / "viewer.js")
34 # the shared Web Audio player, from the installed challenge-tools package
35 from challenge_tools.build import player_js
37 (out / "player.js").write_text(player_js())
38 # suggested playback gains, when the challenge repository is checked out beside us (as in the workflow)
39 gains = {}
40 for cj in sorted((ROOT / "challenge-repo" / "challenges").glob("*/challenge.json")) if (ROOT / "challenge-repo").exists() else []:
41 ch = json.loads(cj.read_text())
42 gains[ch["challenge_id"]] = (ch.get("playback") or {}).get("gain_db")
43 manifest["playback_gain_db"] = gains
44 (out / "manifest.json").write_text(json.dumps(manifest, indent=1))
45 print(f"site written to {out}")
48if __name__ == "__main__":
49 main()
↑↓ move↵openescclose