@@ -31,6 +31,43 @@ This rule is intended to be used as data dependency to py_wheel rule.
31
31
attrs = py_package_lib .attrs ,
32
32
)
33
33
34
+ # Based on https://github.com/aspect-build/bazel-lib/tree/main/lib/private/copy_to_directory.bzl
35
+ # Avoiding a bazelbuild -> aspect-build dependency :(
36
+ def _py_wheel_dist_impl (ctx ):
37
+ dir = ctx .actions .declare_directory (ctx .attr .out )
38
+ name_file = ctx .attr .wheel [PyWheelInfo ].name_file
39
+ cmds = [
40
+ "mkdir -p \" %s\" " % dir .path ,
41
+ """cp "{}" "{}/$(cat "{}")" """ .format (ctx .files .wheel [0 ].path , dir .path , name_file .path ),
42
+ ]
43
+ ctx .actions .run_shell (
44
+ inputs = ctx .files .wheel + [name_file ],
45
+ outputs = [dir ],
46
+ command = "\n " .join (cmds ),
47
+ mnemonic = "CopyToDirectory" ,
48
+ progress_message = "Copying files to directory" ,
49
+ use_default_shell_env = True ,
50
+ )
51
+ return [
52
+ DefaultInfo (files = depset ([dir ])),
53
+ ]
54
+
55
+ py_wheel_dist = rule (
56
+ doc = """\
57
+ Prepare a dist/ folder, following Python's packaging standard practice.
58
+
59
+ See https://packaging.python.org/en/latest/tutorials/packaging-projects/#generating-distribution-archives
60
+ which recommends a dist/ folder containing the wheel file(s), source distributions, etc.
61
+
62
+ This also has the advantage that stamping information is included in the wheel's filename.
63
+ """ ,
64
+ implementation = _py_wheel_dist_impl ,
65
+ attrs = {
66
+ "out" : attr .string (doc = "name of the resulting directory" , mandatory = True ),
67
+ "wheel" : attr .label (doc = "a [py_wheel rule](/docs/packaging.md#py_wheel_rule)" , providers = [PyWheelInfo ]),
68
+ },
69
+ )
70
+
34
71
def py_wheel (name , ** kwargs ):
35
72
"""Builds a Python Wheel.
36
73
@@ -80,15 +117,12 @@ def py_wheel(name, **kwargs):
80
117
name: A unique name for this target.
81
118
**kwargs: other named parameters passed to the underlying [py_wheel rule](#py_wheel_rule)
82
119
"""
83
- _py_wheel (name = name , ** kwargs )
120
+ py_wheel_dist (
121
+ name = "{}.dist" .format (name ),
122
+ wheel = name ,
123
+ out = kwargs .pop ("dist_folder" , "{}_dist" .format (name )),
124
+ )
84
125
85
- # TODO(alexeagle): produce an executable target like this:
86
- # py_publish_wheel(
87
- # name = "{}.publish".format(name),
88
- # wheel = name,
89
- # # Optional: override the label for a py_binary that runs twine
90
- # # https://twine.readthedocs.io/en/stable/
91
- # twine_bin = "//path/to:twine",
92
- # )
126
+ _py_wheel (name = name , ** kwargs )
93
127
94
128
py_wheel_rule = _py_wheel
0 commit comments