Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wheel publishing #1015

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion docs/packaging.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 49 additions & 2 deletions python/packaging.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ This also has the advantage that stamping information is included in the wheel's
},
)

def py_wheel(name, **kwargs):
def py_wheel(name, twine = None, **kwargs):
"""Builds a Python Wheel.
Wheels are Python distribution format defined in https://www.python.org/dev/peps/pep-0427/.
Expand Down Expand Up @@ -113,16 +113,63 @@ def py_wheel(name, **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.
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",
...
)
```
Now you can run a command like the following, which publishes to https://test.pypi.org/
```sh
% TWINE_USERNAME=__token__ TWINE_PASSWORD=pypi-*** \\
bazel run --stamp --embed_label=1.2.4 -- \\
//path/to:my_wheel.publish --repository testpypi
```
Args:
name: A unique name for this target.
twine: A label of the external location of the py_library target for twine
**kwargs: other named parameters passed to the underlying [py_wheel rule](#py_wheel_rule)
"""
_dist_target = "{}.dist".format(name)
py_wheel_dist(
name = "{}.dist".format(name),
name = _dist_target,
wheel = name,
out = kwargs.pop("dist_folder", "{}_dist".format(name)),
)

_py_wheel(name = name, **kwargs)

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")

# TODO: use py_binary from //python:defs.bzl after our stardoc setup is less brittle
# buildifier: disable=native-py
native.py_binary(
name = "{}.publish".format(name),
srcs = [twine_main],
args = [
"upload",
"$(rootpath :{})/*".format(_dist_target),
],
data = [_dist_target],
imports = ["."],
main = twine_main,
deps = [twine],
)

py_wheel_rule = _py_wheel
21 changes: 2 additions & 19 deletions python/runfiles/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("//python:defs.bzl", "py_binary", "py_library")
load("//python:defs.bzl", "py_library")
load("//python:packaging.bzl", "py_wheel")

filegroup(
Expand Down Expand Up @@ -45,26 +45,9 @@ py_wheel(
distribution = "bazel_runfiles",
homepage = "https://github.com/bazelbuild/rules_python",
strip_path_prefixes = ["python"],
twine = "@publish_deps_twine//:pkg",
# this can be replaced by building with --stamp --embed_label=1.2.3
version = "{BUILD_EMBED_LABEL}",
visibility = ["//visibility:public"],
deps = [":runfiles"],
)

# TODO(alexeagle): carry forward #1015 to make this part of the py_wheel macro
# Typical command-line to run this:
# TWINE_USERNAME=__token__ TWINE_PASSWORD=pypi-*** \
# bazel run --stamp --embed_label=1.2.4 -- \
# //python/runfiles:wheel.publish --repository testpypi
py_binary(
name = "wheel.publish",
srcs = ["@publish_deps_twine//:rules_python_wheel_entry_point_twine.py"],
args = [
"upload",
"$(rootpath :wheel.dist)/*",
],
data = [":wheel.dist"],
imports = ["."],
main = "@publish_deps_twine//:rules_python_wheel_entry_point_twine.py",
deps = ["@publish_deps_twine//:pkg"],
)