forked from ApeWorX/ape
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_docs.py
90 lines (65 loc) · 2.46 KB
/
build_docs.py
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
import os
import shutil
import subprocess
from pathlib import Path
REDIRECT_HTML = """
<!DOCTYPE html>
<meta charset="utf-8">
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; URL=./{}/">
"""
DOCS_BUILD_PATH = Path("docs/_build/ape")
LATEST_PATH = DOCS_BUILD_PATH / "latest"
STABLE_PATH = DOCS_BUILD_PATH / "stable"
class ApeDocsBuildError(Exception):
pass
def git(*args):
return subprocess.check_output(["git", *args]).decode("ascii").strip()
def new_dir(path: Path) -> Path:
if path.exists():
shutil.rmtree(path)
path.mkdir(parents=True)
return path
def build_docs(path: Path) -> Path:
path = new_dir(path)
try:
subprocess.check_call(["sphinx-build", "docs", str(path)])
except subprocess.SubprocessError as err:
message = f"Command 'sphinx-build docs {path}' failed.\nErr:{err}"
raise ApeDocsBuildError(message) from err
return path
def main():
"""
There are three GH events we build for:
1. Push to main: we build into 'latest/'.
The GH action will commit these changes to the 'gh-pages' branch.
2. Release: we copy 'latest/' into the release dir, as well as to 'stable/'.
The GH action will commit these changes to the 'gh-pages' branch.
3. Pull requests or local development: We ensure a successful build.
"""
event_name = os.environ.get("GITHUB_EVENT_NAME")
is_ephemeral = event_name in ["pull_request", None]
if event_name == "push" or is_ephemeral:
build_docs(LATEST_PATH)
elif event_name == "release":
tag = git("describe", "--tag")
if not tag:
raise ApeDocsBuildError("Unable to find release tag.")
if "beta" not in tag and "alpha" not in tag:
build_dir = DOCS_BUILD_PATH / tag
build_docs(build_dir)
# Clean-up unnecessary extra 'fonts/' directories to save space.
# There should still be one in 'latest/'
for font_dirs in build_dir.glob("**/fonts"):
if font_dirs.exists():
shutil.rmtree(font_dirs)
shutil.copytree(build_dir, STABLE_PATH)
else:
build_docs(STABLE_PATH)
# Set up the redirect at /index.html
DOCS_BUILD_PATH.mkdir(exist_ok=True, parents=True)
with open(DOCS_BUILD_PATH / "index.html", "w") as f:
redirect = "latest" if is_ephemeral else "stable"
f.write(REDIRECT_HTML.format(redirect))
if __name__ == "__main__":
main()