diff --git a/CHANGELOG.md b/CHANGELOG.md index 32ab939c5d..4216848092 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/MODULE.bazel b/MODULE.bazel index 7547f61b0b..d7a5761e73 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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", diff --git a/python/packaging.bzl b/python/packaging.bzl index 48423e307f..cf46495c44 100644 --- a/python/packaging.bzl +++ b/python/packaging.bzl @@ -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") @@ -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", ... ) ``` @@ -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 @@ -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 diff --git a/python/runfiles/BUILD.bazel b/python/runfiles/BUILD.bazel index c6cfc2fa94..15dce19067 100644 --- a/python/runfiles/BUILD.bazel +++ b/python/runfiles/BUILD.bazel @@ -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"], diff --git a/tools/publish/BUILD.bazel b/tools/publish/BUILD.bazel index 4759a31257..4b5a930492 100644 --- a/tools/publish/BUILD.bazel +++ b/tools/publish/BUILD.bazel @@ -1,4 +1,5 @@ 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", @@ -6,3 +7,9 @@ compile_pip_requirements( requirements_darwin = "requirements_darwin.txt", requirements_windows = "requirements_windows.txt", ) + +py_console_script_binary( + name = "twine", + pkg = "@publish_deps//twine", + visibility = ["//visibility:public"], +)