-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_docs.py
More file actions
executable file
·132 lines (103 loc) · 3.5 KB
/
build_docs.py
File metadata and controls
executable file
·132 lines (103 loc) · 3.5 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
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
"""
Script to build AudioSamples Python documentation locally.
Usage:
python build_docs.py [--clean] [--serve]
Options:
--clean Clean the build directory before building
--serve Start a local HTTP server to view the docs after building
"""
import argparse
import os
import shutil
import subprocess
import sys
import webbrowser
from http.server import HTTPServer, SimpleHTTPRequestHandler
from pathlib import Path
import threading
import time
def clean_build():
"""Remove the build directory."""
build_dir = Path(__file__).parent / "docs" / "build"
if build_dir.exists():
print(f"Cleaning {build_dir}")
shutil.rmtree(build_dir)
def build_docs():
"""Build the documentation using Sphinx."""
docs_dir = Path(__file__).parent / "docs"
if not docs_dir.exists():
print("Error: docs directory not found")
return False
print("Building documentation with Sphinx...")
try:
result = subprocess.run(
["make", "html"],
cwd=docs_dir,
capture_output=True,
text=True
)
if result.returncode == 0:
print("Documentation built successfully!")
html_dir = docs_dir / "build" / "html"
print(f"Documentation available at: {html_dir / 'index.html'}")
return True
else:
print("Error building documentation:")
print(result.stdout)
print(result.stderr)
return False
except subprocess.CalledProcessError as e:
print(f"Error running make: {e}")
return False
def serve_docs(port=8000):
"""Start a local HTTP server to view the documentation."""
html_dir = Path(__file__).parent / "docs" / "build" / "html"
if not html_dir.exists():
print("Error: Built documentation not found. Run build first.")
return False
os.chdir(html_dir)
class CustomHandler(SimpleHTTPRequestHandler):
def log_message(self, format, *args):
# Suppress server logs
pass
server = HTTPServer(('localhost', port), CustomHandler)
def start_server():
print(f"Serving documentation at http://localhost:{port}")
print("Press Ctrl+C to stop the server")
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nStopping server...")
server.server_close()
# Start server in a separate thread
server_thread = threading.Thread(target=start_server, daemon=True)
server_thread.start()
# Open browser after a short delay
time.sleep(1)
url = f"http://localhost:{port}"
print(f"Opening {url} in browser...")
webbrowser.open(url)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nShutting down...")
server.server_close()
return True
def main():
parser = argparse.ArgumentParser(description="Build AudioSamples Python documentation")
parser.add_argument("--clean", action="store_true", help="Clean build directory first")
parser.add_argument("--serve", action="store_true", help="Serve docs locally after building")
parser.add_argument("--port", type=int, default=8000, help="Port for local server (default: 8000)")
args = parser.parse_args()
if args.clean:
clean_build()
# Build documentation
if not build_docs():
sys.exit(1)
# Serve if requested
if args.serve:
serve_docs(args.port)
if __name__ == "__main__":
main()