Skip to content

Commit 327b4e3

Browse files
authored
docs: make readthedocs render a bit nicer and port docs over to Sphinx (bazel-contrib#1511)
This makes the Sphinx-based docs hosted on readthedocs render a bit more nicely, fixes a few issues, and adds some features to //sphinxdocs This also moves all the docs onto Sphinx, deleting the checked-in documentation. Doc fixes/improvements: * Ports various docs over to Sphinx pages. They're split out from the readme file. * Version RTD is building is reflected in the docs * Fixes some references to github files * Includes the custom CSS file that styled the api docs * Removes `-Q` from doc building; all warnings should be fixed now * Added Bazel inventory file. Bazel doesn't provide one, but we can manually provide on and still use intersphinx functionality. * Added `gh-path` custom role. This is a shortcut for writing the whole github URL. * Sets the primary domain to None. The default is py, which we don't use much of, so it just results in confusing crossref errors. * Enable nitpicky mode to catch more errors. * Remove the `starlark` marker from codeblocks; that name isn't recognized by Sphinx. The highlighting is still sufficient. * Adds a glossary Sphinxdocs improvements: * Added a flag to pass along arbitrary `-D` args to the Sphinx invocations. This allows e.g., the `version` setting of the docs to be set on the command line from the `READTHEDOCS_VERSION` environment variable * Added inventory file generation. These are files that allow referencing external projects using intersphinx. * `sphinx_stardocs` have their public load path set as their page title. This groups the API docs more naturally by file. The path can be customized. * `sphinx_stardocs` can have a footer specified for generated pages. This allows easily added a list of link labels for easy re-use. * `readthedocs_install` now tries harder to find an open port * The conf.py file is moved into the generated sources directly. This was done because some config settings are relative to the conf.py file, which was being placed one directory above the regular sources. Fixes bazel-contrib#1484, bazel-contrib#1481
1 parent a9a0e59 commit 327b4e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1262
-1782
lines changed

.bazelrc

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ startup --windows_enable_symlinks
2323
# TODO: migrate all dependencies from WORKSPACE to MODULE.bazel
2424
# https://github.com/bazelbuild/rules_python/issues/1469
2525
common --noexperimental_enable_bzlmod
26+
27+
# Additional config to use for readthedocs builds.
28+
# See .readthedocs.yml for additional flags
29+
build:rtd --stamp

.readthedocs.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ build:
66
tools:
77
nodejs: "19"
88
commands:
9+
- env
910
- npm install -g @bazel/bazelisk
10-
- bazel run //docs/sphinx:readthedocs_install
11+
- bazel run --config=rtd --//sphinxdocs:extra_defines=version=$READTHEDOCS_VERSION //docs/sphinx:readthedocs_install

README.md

+5-350
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ This repository is the home of the core Python rules -- `py_library`,
88
`py_binary`, `py_test`, `py_proto_library`, and related symbols that provide the basis for Python
99
support in Bazel. It also contains package installation rules for integrating with PyPI and other indices.
1010

11-
Documentation for rules_python lives in the
12-
[`docs/`](https://github.com/bazelbuild/rules_python/tree/main/docs)
13-
directory and in the
11+
Documentation for rules_python is at <https://rules-python.readthedocs.io> and in the
1412
[Bazel Build Encyclopedia](https://docs.bazel.build/versions/master/be/python.html).
1513

1614
Examples live in the [examples](examples) directory.
@@ -25,356 +23,13 @@ rate, but this repository will still follow [semantic versioning](https://semver
2523

2624
The Bazel community maintains this repository. Neither Google nor the Bazel team provides support for the code. However, this repository is part of the test suite used to vet new Bazel releases. See [How to contribute](CONTRIBUTING.md) page for information on our development workflow.
2725

26+
## Documentation
27+
28+
For detailed documentation, see <https://rules-python.readthedocs.io>
29+
2830
## Bzlmod support
2931

3032
- Status: Beta
3133
- Full Feature Parity: No
3234

3335
See [Bzlmod support](BZLMOD_SUPPORT.md) for more details.
34-
35-
## Getting started
36-
37-
The following two sections cover using `rules_python` with bzlmod and
38-
the older way of configuring bazel with a `WORKSPACE` file.
39-
40-
### Using bzlmod
41-
42-
**IMPORTANT: bzlmod support is still in Beta; APIs are subject to change.**
43-
44-
The first step to using rules_python with bzlmod is to add the dependency to
45-
your MODULE.bazel file:
46-
47-
```starlark
48-
# Update the version "0.0.0" to the release found here:
49-
# https://github.com/bazelbuild/rules_python/releases.
50-
bazel_dep(name = "rules_python", version = "0.0.0")
51-
```
52-
53-
Once added, you can load the rules and use them:
54-
55-
```starlark
56-
load("@rules_python//python:py_binary.bzl", "py_binary")
57-
58-
py_binary(...)
59-
```
60-
61-
Depending on what you're doing, you likely want to do some additional
62-
configuration to control what Python version is used; read the following
63-
sections for how to do that.
64-
65-
#### Toolchain registration with bzlmod
66-
67-
A default toolchain is automatically configured depending on
68-
`rules_python`. Note, however, the version used tracks the most recent Python
69-
release and will change often.
70-
71-
If you want to use a specific Python version for your programs, then how
72-
to do so depends on if you're configuring the root module or not. The root
73-
module is special because it can set the *default* Python version, which
74-
is used by the version-unaware rules (e.g. `//python:py_binary.bzl` et al). For
75-
submodules, it's recommended to use the version-aware rules to pin your programs
76-
to a specific Python version so they don't accidentally run with a different
77-
version configured by the root module.
78-
79-
##### Configuring and using the default Python version
80-
81-
To specify what the default Python version is, set `is_default = True` when
82-
calling `python.toolchain()`. This can only be done by the root module; it is
83-
silently ignored if a submodule does it. Similarly, using the version-unaware
84-
rules (which always use the default Python version) should only be done by the
85-
root module. If submodules use them, then they may run with a different Python
86-
version than they expect.
87-
88-
```starlark
89-
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
90-
91-
python.toolchain(
92-
python_version = "3.11",
93-
is_default = True,
94-
)
95-
```
96-
97-
Then use the base rules from e.g. `//python:py_binary.bzl`.
98-
99-
##### Pinning to a Python version
100-
101-
Pinning to a version allows targets to force that a specific Python version is
102-
used, even if the root module configures a different version as a default. This
103-
is most useful for two cases:
104-
105-
1. For submodules to ensure they run with the appropriate Python version
106-
2. To allow incremental, per-target, upgrading to newer Python versions,
107-
typically in a mono-repo situation.
108-
109-
To configure a submodule with the version-aware rules, request the particular
110-
version you need, then use the `@python_versions` repo to use the rules that
111-
force specific versions:
112-
113-
```starlark
114-
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
115-
116-
python.toolchain(
117-
python_version = "3.11",
118-
)
119-
use_repo(python, "python_versions")
120-
```
121-
122-
Then use e.g. `load("@python_versions//3.11:defs.bzl", "py_binary")` to use
123-
the rules that force that particular version. Multiple versions can be specified
124-
and use within a single build.
125-
126-
For more documentation, see the bzlmod examples under the [examples](examples) folder. Look for the examples that contain a `MODULE.bazel` file.
127-
128-
##### Other toolchain details
129-
130-
The `python.toolchain()` call makes its contents available under a repo named
131-
`python_X_Y`, where X and Y are the major and minor versions. For example,
132-
`python.toolchain(python_version="3.11")` creates the repo `@python_3_11`.
133-
Remember to call `use_repo()` to make repos visible to your module:
134-
`use_repo(python, "python_3_11")`
135-
136-
### Using a WORKSPACE file
137-
138-
To import rules_python in your project, you first need to add it to your
139-
`WORKSPACE` file, using the snippet provided in the
140-
[release you choose](https://github.com/bazelbuild/rules_python/releases)
141-
142-
To depend on a particular unreleased version, you can do the following:
143-
144-
```starlark
145-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
146-
147-
148-
# Update the SHA and VERSION to the lastest version available here:
149-
# https://github.com/bazelbuild/rules_python/releases.
150-
151-
SHA="84aec9e21cc56fbc7f1335035a71c850d1b9b5cc6ff497306f84cced9a769841"
152-
153-
VERSION="0.23.1"
154-
155-
http_archive(
156-
name = "rules_python",
157-
sha256 = SHA,
158-
strip_prefix = "rules_python-{}".format(VERSION),
159-
url = "https://github.com/bazelbuild/rules_python/releases/download/{}/rules_python-{}.tar.gz".format(VERSION,VERSION),
160-
)
161-
162-
load("@rules_python//python:repositories.bzl", "py_repositories")
163-
164-
py_repositories()
165-
```
166-
167-
#### Toolchain registration
168-
169-
To register a hermetic Python toolchain rather than rely on a system-installed interpreter for runtime execution, you can add to the `WORKSPACE` file:
170-
171-
```starlark
172-
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
173-
174-
python_register_toolchains(
175-
name = "python_3_11",
176-
# Available versions are listed in @rules_python//python:versions.bzl.
177-
# We recommend using the same version your team is already standardized on.
178-
python_version = "3.11",
179-
)
180-
181-
load("@python_3_11//:defs.bzl", "interpreter")
182-
183-
load("@rules_python//python:pip.bzl", "pip_parse")
184-
185-
pip_parse(
186-
...
187-
python_interpreter_target = interpreter,
188-
...
189-
)
190-
```
191-
192-
After registration, your Python targets will use the toolchain's interpreter during execution, but a system-installed interpreter
193-
is still used to 'bootstrap' Python targets (see https://github.com/bazelbuild/rules_python/issues/691).
194-
You may also find some quirks while using this toolchain. Please refer to [python-build-standalone documentation's _Quirks_ section](https://python-build-standalone.readthedocs.io/en/latest/quirks.html).
195-
196-
### Toolchain usage in other rules
197-
198-
Python toolchains can be utilized in other bazel rules, such as `genrule()`, by adding the `toolchains=["@rules_python//python:current_py_toolchain"]` attribute. You can obtain the path to the Python interpreter using the `$(PYTHON2)` and `$(PYTHON3)` ["Make" Variables](https://bazel.build/reference/be/make-variables). See the [`test_current_py_toolchain`](tests/load_from_macro/BUILD.bazel) target for an example.
199-
200-
### "Hello World"
201-
202-
Once you've imported the rule set into your `WORKSPACE` using any of these
203-
methods, you can then load the core rules in your `BUILD` files with the following:
204-
205-
```starlark
206-
load("@rules_python//python:defs.bzl", "py_binary")
207-
208-
py_binary(
209-
name = "main",
210-
srcs = ["main.py"],
211-
)
212-
```
213-
214-
## Using dependencies from PyPI
215-
216-
Using PyPI packages (aka "pip install") involves two main steps.
217-
218-
1. [Installing third_party packages](#installing-third_party-packages)
219-
2. [Using third_party packages as dependencies](#using-third_party-packages-as-dependencies)
220-
221-
### Installing third_party packages
222-
223-
#### Using bzlmod
224-
225-
To add pip dependencies to your `MODULE.bazel` file, use the `pip.parse` extension, and call it to create the central external repo and individual wheel external repos. Include in the `MODULE.bazel` the toolchain extension as shown in the first bzlmod example above.
226-
227-
```starlark
228-
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
229-
pip.parse(
230-
hub_name = "my_deps",
231-
python_version = "3.11",
232-
requirements_lock = "//:requirements_lock_3_11.txt",
233-
)
234-
use_repo(pip, "my_deps")
235-
```
236-
For more documentation, including how the rules can update/create a requirements file, see the bzlmod examples under the [examples](examples) folder.
237-
238-
#### Using a WORKSPACE file
239-
240-
To add pip dependencies to your `WORKSPACE`, load the `pip_parse` function and call it to create the central external repo and individual wheel external repos.
241-
242-
```starlark
243-
load("@rules_python//python:pip.bzl", "pip_parse")
244-
245-
# Create a central repo that knows about the dependencies needed from
246-
# requirements_lock.txt.
247-
pip_parse(
248-
name = "my_deps",
249-
requirements_lock = "//path/to:requirements_lock.txt",
250-
)
251-
# Load the starlark macro, which will define your dependencies.
252-
load("@my_deps//:requirements.bzl", "install_deps")
253-
# Call it to define repos for your requirements.
254-
install_deps()
255-
```
256-
257-
#### pip rules
258-
259-
Note that since `pip_parse` is a repository rule and therefore executes pip at WORKSPACE-evaluation time, Bazel has no
260-
information about the Python toolchain and cannot enforce that the interpreter
261-
used to invoke pip matches the interpreter used to run `py_binary` targets. By
262-
default, `pip_parse` uses the system command `"python3"`. To override this, pass in the
263-
`python_interpreter` attribute or `python_interpreter_target` attribute to `pip_parse`.
264-
265-
You can have multiple `pip_parse`s in the same workspace. Or use the pip extension multiple times when using bzlmod.
266-
This configuration will create multiple external repos that have no relation to one another
267-
and may result in downloading the same wheels numerous times.
268-
269-
As with any repository rule, if you would like to ensure that `pip_parse` is
270-
re-executed to pick up a non-hermetic change to your environment (e.g.,
271-
updating your system `python` interpreter), you can force it to re-execute by running
272-
`bazel sync --only [pip_parse name]`.
273-
274-
Note: The `pip_install` rule is deprecated. `pip_parse` offers identical functionality, and both `pip_install` and `pip_parse` now have the same implementation. The name `pip_install` may be removed in a future version of the rules.
275-
276-
The maintainers have made all reasonable efforts to facilitate a smooth transition. Still, some users of `pip_install` will need to replace their existing `requirements.txt` with a fully resolved set of dependencies using a tool such as `pip-tools` or the `compile_pip_requirements` repository rule.
277-
278-
### Using third_party packages as dependencies
279-
280-
Each extracted wheel repo contains a `py_library` target representing
281-
the wheel's contents. There are two ways to access this library. The
282-
first uses the `requirement()` function defined in the central
283-
repo's `//:requirements.bzl` file. This function maps a pip package
284-
name to a label:
285-
286-
```starlark
287-
load("@my_deps//:requirements.bzl", "requirement")
288-
289-
py_library(
290-
name = "mylib",
291-
srcs = ["mylib.py"],
292-
deps = [
293-
":myotherlib",
294-
requirement("some_pip_dep"),
295-
requirement("another_pip_dep"),
296-
]
297-
)
298-
```
299-
300-
The reason `requirement()` exists is that the pattern for the labels,
301-
while not expected to change frequently, is not guaranteed to be
302-
stable. Using `requirement()` ensures you do not have to refactor
303-
your `BUILD` files if the pattern changes.
304-
305-
On the other hand, using `requirement()` has several drawbacks; see
306-
[this issue][requirements-drawbacks] for an enumeration. If you don't
307-
want to use `requirement()`, you can use the library
308-
labels directly instead. For `pip_parse`, the labels are of the following form:
309-
310-
```starlark
311-
@{name}_{package}//:pkg
312-
```
313-
314-
Here `name` is the `name` attribute that was passed to `pip_parse` and
315-
`package` is the pip package name with characters that are illegal in
316-
Bazel label names (e.g. `-`, `.`) replaced with `_`. If you need to
317-
update `name` from "old" to "new", then you can run the following
318-
buildozer command:
319-
320-
```shell
321-
buildozer 'substitute deps @old_([^/]+)//:pkg @new_${1}//:pkg' //...:*
322-
```
323-
324-
For `pip_install`, the labels are instead of the form:
325-
326-
```starlark
327-
@{name}//pypi__{package}
328-
```
329-
330-
[requirements-drawbacks]: https://github.com/bazelbuild/rules_python/issues/414
331-
332-
#### 'Extras' dependencies
333-
334-
Any 'extras' specified in the requirements lock file will be automatically added as transitive dependencies of the package. In the example above, you'd just put `requirement("useful_dep")`.
335-
336-
### Consuming Wheel Dists Directly
337-
338-
If you need to depend on the wheel dists themselves, for instance, to pass them
339-
to some other packaging tool, you can get a handle to them with the `whl_requirement` macro. For example:
340-
341-
```starlark
342-
filegroup(
343-
name = "whl_files",
344-
data = [
345-
whl_requirement("boto3"),
346-
]
347-
)
348-
```
349-
# Python Gazelle plugin
350-
351-
[Gazelle](https://github.com/bazelbuild/bazel-gazelle)
352-
is a build file generator for Bazel projects. It can create new `BUILD.bazel` files for a project that follows language conventions and update existing build files to include new sources, dependencies, and options.
353-
354-
Bazel may run Gazelle using the Gazelle rule, or it may be installed and run as a command line tool.
355-
356-
See the documentation for Gazelle with rules_python [here](gazelle).
357-
358-
## Migrating from the bundled rules
359-
360-
The core rules are currently available in Bazel as built-in symbols, but this
361-
form is deprecated. Instead, you should depend on rules_python in your
362-
`WORKSPACE` file and load the Python rules from
363-
`@rules_python//python:defs.bzl`.
364-
365-
A [buildifier](https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md)
366-
fix is available to automatically migrate `BUILD` and `.bzl` files to add the
367-
appropriate `load()` statements and rewrite uses of `native.py_*`.
368-
369-
```sh
370-
# Also consider using the -r flag to modify an entire workspace.
371-
buildifier --lint=fix --warnings=native-py <files>
372-
```
373-
374-
Currently, the `WORKSPACE` file needs to be updated manually as per [Getting
375-
started](#Getting-started) above.
376-
377-
Note that Starlark-defined bundled symbols underneath
378-
`@bazel_tools//tools/python` are also deprecated. These are not yet rewritten
379-
by buildifier.
380-

0 commit comments

Comments
 (0)