Skip to content

Commit

Permalink
chore(twine): support 'bzlmod' users out of the box
Browse files Browse the repository at this point in the history
Fixes #1369
  • Loading branch information
aignas committed Nov 20, 2023
1 parent cde1b52 commit 9505116
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ A brief description of the categories of changes:

[0.XX.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.XX.0

### Added

* (py_wheel) `bzlmod` installations now provide a `twine` setup for the default
Python toolchain in `rules_python` for version 3.11.

## [0.27.0] - 2023-11-16

[0.27.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.27.0
Expand Down
9 changes: 9 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ use_repo(python, "pythons_hub")
# This call registers the Python toolchains.
register_toolchains("@pythons_hub//:all")

pip = use_extension("//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "publish_deps",
python_version = "3.11",
requirements_darwin = "//tools/publish:requirements_darwin.txt",
requirements_lock = "//tools/publish:requirements.txt",
requirements_windows = "//tools/publish:requirements_windows.txt",
)

# ===== DEV ONLY SETUP =====
docs_pip = use_extension(
"//python/extensions:pip.bzl",
Expand Down
35 changes: 26 additions & 9 deletions python/packaging.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Public API for for building wheels."""

load("//python:py_binary.bzl", "py_binary")
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
load("//python/private:py_package.bzl", "py_package_lib")
load("//python/private:py_wheel.bzl", _PyWheelInfo = "PyWheelInfo", _py_wheel = "py_wheel")
load("//python/private:util.bzl", "copy_propagating_kwargs")
Expand Down Expand Up @@ -115,19 +116,21 @@ def py_wheel(name, twine = None, publish_args = [], **kwargs):
)
```
To publish the wheel to Pypi, the twine package is required.
rules_python doesn't provide twine itself, see https://github.com/bazelbuild/rules_python/issues/1016
However you can install it with pip_parse, just like we do in the WORKSPACE file in rules_python.
To publish the wheel to PyPI, the twine package is required and it is installed
by default on `bzlmod` setups. On legacy `WORKSPACE`, `rules_python`
doesn't provide `twine` itself
(see https://github.com/bazelbuild/rules_python/issues/1016), but
you can install it with `pip_parse`, just like we do any other dependencies.
Once you've installed twine, you can pass its label to the `twine` attribute of this macro,
to get a "[name].publish" target.
Once you've installed twine, you can pass its label to the `twine`
attribute of this macro, to get a "[name].publish" target.
Example:
```python
py_wheel(
name = "my_wheel",
twine = "@publish_deps_twine//:pkg",
twine = "@publish_deps//twine",
...
)
```
Expand Down Expand Up @@ -158,13 +161,17 @@ def py_wheel(name, twine = None, publish_args = [], **kwargs):

_py_wheel(name = name, **kwargs)

twine_args = []
if twine or BZLMOD_ENABLED:
twine_args = ["upload"]
twine_args.extend(publish_args)
twine_args.append("$(rootpath :{})/*".format(_dist_target))

if twine:
if not twine.endswith(":pkg"):
fail("twine label should look like @my_twine_repo//:pkg")

twine_main = twine.replace(":pkg", ":rules_python_wheel_entry_point_twine.py")
twine_args = ["upload"]
twine_args.extend(publish_args)
twine_args.append("$(rootpath :{})/*".format(_dist_target))

# TODO: use py_binary from //python:defs.bzl after our stardoc setup is less brittle
# buildifier: disable=native-py
Expand All @@ -179,5 +186,15 @@ def py_wheel(name, twine = None, publish_args = [], **kwargs):
visibility = kwargs.get("visibility"),
**copy_propagating_kwargs(kwargs)
)
elif BZLMOD_ENABLED:
native_binary(
name = "{}.publish".format(name),
src = Label("//tools/publish:twine"),
out = "{}.publish",
args = twine_args,
data = [_dist_target],
visibility = kwargs.get("visibility"),
**copy_propagating_kwargs(kwargs)
)

py_wheel_rule = _py_wheel
2 changes: 1 addition & 1 deletion python/runfiles/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ py_wheel(
distribution = "bazel_runfiles",
homepage = "https://github.com/bazelbuild/rules_python",
strip_path_prefixes = ["python"],
twine = "@publish_deps_twine//:pkg",
twine = "//tools/publish:twine",
# this can be replaced by building with --stamp --embed_label=1.2.3
version = "{BUILD_EMBED_LABEL}",
visibility = ["//visibility:public"],
Expand Down
7 changes: 7 additions & 0 deletions tools/publish/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
load("//python:pip.bzl", "compile_pip_requirements")
load("//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")

compile_pip_requirements(
name = "requirements",
src = "requirements.in",
requirements_darwin = "requirements_darwin.txt",
requirements_windows = "requirements_windows.txt",
)

py_console_script_binary(
name = "twine",
pkg = "@publish_deps//twine",
visibility = ["//visibility:public"],
)

0 comments on commit 9505116

Please sign in to comment.