-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.py
More file actions
107 lines (84 loc) · 2.81 KB
/
build.py
File metadata and controls
107 lines (84 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import shutil
import subprocess
from contextlib import redirect_stdout
from io import StringIO
from pathlib import Path
from typing import Iterable
import replacement
NOTEBOOK_ORDER: list[str] = [
"Introduction.ipynb",
"SingleLens.ipynb",
"BinarySource.ipynb",
"PlanetsAndBrownDwarfs.ipynb",
"RemnantsAndDarkMatter.ipynb",
"Eras.ipynb",
"Modelling.ipynb",
"MulensModelFSPLError.ipynb",
]
NOTEBOOK_ROOT = Path("Notebooks")
REFERENCE_DIR = NOTEBOOK_ROOT / "Exercises"
BUILD_ROOT = Path("build")
SOLVED_BUILD_DIR = BUILD_ROOT / "Complete"
DOCS_ROOT = Path("docs")
DOCS_UNSOLVED_DIR = DOCS_ROOT / "Notebooks"
DOCS_SOLVED_DIR = DOCS_ROOT / "Solved"
def clean_directory(path: Path) -> None:
if path.exists():
shutil.rmtree(path)
path.mkdir(parents=True, exist_ok=True)
def copy_notebook_tree(target: Path) -> None:
if target.exists():
shutil.rmtree(target)
shutil.copytree(NOTEBOOK_ROOT, target)
def iter_course_notebooks() -> Iterable[Path]:
for name in NOTEBOOK_ORDER:
notebook_path = NOTEBOOK_ROOT / name
if notebook_path.exists():
yield notebook_path
else:
print(f"Warning: expected notebook {notebook_path} not found; skipping.")
def generate_solved_notebooks() -> None:
clean_directory(SOLVED_BUILD_DIR)
# Ensure docs/Solved mirrors the notebook tree (assets, data, etc.)
copy_notebook_tree(DOCS_SOLVED_DIR)
for src in iter_course_notebooks():
dest = SOLVED_BUILD_DIR / src.name
dest.parent.mkdir(parents=True, exist_ok=True)
try:
buffer = StringIO()
with redirect_stdout(buffer):
replacement.replace_solutions(
str(src),
str(dest),
str(REFERENCE_DIR),
destructive=True,
skip_missing=True,
)
print(f"Solved notebook created for {src.name}.")
except Exception as exc:
print(
f"Warning: could not generate solved notebook for {src.name}: {exc}"
)
print("Copying the unsolved version instead.")
shutil.copy2(src, dest)
finally:
temp_txt = Path("old_notebook.txt")
if temp_txt.exists():
temp_txt.unlink()
shutil.copy2(dest, DOCS_SOLVED_DIR / src.name)
def build_sphinx_docs() -> None:
html_dir = DOCS_ROOT / "_build" / "html"
if html_dir.exists():
shutil.rmtree(html_dir)
subprocess.run(
["sphinx-build", "-b", "html", str(DOCS_ROOT), str(html_dir)],
check=True,
)
def main() -> None:
clean_directory(BUILD_ROOT)
copy_notebook_tree(DOCS_UNSOLVED_DIR)
generate_solved_notebooks()
build_sphinx_docs()
if __name__ == "__main__":
main()