Skip to content

Commit 385c103

Browse files
committed
Adding variable support for distribution in py_wheel
1 parent 28e15c2 commit 385c103

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

docs/packaging.md

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

examples/wheel/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ build_test(
9494
py_wheel(
9595
name = "minimal_with_py_library_with_stamp",
9696
# Package data. We're building "example_minimal_library-0.0.1-py3-none-any.whl"
97-
distribution = "example_minimal_library",
97+
distribution = "example_minimal_library{BUILD_USER}",
9898
python_tag = "py3",
9999
stamp = 1,
100100
version = "0.1.{BUILD_TIMESTAMP}",

examples/wheel/wheel_test.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,30 +374,36 @@ def test_rule_creates_directory_and_is_included_in_wheel(self):
374374
],
375375
)
376376

377-
def test_rule_sets_stamped_version_in_wheel_metadata(self):
377+
def test_rule_expands_workspace_status_keys_in_wheel_metadata(self):
378378
filename = os.path.join(
379379
os.environ["TEST_SRCDIR"],
380380
"rules_python",
381381
"examples",
382382
"wheel",
383-
"example_minimal_library-0.1._BUILD_TIMESTAMP_-py3-none-any.whl",
383+
"example_minimal_library_BUILD_USER_-0.1._BUILD_TIMESTAMP_-py3-none-any.whl",
384384
)
385385

386386
with zipfile.ZipFile(filename) as zf:
387387
metadata_file = None
388388
for f in zf.namelist():
389389
self.assertNotIn("_BUILD_TIMESTAMP_", f)
390+
self.assertNotIn("_BUILD_USER_", f)
390391
if os.path.basename(f) == "METADATA":
391392
metadata_file = f
392393
self.assertIsNotNone(metadata_file)
393394

394395
version = None
396+
name = None
395397
with zf.open(metadata_file) as fp:
396398
for line in fp:
397-
if line.startswith(b'Version:'):
399+
if line.startswith(b"Version:"):
398400
version = line.decode().split()[-1]
401+
if line.startswith(b"Name:"):
402+
name = line.decode().split()[-1]
399403
self.assertIsNotNone(version)
404+
self.assertIsNotNone(name)
400405
self.assertNotIn("{BUILD_TIMESTAMP}", version)
406+
self.assertNotIn("{BUILD_USER}", name)
401407

402408

403409
if __name__ == "__main__":

python/private/py_wheel.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ Name of the distribution.
4040
4141
This should match the project name onm PyPI. It's also the name that is used to
4242
refer to the package in other packages' dependencies.
43+
44+
Workspace status keys are expanded using `{NAME}` format, for example:
45+
- `distribution = "package.{CLASSIFIER}"`
46+
- `distribution = "{DISTRIBUTION}"`
47+
48+
For the available keys, see https://bazel.build/docs/user-manual#workspace-status
4349
""",
4450
),
4551
"platform": attr.string(

tools/wheelmaker.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ def add_wheelfile(self):
167167
wheel_contents += "Tag: %s\n" % tag
168168
self.add_string(self.distinfo_path("WHEEL"), wheel_contents)
169169

170-
def add_metadata(self, metadata, description, version):
170+
def add_metadata(self, metadata, name, description, version):
171171
"""Write METADATA file to the distribution."""
172172
# https://www.python.org/dev/peps/pep-0566/
173173
# https://packaging.python.org/specifications/core-metadata/
174-
metadata += "Version: " + version
175-
metadata += "\n\n"
174+
metadata = re.sub("^Name: .*$", "Name: %s" % name, metadata, flags=re.MULTILINE)
175+
metadata += "Version: %s\n\n" % version
176176
# setuptools seems to insert UNKNOWN as description when none is
177177
# provided.
178178
metadata += description if description else "UNKNOWN"
@@ -207,18 +207,18 @@ def get_files_to_package(input_files):
207207
return files
208208

209209

210-
def resolve_version_stamp(
211-
version: str, volatile_status_stamp: Path, stable_status_stamp: Path
210+
def resolve_argument_stamp(
211+
argument: str, volatile_status_stamp: Path, stable_status_stamp: Path
212212
) -> str:
213-
"""Resolve workspace status stamps format strings found in the version string
213+
"""Resolve workspace status stamps format strings found in the argument string
214214
215215
Args:
216-
version (str): The raw version represenation for the wheel (may include stamp variables)
216+
argument (str): The raw argument represenation for the wheel (may include stamp variables)
217217
volatile_status_stamp (Path): The path to a volatile workspace status file
218218
stable_status_stamp (Path): The path to a stable workspace status file
219219
220220
Returns:
221-
str: A resolved version string
221+
str: A resolved argument string
222222
"""
223223
lines = (
224224
volatile_status_stamp.read_text().splitlines()
@@ -229,9 +229,9 @@ def resolve_version_stamp(
229229
continue
230230
key, value = line.split(" ", maxsplit=1)
231231
stamp = "{" + key + "}"
232-
version = version.replace(stamp, value)
232+
argument = argument.replace(stamp, value)
233233

234-
return version
234+
return argument
235235

236236

237237
def parse_args() -> argparse.Namespace:
@@ -357,7 +357,16 @@ def main() -> None:
357357
strip_prefixes = [p for p in arguments.strip_path_prefix]
358358

359359
if arguments.volatile_status_file and arguments.stable_status_file:
360-
version = resolve_version_stamp(
360+
name = resolve_argument_stamp(
361+
arguments.name,
362+
arguments.volatile_status_file,
363+
arguments.stable_status_file,
364+
)
365+
else:
366+
name = arguments.name
367+
368+
if arguments.volatile_status_file and arguments.stable_status_file:
369+
version = resolve_argument_stamp(
361370
arguments.version,
362371
arguments.volatile_status_file,
363372
arguments.stable_status_file,
@@ -366,7 +375,7 @@ def main() -> None:
366375
version = arguments.version
367376

368377
with WheelMaker(
369-
name=arguments.name,
378+
name=name,
370379
version=version,
371380
build_tag=arguments.build_tag,
372381
python_tag=arguments.python_tag,
@@ -398,7 +407,9 @@ def main() -> None:
398407
with open(arguments.metadata_file, "rt", encoding="utf-8") as metadata_file:
399408
metadata = metadata_file.read()
400409

401-
maker.add_metadata(metadata=metadata, description=description, version=version)
410+
maker.add_metadata(
411+
metadata=metadata, name=name, description=description, version=version
412+
)
402413

403414
if arguments.entry_points_file:
404415
maker.add_file(

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