8000 fix: embed stamped version in py_wheel METADATA by mattoberle · Pull Request #935 · bazel-contrib/rules_python · GitHub
[go: up one dir, main page]

Skip to content

fix: embed stamped version in py_wheel METADATA #935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/pip_parse_vendored/requirements.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Starlark representation of locked requirements.

@generated by rules_python pip_parse repository rule
from //:requirements.txt
from @//:requirements.txt
"""

load("@python39//:defs.bzl", "interpreter")
Expand Down
1 change: 1 addition & 0 deletions examples/wheel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ py_test(
":customized",
":filename_escaping",
":minimal_with_py_library",
":minimal_with_py_library_with_stamp",
":minimal_with_py_package",
":python_abi3_binary_wheel",
":python_requires_in_a_package",
Expand Down
33 changes: 29 additions & 4 deletions examples/wheel/wheel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_customized_wheel(self):
record_contents,
# The entries are guaranteed to be sorted.
b"""\
example_customized-0.0.1.dist-info/METADATA,sha256=TeeEmokHE2NWjkaMcVJuSAq4_AXUoIad2-SLuquRmbg,372
example_customized-0.0.1.dist-info/METADATA,sha256=YUnzQ9gTMXspIBURe90Ct3aL_CCn8fwC3SiZe6MMTs8,372
example_customized-0.0.1.dist-info/NOTICE,sha256=Xpdw-FXET1IRgZ_wTkx1YQfo1-alET0FVf6V1LXO4js,76
example_customized-0.0.1.dist-info/README,sha256=WmOFwZ3Jga1bHG3JiGRsUheb4UbLffUxyTdHczS27-o,40
example_customized-0.0.1.dist-info/RECORD,,
Expand All @@ -124,14 +124,14 @@ def test_customized_wheel(self):
b"""\
Metadata-Version: 2.1
Name: example_customized
Version: 0.0.1
Author: Example Author with non-ascii characters: \xc5\xbc\xc3\xb3\xc5\x82w
Author-email: example@example.com
Home-page: www.example.com
License: Apache 2.0
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Intended Audience :: Developers
Requires-Dist: pytest
Version: 0.0.1

This is a sample description of a wheel.
""")
Expand Down Expand Up @@ -297,8 +297,8 @@ def test_python_requires_wheel(self):
b"""\
Metadata-Version: 2.1
Name: example_python_requires_in_a_package
Version: 0.0.1
Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*
Version: 0.0.1

UNKNOWN
""",
Expand Down Expand Up @@ -332,8 +332,8 @@ def test_python_abi3_binary_wheel(self):
b"""\
Metadata-Version: 2.1
Name: example_python_abi3_binary_wheel
Version: 0.0.1
Requires-Python: >=3.8
Version: 0.0.1

UNKNOWN
""",
Expand Down Expand Up @@ -372,6 +372,31 @@ def test_rule_creates_directory_and_is_included_in_wheel(self):
],
)

def test_rule_sets_stamped_version_in_wheel_metadata(self):
filename = os.path.join(
os.environ["TEST_SRCDIR"],
"rules_python",
"examples",
"wheel",
"example_minimal_library-0.1._BUILD_TIMESTAMP_-py3-none-any.whl",
)

with zipfile.ZipFile(filename) as zf:
metadata_file = None
for f in zf.namelist():
self.assertNotIn("_BUILD_TIMESTAMP_", f)
if os.path.basename(f) == "METADATA":
metadata_file = f
self.assertIsNotNone(metadata_file)

version = None
with zf.open(metadata_file) as fp:
for line in fp:
if line.startswith(b'Version:'):
version = line.decode().split()[-1]
self.assertIsNotNone(version)
self.assertNotIn("{BUILD_TIMESTAMP}", version)


if __name__ == "__main__":
unittest.main()
3 changes: 1 addition & 2 deletions python/packaging.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,11 @@ def _py_wheel_impl(ctx):

args.add("--input_file_list", packageinputfile)

# Note: Description file is not embedded into metadata.txt yet,
# Note: Description file and version are not embedded into metadata.txt yet,
# it will be done later by wheelmaker script.
metadata_file = ctx.actions.declare_file(ctx.attr.name + ".metadata.txt")
metadata_contents = ["Metadata-Version: 2.1"]
metadata_contents.append("Name: %s" % ctx.attr.distribution)
metadata_contents.append("Version: %s" % version)

if ctx.attr.author:
metadata_contents.append("Author: %s" % ctx.attr.author)
Expand Down
7 changes: 4 additions & 3 deletions tools/wheelmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ def add_wheelfile(self):
wheel_contents += "Tag: %s\n" % tag
self.add_string(self.distinfo_path("WHEEL"), wheel_contents)

def add_metadata(self, metadata, description):
def add_metadata(self, metadata, description, version):
"""Write METADATA file to the distribution."""
# https://www.python.org/dev/peps/pep-0566/
# https://packaging.python.org/specifications/core-metadata/
metadata += "\n"
metadata += "Version: " + version
metadata += "\n\n"
# setuptools seems to insert UNKNOWN as description when none is
# provided.
metadata += description if description else "UNKNOWN"
Expand Down Expand Up @@ -398,7 +399,7 @@ def main() -> None:
encoding="utf-8") as metadata_file:
metadata = metadata_file.read()

maker.add_metadata(metadata=metadata, description=description)
maker.add_metadata(metadata=metadata, description=description, version=version)

if arguments.entry_points_file:
maker.add_file(
Expand Down
0