-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
231 lines (204 loc) · 6.9 KB
/
manage.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os
import shutil
import subprocess
import sys
from argparse import ArgumentParser
from collections.abc import Sequence
from contextlib import suppress
from pathlib import Path
import pytest
from rich import print
from rich_argparse import RichHelpFormatter
PROJECT_ROOT: Path = Path(__file__).resolve().parent
SOURCE_RELATIVE_PATH: Path = Path("./docs/source/")
SOURCE_DIR: Path = PROJECT_ROOT.joinpath(SOURCE_RELATIVE_PATH)
BUILD_RELATIVE_PATH: Path = Path("./docs/build/")
BUILD_DIR: Path = PROJECT_ROOT.joinpath(BUILD_RELATIVE_PATH)
AVAILABLE_BUILDERS: tuple[str, ...] = (
"html",
"dirhtml",
"singlehtml",
"htmlhelp",
"qthelp",
"devhelp",
"epub",
"applehelp",
"latex",
"man",
"texinfo",
"text",
"gettext",
"doctest",
"linkcheck",
"xml",
"pseudoxml",
)
BUILD_HELP: str = (
"Builder to pass to `sphinx-build` (Defaults to html). The available builders are: "
f"{', '.join(f'[green]{b}[/]' for b in AVAILABLE_BUILDERS)}. "
"See https://www.sphinx-doc.org/en/master/usage/builders/index.html for more "
"information about sphinx builders."
)
def clean_docs() -> int:
print("Removing", BUILD_DIR)
shutil.rmtree(BUILD_DIR, ignore_errors=True)
return 0
def build_docs(builder: str) -> int:
output_dir = BUILD_DIR.joinpath(builder)
with suppress(KeyboardInterrupt):
res = subprocess.run(["sphinx-build", SOURCE_DIR, output_dir, "-b", builder])
return res.returncode
# TODO: Change `delay` to float (See below)
def serve_docs(builder: str, *, open_browser: bool, delay: int) -> int:
output_dir = BUILD_DIR.joinpath(builder)
auto_build_cmd = [
"sphinx-autobuild",
SOURCE_DIR,
output_dir,
"--watch",
"src",
"-b",
builder,
]
if open_browser:
auto_build_cmd.append("--open-browser")
auto_build_cmd.append("--delay")
auto_build_cmd.append(str(delay))
with suppress(KeyboardInterrupt):
res = subprocess.run(auto_build_cmd)
return res.returncode
def run_tests() -> int:
return pytest.main([])
def setup(*, dry_run: bool) -> int:
if dry_run:
print("Steps to setup the project:")
print(" 1. Install `pre-commit` hooks: `pre-commit install`")
return 0
with suppress(KeyboardInterrupt):
print("Installing `pre-commit` hooks", end="\n\n")
res = subprocess.run(["pre-commit", "install"])
if res.returncode == 0:
print("[green]Done![/]")
else:
print(
"[red]Couldn't install `pre-commit` hooks. Aborting setup.[/]",
file=sys.stderr,
)
return res.returncode
return 0
def get_parser() -> ArgumentParser:
RichHelpFormatter.highlights.append(r"\b(?P<metavar>BUILDER)\b")
parser = ArgumentParser(
description="Utility scripts and commands to manage the project's development.",
formatter_class=RichHelpFormatter,
)
subparsers = parser.add_subparsers(required=True, title="commands")
# docs group
docs_parser = subparsers.add_parser(
"docs",
description="Commands to manage the documentation of the project.",
help="Documentation management",
formatter_class=parser.formatter_class,
)
docs_subparsers = docs_parser.add_subparsers(required=True, title="commands")
docs_clean_parser = docs_subparsers.add_parser(
"clean",
description=(
"Delete the generated documentation directory and all of its contents."
),
help="Remove built documentation",
formatter_class=docs_parser.formatter_class,
)
docs_clean_parser.set_defaults(command=clean_docs)
docs_build_parser = docs_subparsers.add_parser(
"build",
description=(
"Build the documentation using `sphinx-build`. The output will be placed "
f"in {BUILD_RELATIVE_PATH.joinpath('BUILDER')} relative to the project "
"root. BUILDER is the parameter passed to --builder (for example: html)."
),
help="Build documentation",
formatter_class=docs_parser.formatter_class,
)
docs_build_parser.add_argument(
"-b",
"--builder",
default="html",
choices=AVAILABLE_BUILDERS,
metavar="BUILDER",
help=BUILD_HELP,
)
docs_build_parser.set_defaults(command=build_docs)
docs_serve_parser = docs_subparsers.add_parser(
"serve",
description=(
"Spawn a server that serves the documentation and reloads automatically "
"when files are changed (live reloading) using `sphinx-autobuild`. Like "
"the [blue]build[/] command, the output will be placed in "
f"{BUILD_RELATIVE_PATH.joinpath('BUILDER')} relative to the project root "
"where BUILDER is the value of the parameter --builder."
),
help="Serve documentation with live reloading",
formatter_class=docs_parser.formatter_class,
)
docs_serve_parser.add_argument(
"-b",
"--builder",
default="html",
choices=AVAILABLE_BUILDERS,
metavar="BUILDER",
help=BUILD_HELP,
)
docs_serve_parser.add_argument(
"-o",
"--open-browser",
action="store_true",
help="Open the browser after building the documentation",
)
# TODO: Change parameter type to float once `sphinx-autobuild` supports floats
docs_serve_parser.add_argument(
"-d",
"--delay",
type=int,
default=0,
help=(
"How long to wait before opening the browser in seconds "
"(Defaults to 0, that is, no delay). Note that this parameter has no "
"effect without the --open-browser parameter."
),
)
docs_serve_parser.set_defaults(command=serve_docs)
# test command
test_parser = subparsers.add_parser(
"test",
description="Run tests using `pytest`.",
help="Run tests",
formatter_class=parser.formatter_class,
)
test_parser.set_defaults(command=run_tests)
# setup command
setup_parser = subparsers.add_parser(
"setup",
description=(
"Perform initial setup of the project. This should be the first thing to "
"do after cloning the repository."
),
help="Initial setup of the project when start developing",
formatter_class=parser.formatter_class,
)
setup_parser.add_argument(
"-d",
"--dry-run",
action="store_true",
help="Print what running the setup command would do without doing it",
)
setup_parser.set_defaults(command=setup)
return parser
def main(args: Sequence[str] | None = None) -> None:
parser = get_parser()
args = vars(parser.parse_args(args))
os.chdir(PROJECT_ROOT)
command = args.pop("command")
sys.exit(command(**args))
if __name__ == "__main__":
main()