Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3886b1a

Browse filesBrowse files
sha1npstradomski
authored andcommitted
added the ability to specify a custom wheel package root (bazel-contrib#200)
added the ability to specify a custom wheel package root
1 parent fdbb17a commit 3886b1a
Copy full SHA for 3886b1a

File tree

4 files changed

+132
-7
lines changed
Filter options

4 files changed

+132
-7
lines changed

‎experimental/examples/wheel/BUILD

Copy file name to clipboardExpand all lines: experimental/examples/wheel/BUILD
+49-1Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,60 @@ py_wheel(
8585
deps = [":example_pkg"],
8686
)
8787

88+
# An example of how to change the wheel package root directory using 'strip_path_prefixes'.
89+
py_wheel(
90+
name = "custom_package_root",
91+
# Package data. We're building "custom_package_root-0.0.1-py3-none-any.whl"
92+
distribution = "example_custom_package_root",
93+
python_tag = "py3",
94+
version = "0.0.1",
95+
deps = [
96+
":example_pkg"
97+
],
98+
strip_path_prefixes = [
99+
"experimental"
100+
]
101+
)
102+
103+
py_wheel(
104+
name = "custom_package_root_multi_prefix",
105+
# Package data. We're building "custom_custom_package_root_multi_prefix-0.0.1-py3-none-any.whl"
106+
distribution = "example_custom_package_root_multi_prefix",
107+
python_tag = "py3",
108+
version = "0.0.1",
109+
deps = [
110+
":example_pkg"
111+
],
112+
strip_path_prefixes = [
113+
"experimental/examples/wheel/lib",
114+
"experimental/examples/wheel"
115+
]
116+
)
117+
118+
py_wheel(
119+
name = "custom_package_root_multi_prefix_reverse_order",
120+
# Package data. We're building "custom_custom_package_root_multi_prefix_reverse_order-0.0.1-py3-none-any.whl"
121+
distribution = "example_custom_package_root_multi_prefix_reverse_order",
122+
python_tag = "py3",
123+
version = "0.0.1",
124+
deps = [
125+
":example_pkg"
126+
],
127+
strip_path_prefixes = [
128+
"experimental/examples/wheel" ,
129+
"experimental/examples/wheel/lib" # this is not effective, because the first prefix takes priority
130+
]
131+
)
132+
88133
py_test(
89134
name = "wheel_test",
90135
srcs = ["wheel_test.py"],
91136
data = [
92137
":customized",
93138
":minimal_with_py_library",
94139
":minimal_with_py_package",
95-
],
140+
":custom_package_root",
141+
":custom_package_root_multi_prefix",
142+
":custom_package_root_multi_prefix_reverse_order"
143+
]
96144
)

‎experimental/examples/wheel/wheel_test.py

Copy file name to clipboardExpand all lines: experimental/examples/wheel/wheel_test.py
+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,57 @@ def test_customized_wheel(self):
102102
This is a sample description of a wheel.
103103
""")
104104

105+
def test_custom_package_root_wheel(self):
106+
filename = os.path.join(os.environ['TEST_SRCDIR'],
107+
'io_bazel_rules_python', 'experimental',
108+
'examples', 'wheel',
109+
'example_custom_package_root-0.0.1-py3-none-any.whl')
110+
111+
with zipfile.ZipFile(filename) as zf:
112+
self.assertEquals(
113+
zf.namelist(),
114+
['examples/wheel/lib/data.txt',
115+
'examples/wheel/lib/module_with_data.py',
116+
'examples/wheel/lib/simple_module.py',
117+
'examples/wheel/main.py',
118+
'example_custom_package_root-0.0.1.dist-info/WHEEL',
119+
'example_custom_package_root-0.0.1.dist-info/METADATA',
120+
'example_custom_package_root-0.0.1.dist-info/RECORD'])
121+
122+
def test_custom_package_root_multi_prefix_wheel(self):
123+
filename = os.path.join(os.environ['TEST_SRCDIR'],
124+
'io_bazel_rules_python', 'experimental',
125+
'examples', 'wheel',
126+
'example_custom_package_root_multi_prefix-0.0.1-py3-none-any.whl')
127+
128+
with zipfile.ZipFile(filename) as zf:
129+
self.assertEquals(
130+
zf.namelist(),
131+
['data.txt',
132+
'module_with_data.py',
133+
'simple_module.py',
134+
'main.py',
135+
'example_custom_package_root_multi_prefix-0.0.1.dist-info/WHEEL',
136+
'example_custom_package_root_multi_prefix-0.0.1.dist-info/METADATA',
137+
'example_custom_package_root_multi_prefix-0.0.1.dist-info/RECORD'])
138+
139+
def test_custom_package_root_multi_prefix_reverse_order_wheel(self):
140+
filename = os.path.join(os.environ['TEST_SRCDIR'],
141+
'io_bazel_rules_python', 'experimental',
142+
'examples', 'wheel',
143+
'example_custom_package_root_multi_prefix_reverse_order-0.0.1-py3-none-any.whl')
144+
145+
with zipfile.ZipFile(filename) as zf:
146+
self.assertEquals(
147+
zf.namelist(),
148+
['lib/data.txt',
149+
'lib/module_with_data.py',
150+
'lib/simple_module.py',
151+
'main.py',
152+
'example_custom_package_root_multi_prefix_reverse_order-0.0.1.dist-info/WHEEL',
153+
'example_custom_package_root_multi_prefix_reverse_order-0.0.1.dist-info/METADATA',
154+
'example_custom_package_root_multi_prefix_reverse_order-0.0.1.dist-info/RECORD'])
155+
105156

106157
if __name__ == '__main__':
107158
unittest.main()

‎experimental/python/wheel.bzl

Copy file name to clipboardExpand all lines: experimental/python/wheel.bzl
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def _py_wheel_impl(ctx):
102102
args.add("--abi", ctx.attr.abi)
103103
args.add("--platform", ctx.attr.platform)
104104
args.add("--out", outfile.path)
105+
args.add_all(ctx.attr.strip_path_prefixes, format_each = "--strip_path_prefix=%s")
105106

106107
args.add_all(inputs_to_package, format_each = "--input_file=%s", map_each = _input_file_to_arg)
107108

@@ -242,6 +243,10 @@ to refer to the package in other packages' dependencies.
242243
"license": attr.string(default = ""),
243244
"classifiers": attr.string_list(),
244245
"description_file": attr.label(allow_single_file = True),
246+
"strip_path_prefixes": attr.string_list(
247+
default = [],
248+
doc = "path prefixes to strip from files added to the generated package",
249+
),
245250
# Requirements
246251
"requires": attr.string_list(
247252
doc = "List of requirements for this package",

‎experimental/rules_python/wheelmaker.py

Copy file name to clipboardExpand all lines: experimental/rules_python/wheelmaker.py
+27-6Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import argparse
1616
import base64
1717
import collections
18-
import csv
1918
import hashlib
20-
import io
2119
import os
2220
import os.path
2321
import sys
@@ -35,14 +33,15 @@ def commonpath(path1, path2):
3533

3634
class WheelMaker(object):
3735
def __init__(self, name, version, build_tag, python_tag, abi, platform,
38-
outfile=None):
36+
outfile=None, strip_path_prefixes=None):
3937
self._name = name
4038
self._version = version
4139
self._build_tag = build_tag
4240
self._python_tag = python_tag
4341
self._abi = abi
4442
self._platform = platform
4543
self._outfile = outfile
44+
self._strip_path_prefixes = strip_path_prefixes if strip_path_prefixes is not None else []
4645

4746
self._zipfile = None
4847
self._record = []
@@ -93,8 +92,17 @@ def add_string(self, filename, contents):
9392

9493
def add_file(self, package_filename, real_filename):
9594
"""Add given file to the distribution."""
96-
# Always use unix path separators.
97-
arcname = package_filename.replace(os.path.sep, '/')
95+
def arcname_from(name):
96+
# Always use unix path separators.
97+
normalized_arcname = name.replace(os.path.sep, '/')
98+
for prefix in self._strip_path_prefixes:
99+
if normalized_arcname.startswith(prefix):
100+
return normalized_arcname[len(prefix):]
101+
102+
return normalized_arcname
103+
104+
arcname = arcname_from(package_filename)
105+
98106
self._zipfile.write(real_filename, arcname=arcname)
99107
# Find the hash and length
100108
hash = hashlib.sha256()
@@ -208,6 +216,15 @@ def main():
208216
output_group.add_argument('--out', type=str, default=None,
209217
help="Override name of ouptut file")
210218

219+
output_group.add_argument('--strip_path_prefix',
220+
type=str,
221+
action="append",
222+
default=[],
223+
help="Path prefix to be stripped from input package files' path. "
224+
"Can be supplied multiple times. "
225+
"Evaluated in order."
226+
)
227+
211228
wheel_group = parser.add_argument_group("Wheel metadata")
212229
wheel_group.add_argument(
213230
'--header', action='append',
@@ -248,13 +265,17 @@ def main():
248265
# Sort the files for reproducible order in the archive.
249266
all_files = sorted(all_files.items())
250267

268+
strip_prefixes = [p for p in arguments.strip_path_prefix]
269+
251270
with WheelMaker(name=arguments.name,
252271
version=arguments.version,
253272
build_tag=arguments.build_tag,
254273
python_tag=arguments.python_tag,
255274
abi=arguments.abi,
256275
platform=arguments.platform,
257-
outfile=arguments.out) as maker:
276+
outfile=arguments.out,
277+
strip_path_prefixes=strip_prefixes
278+
) as maker:
258279
for package_filename, real_filename in all_files:
259280
maker.add_file(package_filename, real_filename)
260281
maker.add_wheelfile()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.