Skip to content

Commit 1ca2aac

Browse files
committed
Generate examples database during install
1 parent 27ff1e7 commit 1ca2aac

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

ci/install_examples_db.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python3
2+
"""Generate the installed examples SQLite database for the GUI setup tab."""
3+
4+
from __future__ import annotations
5+
6+
import os
7+
import subprocess
8+
import sys
9+
from pathlib import Path
10+
11+
12+
EXAMPLES = {
13+
"basic": {
14+
"simple_flux": {"runs": ["1"], "variations": ["default"]},
15+
"b1": {"runs": ["1"], "variations": ["default", "test"]},
16+
"b2": {"runs": ["1", "11"], "variations": ["default", "alt"]},
17+
"b3": {"runs": ["1", "11"], "variations": ["default", "alt"]},
18+
},
19+
"optical": {
20+
"cherenkov": {"runs": ["1"], "variations": ["default", "CO2", "C4F10"]},
21+
},
22+
}
23+
24+
25+
def run_geometry_script(python: Path, script: Path, db: Path, args: list[str], env: dict[str, str]) -> None:
26+
subprocess.run(
27+
[str(python), str(script), "-f", "sqlite", "-sql", str(db), *args],
28+
cwd=script.parent,
29+
env=env,
30+
check=True,
31+
)
32+
33+
34+
def main() -> int:
35+
if os.environ.get("GEMC_SKIP_EXAMPLES_DB_INSTALL") == "1":
36+
print(" > Skipping examples database generation (GEMC_SKIP_EXAMPLES_DB_INSTALL=1)")
37+
return 0
38+
39+
prefix = Path(os.environ["MESON_INSTALL_PREFIX"])
40+
examples_dir = prefix / "examples"
41+
db = examples_dir / "gemc.db"
42+
python = prefix / "python_env" / "bin" / "python3"
43+
44+
if not python.exists():
45+
print(f" > Cannot generate examples database: {python} was not found", file=sys.stderr)
46+
return 1
47+
48+
db.unlink(missing_ok=True)
49+
50+
env = os.environ.copy()
51+
env["PYTHONDONTWRITEBYTECODE"] = "1"
52+
53+
print(f" > Generating examples database at {db}")
54+
for branch, examples in EXAMPLES.items():
55+
for example, config in examples.items():
56+
script = examples_dir / branch / example / f"{example}.py"
57+
if not script.exists():
58+
print(f" > Missing example geometry script: {script}", file=sys.stderr)
59+
return 1
60+
61+
for variation in config["variations"]:
62+
run_geometry_script(python, script, db, ["-v", variation], env)
63+
64+
for run in config["runs"]:
65+
run_geometry_script(python, script, db, ["-r", run], env)
66+
67+
return 0
68+
69+
70+
if __name__ == "__main__":
71+
raise SystemExit(main())

0 commit comments

Comments
 (0)