-
Notifications
You must be signed in to change notification settings - Fork 213
/
build-docs.py
executable file
·77 lines (56 loc) · 2.05 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
#!/usr/bin/env python3
# flake8: noqa
"""Build the docs with https://www.sphinx-doc.org."""
import functools
import pathlib
import sys
import webbrowser
from sphinx.cmd import build
SELF = pathlib.Path(__file__)
SOURCE = pathlib.Path('docs')
TARGET = SOURCE / '_build'
RESULT = TARGET / 'index.html'
BROWSER_OPEN = '--open'
SKIP_OPEN_RESULT = '--no-open'
DEFAULT_ARGS = [BROWSER_OPEN, '-W', '-n', '-v', str(SOURCE), str(TARGET)]
OPEN_RESULT = BROWSER_OPEN in DEFAULT_ARGS
print = functools.partial(print, sep='\n')
args = sys.argv[1:]
print(f'run {[SELF.name] + args}')
if not args:
args = DEFAULT_ARGS
if SKIP_OPEN_RESULT in args:
open_result = None
args = [a for a in args
for name, value in [a.partition('=')[::2]]
if name not in (SKIP_OPEN_RESULT, BROWSER_OPEN)]
elif any(a.partition('=')[0] == BROWSER_OPEN for a in args):
open_result = RESULT
values = {value for a in args
for name, value in [a.partition('=')[::2]]
if name == BROWSER_OPEN and value}
if values:
if len(values) != 1:
raise ValueError(f'conflicting {BROWSER_OPEN}: {values}')
value, = values
if value:
open_result = open_result.parent / value
args = [a for a in args if a.partition('=')[0] != BROWSER_OPEN]
if not args: # no pytest args given
args = [a for a in DEFAULT_ARGS
if a != SKIP_OPEN_RESULT and a.partition('=')[0] != BROWSER_OPEN]
if args == ['-b', 'doctest']:
args += ['-W', str(SOURCE), str(SOURCE / '_doctest')]
print('', f'sphinx.cmd.build.main({args})',)
returncode = build.main(args)
status = 'FAILED' if returncode else 'PASSED'
print('', f'{status} (returncode {returncode!r})', end='')
try:
if 'doctest' not in args:
print('', f'index: {RESULT}', f'assert {RESULT!r}.stat().st_size', end='')
assert open_result.stat().st_size, f'non-empty {open_result}'
if open_result:
print('', f'webbrowser.open({open_result!r})', end='')
webbrowser.open(open_result)
finally:
sys.exit(returncode)