Repository navigation
Expand file tree
/
Copy pathupdate.py
More file actions
executable file
·121 lines (95 loc) · 4.57 KB
/
Copy pathupdate.py
File metadata and controls
executable file
·121 lines (95 loc) · 4.57 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
"""Rebuild everything downstream of soln/ and publish it.
soln/*.ipynb is the canonical source. Five targets are generated from it,
each by its own build.sh:
chapters/ student notebooks + ThinkPythonNotebooks.zip commits & pushes
blank/ code-free notebooks commits & pushes
quizzes/ LMS quiz files (Canvas, Moodle, Brightspace) commits & pushes
jb/ Jupyter Book HTML PUBLISHES to Pages
top level support files, SVG -> PNG commits & pushes
Step 5 makes the website live. You are asked to confirm before it runs;
pass --yes to skip the prompt.
python update.py # test, commit, push, build, confirm, publish
python update.py --yes # no confirmation
python update.py --skip-tests # skip `make tests` (~2 minutes)
"""
import argparse
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).parent.resolve() # ThinkPythonSolutions
BOOK = ROOT.parent # ThinkPython
def banner(text):
print(f"\n{'=' * 70}\n{text}\n{'=' * 70}", flush=True)
def run(cmd, cwd, check=True):
"""Run a command, streaming its output. Stop the script if it fails.
The old version captured output and merely warned on a nonzero exit, so a
failed build still ended with a success message.
"""
print(f"\n>>> {cmd}\n in {cwd}", flush=True)
result = subprocess.run(cmd, shell=True, cwd=cwd)
if check and result.returncode:
sys.exit(
f"\nFAILED (exit {result.returncode}): {cmd}\n"
f" in {cwd}\n"
f"Stopping. Later steps have not run."
)
return result.returncode
def nothing_to_commit(cwd):
return subprocess.run(
"git diff --cached --quiet && git diff --quiet",
shell=True, cwd=cwd
).returncode == 0
def main():
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--yes", action="store_true",
help="do not ask before publishing the website")
parser.add_argument("--skip-tests", action="store_true",
help="skip `make tests`")
args = parser.parse_args()
banner("STEP 1: check and test the solution notebooks")
if args.skip_tests:
print("skipping `make tests` at your request; running the outputs check only")
run("python check_outputs.py", ROOT)
else:
run("make tests", ROOT)
banner("STEP 1b: parse the quizzes")
# soln/quizzes.ipynb -> quizzes/quizzes.md (no jupytext markers) -> parsed JSON,
# committed with soln/ below and read by quizzes/build.sh.
run("jupytext --to md -o - soln/quizzes.ipynb"
" | grep -v -E '^<!-- #(end)?region -->$'"
" | sed '/^ split_at_heading: true$/d'"
" > soln/quizzes/quizzes.md", ROOT)
run("python parse_quizzes.py > /dev/null", ROOT / "soln" / "quizzes")
banner("STEP 2: commit and push soln/")
# The old version committed here and never pushed, so the solutions repo
# could sit unpushed indefinitely while ThinkPython went out.
if nothing_to_commit(ROOT):
print("nothing to commit in ThinkPythonSolutions")
else:
# -u stages tracked files only, so gitignored copies such as
# soln/diagram.py cannot abort the run.
run("git add -u soln/", ROOT)
run('git commit -m "Update solution notebooks"', ROOT)
run("git push", ROOT)
banner("STEP 3: build chapters/ (student notebooks and the zip)")
run("bash build.sh", BOOK / "chapters")
banner("STEP 4: build blank/")
# The old version skipped this target entirely, which is why blank/ drifted.
run("bash build.sh", BOOK / "blank")
banner("STEP 4b: build quizzes/ (LMS quiz files)")
run("bash build.sh", BOOK / "quizzes")
banner("STEP 5: build and PUBLISH the website")
print("jb/build.sh ends in `ghp-import -n -p -f`, which pushes straight to")
print("GitHub Pages. The site goes live as soon as this step finishes.")
if not args.yes:
if input("\nPublish the website? [y/N] ").strip().lower() not in ("y", "yes"):
sys.exit("Stopped before publishing. Steps 1-4 are done and pushed.")
run("rm -rf _build", BOOK / "jb") # stale pages have been published before
run("bash build.sh", BOOK / "jb")
banner("STEP 6: top-level support files")
run("bash build.sh", BOOK)
banner("Done. All five targets built; the website is live.")
if __name__ == "__main__":
main()