@@ -68,7 +68,7 @@ This also has the advantage that stamping information is included in the wheel's
68
68
},
69
69
)
70
70
71
- def py_wheel (name , ** kwargs ):
71
+ def py_wheel (name , twine = None , ** kwargs ):
72
72
"""Builds a Python Wheel.
73
73
74
74
Wheels are Python distribution format defined in https://www.python.org/dev/peps/pep-0427/.
@@ -113,16 +113,63 @@ def py_wheel(name, **kwargs):
113
113
)
114
114
```
115
115
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
+
116
141
Args:
117
142
name: A unique name for this target.
143
+ twine: A label of the external location of the py_library target for twine
118
144
**kwargs: other named parameters passed to the underlying [py_wheel rule](#py_wheel_rule)
119
145
"""
146
+ _dist_target = "{}.dist" .format (name )
120
147
py_wheel_dist (
121
- name = "{}.dist" . format ( name ) ,
148
+ name = _dist_target ,
122
149
wheel = name ,
123
150
out = kwargs .pop ("dist_folder" , "{}_dist" .format (name )),
124
151
)
125
152
126
153
_py_wheel (name = name , ** kwargs )
127
154
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
+
128
175
py_wheel_rule = _py_wheel
0 commit comments