-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgenerate-docs-simple.py
More file actions
184 lines (160 loc) Β· 5.42 KB
/
generate-docs-simple.py
File metadata and controls
184 lines (160 loc) Β· 5.42 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
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
# AUTO-GENERATED FILE - DO NOT EDIT
# This file was automatically generated by the XDK build tool.
# Any manual changes will be overwritten on the next generation.
#!/usr/bin/env python3
"""
AUTO-GENERATED FILE - DO NOT EDIT
This file was automatically generated by the XDK build tool.
Any manual changes will be overwritten on the next generation.
Generate API documentation for the Python SDK using Sphinx with markdown output.
"""
import os
import sys
import shutil
import subprocess
from pathlib import Path
print("π Generating X API SDK Documentation...")
# Try to use virtual environment if available
venv_python = Path(".venv") / "bin" / "python"
if venv_python.exists():
print("π¦ Using virtual environment...")
python_exe = str(venv_python)
else:
python_exe = sys.executable
# Configuration
DOCS_DIR = Path("docs")
SPHINX_SOURCE_DIR = Path("docs_source")
SPHINX_BUILD_DIR = DOCS_DIR / "_build"
SPHINX_MARKDOWN_DIR = DOCS_DIR
# Clean up old docs
if DOCS_DIR.exists():
shutil.rmtree(DOCS_DIR)
DOCS_DIR.mkdir(parents=True, exist_ok=True)
# Clean up old source
if SPHINX_SOURCE_DIR.exists():
shutil.rmtree(SPHINX_SOURCE_DIR)
# Create Sphinx source directory structure
SPHINX_SOURCE_DIR.mkdir(parents=True, exist_ok=True)
(SPHINX_SOURCE_DIR / "_static").mkdir(exist_ok=True)
(SPHINX_SOURCE_DIR / "_templates").mkdir(exist_ok=True)
# Copy conf.py from root (should be generated)
conf_py_path = Path("conf.py")
if conf_py_path.exists():
shutil.copy(conf_py_path, SPHINX_SOURCE_DIR / "conf.py")
else:
print("β οΈ Warning: conf.py not found. Sphinx may not work correctly.")
try:
# Use sphinx-apidoc to auto-generate API documentation
print("π Running sphinx-apidoc to generate API documentation structure...")
apidoc_cmd = [
python_exe,
"-m",
"sphinx.ext.apidoc",
"-o",
str(SPHINX_SOURCE_DIR),
"-f", # Force overwrite
"--separate", # Put each module in its own file
"xdk", # Source package
]
subprocess.run(apidoc_cmd, check=True, capture_output=True, text=True)
# Create or update index.rst
index_content = """X API SDK Documentation
==========================
Welcome to the X API SDK documentation.
.. toctree::
:maxdepth: 2
:caption: API Reference:
modules
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
"""
(SPHINX_SOURCE_DIR / "index.rst").write_text(index_content)
# Run Sphinx with markdown builder
print("π Running Sphinx to generate markdown documentation...")
sphinx_cmd = [
python_exe,
"-m",
"sphinx",
"-b",
"markdown", # Use markdown builder
str(SPHINX_SOURCE_DIR),
str(SPHINX_MARKDOWN_DIR),
]
result = subprocess.run(sphinx_cmd, check=True, capture_output=True, text=True)
print("β
Documentation generated successfully in docs/")
# Clean up build directory
if SPHINX_BUILD_DIR.exists():
shutil.rmtree(SPHINX_BUILD_DIR)
# Clean up source directory
if SPHINX_SOURCE_DIR.exists():
shutil.rmtree(SPHINX_SOURCE_DIR)
except subprocess.CalledProcessError as e:
print(f"β Documentation generation failed: {e}")
if e.stdout:
print(f"stdout: {e.stdout}")
if e.stderr:
print(f"stderr: {e.stderr}")
sys.exit(1)
except FileNotFoundError:
print("β Sphinx not found.")
print("π‘ Installing dependencies...")
try:
# Try to install using uv if available
if shutil.which("uv"):
subprocess.run(
["uv", "pip", "install", "-e", ".[dev]"], check=True, shell=False
)
else:
# Fallback to pip
subprocess.run(
[
sys.executable,
"-m",
"pip",
"install",
"sphinx",
"myst-parser",
"sphinx-markdown-builder",
],
check=True,
)
print("β
Dependencies installed. Please run the script again.")
except subprocess.CalledProcessError:
print("β Failed to install dependencies automatically.")
print("π‘ Please install manually with:")
print(" uv pip install -e '.[dev]'")
print(" or")
print(" pip install sphinx myst-parser sphinx-markdown-builder")
sys.exit(1)
except ImportError as e:
if "myst_parser" in str(e) or "myst-parser" in str(e):
print("β myst-parser not found.")
print("π‘ Installing dependencies...")
try:
if shutil.which("uv"):
subprocess.run(
["uv", "pip", "install", "-e", ".[dev]"], check=True, shell=False
)
else:
subprocess.run(
[
sys.executable,
"-m",
"pip",
"install",
"myst-parser",
"sphinx-markdown-builder",
],
check=True,
)
print("β
Dependencies installed. Please run the script again.")
except subprocess.CalledProcessError:
print("β Failed to install dependencies automatically.")
print("π‘ Please install manually with:")
print(" uv pip install -e '.[dev]'")
else:
raise
sys.exit(1)