Skip to content

Commit 0051393

Browse files
authored
feat: wheel publishing (bazel-contrib#1015)
feat: add a .publish target to py_wheel macro
1 parent e35cd88 commit 0051393

File tree

3 files changed

+78
-22
lines changed

3 files changed

+78
-22
lines changed

docs/packaging.md

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/packaging.bzl

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ This also has the advantage that stamping information is included in the wheel's
6868
},
6969
)
7070

71-
def py_wheel(name, **kwargs):
71+
def py_wheel(name, twine = None, **kwargs):
7272
"""Builds a Python Wheel.
7373
7474
Wheels are Python distribution format defined in https://www.python.org/dev/peps/pep-0427/.
@@ -113,16 +113,63 @@ def py_wheel(name, **kwargs):
113113
)
114114
```
115115
116+
To publish the wheel to Pypi, the twine package is required.
117+
rules_python doesn't provide twine itself, see https://github.com/bazelbuild/rules_python/issues/1016
118+
However you can install it with pip_parse, just like we do in the WORKSPACE file in rules_python.
119+
120+
Once you've installed twine, you can pass its label to the `twine` attribute of this macro,
121+
to get a "[name].publish" target.
122+
123+
Example:
124+
125+
```python
126+
py_wheel(
127+
name = "my_wheel",
128+
twine = "@publish_deps_twine//:pkg",
129+
...
130+
)
131+
```
132+
133+
Now you can run a command like the following, which publishes to https://test.pypi.org/
134+
135+
```sh
136+
% TWINE_USERNAME=__token__ TWINE_PASSWORD=pypi-*** \\
137+
bazel run --stamp --embed_label=1.2.4 -- \\
138+
//path/to:my_wheel.publish --repository testpypi
139+
```
140+
116141
Args:
117142
name: A unique name for this target.
143+
twine: A label of the external location of the py_library target for twine
118144
**kwargs: other named parameters passed to the underlying [py_wheel rule](#py_wheel_rule)
119145
"""
146+
_dist_target = "{}.dist".format(name)
120147
py_wheel_dist(
121-
name = "{}.dist".format(name),
148+
name = _dist_target,
122149
wheel = name,
123150
out = kwargs.pop("dist_folder", "{}_dist".format(name)),
124151
)
125152

126153
_py_wheel(name = name, **kwargs)
127154

155+
if twine:
156+
if not twine.endswith(":pkg"):
157+
fail("twine label should look like @my_twine_repo//:pkg")
158+
twine_main = twine.replace(":pkg", ":rules_python_wheel_entry_point_twine.py")
159+
160+
# TODO: use py_binary from //python:defs.bzl after our stardoc setup is less brittle
161+
# buildifier: disable=native-py
162+
native.py_binary(
163+
name = "{}.publish".format(name),
164+
srcs = [twine_main],
165+
args = [
166+
"upload",
167+
"$(rootpath :{})/*".format(_dist_target),
168+
],
169+
data = [_dist_target],
170+
imports = ["."],
171+
main = twine_main,
172+
deps = [twine],
173+
)
174+
128175
py_wheel_rule = _py_wheel

python/runfiles/BUILD.bazel

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("//python:defs.bzl", "py_binary", "py_library")
15+
load("//python:defs.bzl", "py_library")
1616
load("//python:packaging.bzl", "py_wheel")
1717

1818
filegroup(
@@ -45,26 +45,9 @@ py_wheel(
4545
distribution = "bazel_runfiles",
4646
homepage = "https://github.com/bazelbuild/rules_python",
4747
strip_path_prefixes = ["python"],
48+
twine = "@publish_deps_twine//:pkg",
4849
# this can be replaced by building with --stamp --embed_label=1.2.3
4950
version = "{BUILD_EMBED_LABEL}",
5051
visibility = ["//visibility:public"],
5152
deps = [":runfiles"],
5253
)
53-
54-
# TODO(alexeagle): carry forward #1015 to make this part of the py_wheel macro
55-
# Typical command-line to run this:
56-
# TWINE_USERNAME=__token__ TWINE_PASSWORD=pypi-*** \
57-
# bazel run --stamp --embed_label=1.2.4 -- \
58-
# //python/runfiles:wheel.publish --repository testpypi
59-
py_binary(
60-
name = "wheel.publish",
61-
srcs = ["@publish_deps_twine//:rules_python_wheel_entry_point_twine.py"],
62-
args = [
63-
"upload",
64-
"$(rootpath :wheel.dist)/*",
65-
],
66-
data = [":wheel.dist"],
67-
imports = ["."],
68-
main = "@publish_deps_twine//:rules_python_wheel_entry_point_twine.py",
69-
deps = ["@publish_deps_twine//:pkg"],
70-
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy