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 from challenge_tools.build import player_version
34 html = (ROOT / "web" / "index.html").read_text().replace('src="player.js"', f'src="player.js?v={player_version()}"')
35 (out / "index.html").write_text(html)
36 shutil.copyfile(ROOT / "web" / "viewer.js", out / "viewer.js")
37 # the shared Web Audio player, from the installed challenge-tools package
38 from challenge_tools.build import player_js
40 (out / "player.js").write_text(player_js())
41 # suggested playback gains, when the challenge repository is checked out beside us (as in the workflow)
42 gains = {}
43 for cj in sorted((ROOT / "challenge-repo" / "challenges").glob("*/challenge.json")) if (ROOT / "challenge-repo").exists() else []:
44 ch = json.loads(cj.read_text())
45 gains[ch["challenge_id"]] = (ch.get("playback") or {}).get("gain_db")
46 manifest["playback_gain_db"] = gains
47 (out / "manifest.json").write_text(json.dumps(manifest, indent=1))
48 print(f"site written to {out}")
51if __name__ == "__main__":
52 main()