xref: /aosp_15_r20/external/bazelbuild-rules_python/docs/conf.py (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Configuration file for the Sphinx documentation builder.
2
3import os
4
5# -- Project information
6project = "rules_python"
7copyright = "2023, The Bazel Authors"
8author = "Bazel"
9
10# NOTE: These are overriden by -D flags via --//sphinxdocs:extra_defines
11version = "0.0.0"
12release = version
13
14# -- General configuration
15# See https://www.sphinx-doc.org/en/master/usage/configuration.html
16# for more settings
17
18# Any extensions here not built into Sphinx must also be added to
19# the dependencies of //docs:sphinx-builder
20extensions = [
21    "autodoc2",
22    "sphinx.ext.autosectionlabel",
23    "sphinx.ext.doctest",
24    "sphinx.ext.duration",
25    "sphinx.ext.extlinks",
26    "sphinx.ext.intersphinx",
27    "myst_parser",
28    "sphinx_rtd_theme",  # Necessary to get jquery to make flyout work
29    "sphinx_bzl.bzl",
30    "sphinx_reredirects",
31]
32
33autodoc2_packages = [
34    "sphinx_bzl",
35    "runfiles",
36]
37
38autodoc2_output_dir = "api/py"
39autodoc2_sort_names = True
40autodoc2_class_docstring = "both"
41autodoc2_index_template = """
42Python APIs
43====================
44
45This 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
59autodoc2_docstring_parser_regexes = [
60    (".*", "myst"),
61]
62
63# NOTE: The redirects generation will clobber existing files.
64redirects = {
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
96# Adapted from the template code:
97# https://github.com/readthedocs/readthedocs.org/blob/main/readthedocs/doc_builder/templates/doc_builder/conf.py.tmpl
98if os.environ.get("READTHEDOCS") == "True":
99    # Must come first because it can interfere with other extensions, according
100    # to the original conf.py template comments
101    extensions.insert(0, "readthedocs_ext.readthedocs")
102
103    if os.environ.get("READTHEDOCS_VERSION_TYPE") == "external":
104        # Insert after the main extension
105        extensions.insert(1, "readthedocs_ext.external_version_warning")
106        readthedocs_vcs_url = (
107            "http://github.com/bazelbuild/rules_python/pull/{}".format(
108                os.environ.get("READTHEDOCS_VERSION", "")
109            )
110        )
111        # The build id isn't directly available, but it appears to be encoded
112        # into the host name, so we can parse it from that. The format appears
113        # to be `build-X-project-Y-Z`, where:
114        # * X is an integer build id
115        # * Y is an integer project id
116        # * Z is the project name
117        _build_id = os.environ.get("HOSTNAME", "build-0-project-0-rules-python")
118        _build_id = _build_id.split("-")[1]
119        readthedocs_build_url = (
120            f"https://readthedocs.org/projects/rules-python/builds/{_build_id}"
121        )
122
123exclude_patterns = ["_includes/*"]
124templates_path = ["_templates"]
125primary_domain = None  # The default is 'py', which we don't make much use of
126nitpicky = True
127
128# --- Intersphinx configuration
129
130intersphinx_mapping = {
131    "bazel": ("https://bazel.build/", "bazel_inventory.inv"),
132}
133
134# --- Extlinks configuration
135extlinks = {
136    "gh-path": (f"https://github.com/bazelbuild/rules_python/tree/main/%s", "%s"),
137}
138
139# --- MyST configuration
140# See https://myst-parser.readthedocs.io/en/latest/configuration.html
141# for more settings
142
143# See https://myst-parser.readthedocs.io/en/latest/syntax/optional.html
144# for additional extensions.
145myst_enable_extensions = [
146    "fieldlist",
147    "attrs_block",
148    "attrs_inline",
149    "colon_fence",
150    "deflist",
151    "substitution",
152]
153
154myst_substitutions = {}
155
156# --- sphinx_stardoc configuration
157
158bzl_default_repository_name = "@rules_python"
159
160# -- Options for HTML output
161# See https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
162# For additional html settings
163
164# See https://sphinx-rtd-theme.readthedocs.io/en/stable/configuring.html for
165# them-specific options
166html_theme = "sphinx_rtd_theme"
167html_theme_options = {}
168
169# The html_context settings are part of the jinja context used by the themes.
170html_context = {
171    # This controls whether the flyout menu is shown. It is always false
172    # because:
173    # * For local builds, the flyout menu is empty and doesn't show in the
174    #   same place as for RTD builds. No point in showing it locally.
175    # * For RTD builds, the flyout menu is always automatically injected,
176    #   so having it be True makes the flyout show up twice.
177    "READTHEDOCS": False,
178    "PRODUCTION_DOMAIN": "readthedocs.org",
179    # This is the path to a page's source (after the github user/repo/commit)
180    "conf_py_path": "/docs/",
181    "github_user": "bazelbuild",
182    "github_repo": "rules_python",
183    # The git version that was checked out, e.g. the tag or branch name
184    "github_version": os.environ.get("READTHEDOCS_GIT_IDENTIFIER", ""),
185    # For local builds, the github link won't work. Disabling it replaces
186    # it with a "view source" link to view the source Sphinx saw, which
187    # is useful for local development.
188    "display_github": os.environ.get("READTHEDOCS") == "True",
189    "commit": os.environ.get("READTHEDOCS_GIT_COMMIT_HASH", "unknown commit"),
190    # Used by readthedocs_ext.external_version_warning extension
191    # This is the PR number being built
192    "current_version": os.environ.get("READTHEDOCS_VERSION", ""),
193}
194
195# Keep this in sync with the stardoc templates
196html_permalinks_icon = "¶"
197
198# These folders are copied to the documentation's HTML output
199html_static_path = ["_static"]
200
201# These paths are either relative to html_static_path
202# or fully qualified paths (eg. https://...)
203html_css_files = [
204    "css/custom.css",
205]
206
207# -- Options for EPUB output
208epub_show_urls = "footnote"
209
210suppress_warnings = [
211    # The autosectionlabel extension turns header titles into referencable
212    # names. Unfortunately, CHANGELOG.md has many duplicate header titles,
213    # which creates lots of warning spam. Just ignore them.
214    "autosectionlabel.*"
215]
216
217
218def setup(app):
219    # Pygments says it supports starlark, but it doesn't seem to actually
220    # recognize `starlark` as a name. So just manually map it to python.
221    from sphinx.highlighting import lexer_classes
222
223    app.add_lexer("starlark", lexer_classes["python"])
224