diff --git a/examples/pip_parse_vendored/requirements.bzl b/examples/pip_parse_vendored/requirements.bzl index 5a0dcf85f5..4febc756fa 100644 --- a/examples/pip_parse_vendored/requirements.bzl +++ b/examples/pip_parse_vendored/requirements.bzl @@ -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") diff --git a/examples/wheel/BUILD b/examples/wheel/BUILD index 2c572761cf..c3dec29c01 100644 --- a/examples/wheel/BUILD +++ b/examples/wheel/BUILD @@ -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", diff --git a/examples/wheel/wheel_test.py b/examples/wheel/wheel_test.py index cbca0927c3..e05de46341 100644 --- a/examples/wheel/wheel_test.py +++ b/examples/wheel/wheel_test.py @@ -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,, @@ -124,7 +124,6 @@ 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 @@ -132,6 +131,7 @@ def test_customized_wheel(self): 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. """) @@ -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 """, @@ -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 """, @@ -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() diff --git a/python/packaging.bzl b/python/packaging.bzl index 6d7a901f53..5bb50173cf 100644 --- a/python/packaging.bzl +++ b/python/packaging.bzl @@ -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) diff --git a/tools/wheelmaker.py b/tools/wheelmaker.py index d5179001a6..8fea7a43e3 100644 --- a/tools/wheelmaker.py +++ b/tools/wheelmaker.py @@ -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" @@ -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(