-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.py
executable file
·65 lines (53 loc) · 1.89 KB
/
preview.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
#!/usr/bin/env python
"""Builds preview pages of the theme and starts a webserver if not already started
"""
from functools import partial
from http.server import SimpleHTTPRequestHandler
from pathlib import Path
from socketserver import TCPServer
from tempfile import TemporaryDirectory
from sphinx.cmd.build import build_main as sphinx_main
DEMO_SRC_DIR = Path(__file__).parent / "tests" / "sites" / "base"
BUILDER = "html"
PORT = 5000
def main() -> int:
"""Run a build and start a http server
:return: Exit code for the process
:rtype: int
"""
with TemporaryDirectory() as build_dir:
build_dir = Path(build_dir)
build(BUILDER, DEMO_SRC_DIR, build_dir)
return serve(build_dir, PORT)
def build(builder: str, source_dir: Path, build_dir: Path) -> int:
"""Build the demo pages from source_dir
into build_dir using the builder
:param builder: Sphinx builder type
:type builder: str
:param source_dir: Directory of source conf.py file
:type source_dir: Path
:param build_dir: Destination of built files
:type build_dir: Path
:return: Exit code from sphinx build
:rtype: int
"""
return sphinx_main(["-b", builder, str(source_dir), str(build_dir)])
def serve(serve_root: Path, port: int):
"""Start a http server on the given port
:param serve_root: The root directory to serve the files
:type serve_root: Path
:param port: Serve on this port
:type port: int
"""
handler_class = partial(SimpleHTTPRequestHandler, directory=str(serve_root))
with TCPServer(("", port), handler_class) as httpd:
print(
f"Serving HTTP on localhost port {port} " f"(http://localhost:{port}/) ..."
)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nKeyboard interrupt received, exiting.")
return 0
if __name__ == "__main__":
main()