Skip to content

Commit 2f46873

Browse files
authored
docs: docgen python apis (bazel-contrib#2149)
Uses autodoc2 to generate Python documentation for runfiles and sphinx_bzl. This provides some basic API doc for our Python code. They don't look particularly great, yet, but we can work on how they look in another PR. Also: * Fixes position of license rule and extra space in license text * Forces sphinx_rtd_theme >= 2.0. uv kept trying to downgrade it for some reason. * Use directives markup to document the sphinx_bzl directives * Add `sphinx_run` rule to make it easier to run Sphinx interactively for debugging
1 parent e331afe commit 2f46873

File tree

18 files changed

+468
-111
lines changed

18 files changed

+468
-111
lines changed

docs/BUILD.bazel

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable
1919
load("//python/uv/private:lock.bzl", "lock") # buildifier: disable=bzl-visibility
2020
load("//sphinxdocs:readthedocs.bzl", "readthedocs_install")
2121
load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs")
22+
load("//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
2223
load("//sphinxdocs:sphinx_stardoc.bzl", "sphinx_stardoc", "sphinx_stardocs")
2324

2425
package(default_visibility = ["//:__subpackages__"])
2526

27+
licenses(["notice"]) # Apache 2.0
28+
2629
# We only build for Linux and Mac because:
2730
# 1. The actual doc process only runs on Linux
2831
# 2. Mac is a common development platform, and is close enough to Linux
@@ -68,6 +71,7 @@ sphinx_docs(
6871
target_compatible_with = _TARGET_COMPATIBLE_WITH,
6972
deps = [
7073
":bzl_api_docs",
74+
":py_api_srcs",
7175
"//sphinxdocs/docs:docs_lib",
7276
],
7377
)
@@ -97,15 +101,15 @@ sphinx_stardocs(
97101
# This depends on @pythons_hub, which is only created under bzlmod,
98102
"//python/extensions:pip_bzl",
99103
] if IS_BAZEL_7_OR_HIGHER and BZLMOD_ENABLED else []),
100-
prefix = "api/",
104+
prefix = "api/rules_python/",
101105
tags = ["docs"],
102106
target_compatible_with = _TARGET_COMPATIBLE_WITH,
103107
)
104108

105109
sphinx_stardoc(
106110
name = "py_cc_toolchain",
107111
src = "//python/private:py_cc_toolchain_rule.bzl",
108-
prefix = "api/",
112+
prefix = "api/rules_python/",
109113
public_load_path = "//python/cc:py_cc_toolchain.bzl",
110114
tags = ["docs"],
111115
target_compatible_with = _TARGET_COMPATIBLE_WITH,
@@ -115,11 +119,20 @@ sphinx_stardoc(
115119
sphinx_stardoc(
116120
name = "py_runtime_pair",
117121
src = "//python/private:py_runtime_pair_rule_bzl",
122+
prefix = "api/rules_python",
118123
public_load_path = "//python:py_runtime_pair.bzl",
119124
tags = ["docs"],
120125
target_compatible_with = _TARGET_COMPATIBLE_WITH,
121126
)
122127

128+
sphinx_docs_library(
129+
name = "py_api_srcs",
130+
srcs = [
131+
"//python/runfiles",
132+
],
133+
strip_prefix = "python/",
134+
)
135+
123136
readthedocs_install(
124137
name = "readthedocs_install",
125138
docs = [":docs"],
@@ -135,6 +148,8 @@ sphinx_build_binary(
135148
requirement("myst_parser"),
136149
requirement("readthedocs_sphinx_ext"),
137150
requirement("typing_extensions"),
151+
requirement("sphinx_autodoc2"),
152+
requirement("sphinx_reredirects"),
138153
"//sphinxdocs/src/sphinx_bzl",
139154
],
140155
)
@@ -147,8 +162,6 @@ lock(
147162
upgrade = True,
148163
)
149164

150-
licenses(["notice"]) # Apache 2.0
151-
152165
# Temporary compatibility aliases for some other projects depending on the old
153166
# bzl_library targets.
154167
alias(

docs/api/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
```{toctree}
44
:glob:
5-
**
5+
*
6+
*/index
67
```

docs/api/rules_python/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# rules_python Bazel APIs
2+
3+
API documentation for rules_python Bazel objects.
4+
5+
```{toctree}
6+
:glob:
7+
**
8+
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

docs/conf.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,81 @@
1818
# Any extensions here not built into Sphinx must also be added to
1919
# the dependencies of //docs:sphinx-builder
2020
extensions = [
21-
"sphinx.ext.autodoc",
21+
"autodoc2",
2222
"sphinx.ext.autosectionlabel",
23-
"sphinx.ext.autosummary",
2423
"sphinx.ext.doctest",
2524
"sphinx.ext.duration",
2625
"sphinx.ext.extlinks",
2726
"sphinx.ext.intersphinx",
2827
"myst_parser",
2928
"sphinx_rtd_theme", # Necessary to get jquery to make flyout work
3029
"sphinx_bzl.bzl",
30+
"sphinx_reredirects",
3131
]
3232

33+
autodoc2_packages = [
34+
"sphinx_bzl",
35+
"runfiles",
36+
]
37+
38+
autodoc2_output_dir = "api/py"
39+
autodoc2_sort_names = True
40+
autodoc2_class_docstring = "both"
41+
autodoc2_index_template = """
42+
Python APIs
43+
====================
44+
45+
This page contains auto-generated API reference documentation [#f1]_.
46+
47+
.. toctree::
48+
:titlesonly:
49+
50+
{% for package in top_level %}
51+
{{ package }}
52+
{%- endfor %}
53+
54+
.. [#f1] Created with `sphinx-autodoc2 <https://github.com/chrisjsewell/sphinx-autodoc2>`_
55+
56+
"""
57+
58+
59+
autodoc2_docstring_parser_regexes = [
60+
(".*", "myst"),
61+
]
62+
63+
# NOTE: The redirects generation will clobber existing files.
64+
redirects = {
65+
"api/tools/precompiler/index": "/api/rules_python/tools/precompiler/index.html",
66+
"api/python/py_library": "/api/rules_python/python/py_library.html",
67+
"api/python/py_binary": "/api/rules_python/python/py_binary.html",
68+
"api/python/py_test": "/api/rules_python/python/py_test.html",
69+
"api/python/defs": "/api/rules_python/python/defs.html",
70+
"api/python/index": "/api/rules_python/python/index.html",
71+
"api/python/py_runtime_info": "/api/rules_python/python/py_runtime_info.html",
72+
"api/python/private/common/py_library_rule_bazel": "/api/rules_python/python/private/common/py_library_rule_bazel.html",
73+
"api/python/private/common/py_test_rule_bazel": "/api/rules_python/python/private/common/py_test_rule_bazel.html",
74+
"api/python/private/common/py_binary_rule_bazel": "/api/rules_python/python/private/common/py_binary_rule_bazel.html",
75+
"api/python/private/common/py_runtime_rule": "/api/rules_python/python/private/common/py_runtime_rule.html",
76+
"api/python/extensions/pip": "/api/rules_python/python/extensions/pip.html",
77+
"api/python/extensions/python": "/api/rules_python/python/extensions/python.html",
78+
"api/python/entry_points/py_console_script_binary": "/api/rules_python/python/entry_points/py_console_script_binary.html",
79+
"api/python/cc/py_cc_toolchain_info": "/api/rules_python/python/cc/py_cc_toolchain_info.html",
80+
"api/python/cc/index": "/api/rules_python/python/cc/index.html",
81+
"api/python/py_cc_link_params_info": "/api/rules_python/python/py_cc_link_params_info.html",
82+
"api/python/runtime_env_toolchains/index": "/api/rules_python/python/runtime_env_toolchains/index.html",
83+
"api/python/pip": "/api/rules_python/python/pip.html",
84+
"api/python/config_settings/index": "/api/rules_python/python/config_settings/index.html",
85+
"api/python/packaging": "/api/rules_python/python/packaging.html",
86+
"api/python/py_runtime": "/api/rules_python/python/py_runtime.html",
87+
"api/sphinxdocs/sphinx": "/api/sphinxdocs/sphinxdocs/sphinx.html",
88+
"api/sphinxdocs/sphinx_stardoc": "/api/sphinxdocs/sphinxdocs/sphinx_stardoc.html",
89+
"api/sphinxdocs/readthedocs": "/api/sphinxdocs/sphinxdocs/readthedocs.html",
90+
"api/sphinxdocs/index": "/api/sphinxdocs/sphinxdocs/index.html",
91+
"api/sphinxdocs/private/sphinx_docs_library": "/api/sphinxdocs/sphinxdocs/private/sphinx_docs_library.html",
92+
"api/sphinxdocs/sphinx_docs_library": "/api/sphinxdocs/sphinxdocs/sphinx_docs_library.html",
93+
"api/sphinxdocs/inventories/index": "/api/sphinxdocs/sphinxdocs/inventories/index.html",
94+
}
95+
3396
# Adapted from the template code:
3497
# https://github.com/readthedocs/readthedocs.org/blob/main/readthedocs/doc_builder/templates/doc_builder/conf.py.tmpl
3598
if os.environ.get("READTHEDOCS") == "True":

docs/pyproject.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ version = "0.0.0"
55
dependencies = [
66
# NOTE: This is only used as input to create the resolved requirements.txt
77
# file, which is what builds, both Bazel and Readthedocs, both use.
8+
"sphinx-autodoc2",
89
"sphinx",
910
"myst-parser",
10-
"sphinx_rtd_theme",
11+
"sphinx_rtd_theme >=2.0", # uv insists on downgrading for some reason
1112
"readthedocs-sphinx-ext",
1213
"absl-py",
13-
"typing-extensions"
14+
"typing-extensions",
15+
"sphinx-reredirects"
1416
]

0 commit comments

Comments
 (0)