8000 fix: embed stamped version in py_wheel METADATA (#935) · ahans/rules_python@b455267 · GitHub
[go: up one dir, main page]

Skip to content

Commit b455267

Browse files
mattoberlef0rmiga
andauthored
fix: embed stamped version in py_wheel METADATA (bazel-contrib#935)
* fix: add test for stamped Version in METADATA * fix: resolve stamps in wheel Version METADATA * run //:vendor_requirements This seems unrelated to the bazel-contrib#845 issue fix. CI wants `requirements.bzl` to list `@//:requirements.txt` not `//:requirements.txt`? * fix: simplify line breaks Co-authored-by: Matt Oberle <matt.r.oberle@gmail.com> Co-authored-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
1 parent bcd7109 commit b455267

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

examples/wheel/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ py_test(
243243
":customized",
244244
":filename_escaping",
245245
":minimal_with_py_library",
246+
":minimal_with_py_library_with_stamp",
246247
":minimal_with_py_package",
247248
":python_abi3_binary_wheel",
248249
":python_requires_in_a_package",

examples/wheel/wheel_test.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_customized_wheel(self):
9999
record_contents,
100100
# The entries are guaranteed to be sorted.
101101
b"""\
102-
example_customized-0.0.1.dist-info/METADATA,sha256=TeeEmokHE2NWjkaMcVJuSAq4_AXUoIad2-SLuquRmbg,372
102+
example_customized-0.0.1.dist-info/METADATA,sha256=YUnzQ9gTMXspIBURe90Ct3aL_CCn8fwC3SiZe6MMTs8,372
103103
example_customized-0.0.1.dist-info/NOTICE,sha256=Xpdw-FXET1IRgZ_wTkx1YQfo1-alET0FVf6V1LXO4js,76
104104
example_customized-0.0.1.dist-info/README,sha256=WmOFwZ3Jga1bHG3JiGRsUheb4UbLffUxyTdHczS27-o,40
105105
example_customized-0.0.1.dist-info/RECORD,,
@@ -125,14 +125,14 @@ def test_customized_wheel(self):
125125
b"""\
126126
Metadata-Version: 2.1
127127
Name: example_customized
128-
Version: 0.0.1
129128
Author: Example Author with non-ascii characters: \xc5\xbc\xc3\xb3\xc5\x82w
130129
Author-email: example@example.com
131130
Home-page: www.example.com
132131
License: Apache 2.0
133132
Classifier: License :: OSI Approved :: Apache Software License
134133
Classifier: Intended Audience :: Developers
135134
Requires-Dist: pytest
135+
Version: 0.0.1
136136
137137
This is a sample description of a wheel.
138138
""",
@@ -299,8 +299,8 @@ def test_python_requires_wheel(self):
299299
b"""\
300300
Metadata-Version: 2.1
301301
Name: example_python_requires_in_a_package
302-
Version: 0.0.1
303302
Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*
303+
Version: 0.0.1
304304
305305
UNKNOWN
306306
""",
@@ -334,8 +334,8 @@ def test_python_abi3_binary_wheel(self):
334334
b"""\
335335
Metadata-Version: 2.1
336336
Name: example_python_abi3_binary_wheel
337-
Version: 0.0.1
338337
Requires-Python: >=3.8
338+
Version: 0.0.1
339339
340340
UNKNOWN
341341
""",
@@ -374,6 +374,31 @@ 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):
378+
filename = os.path.join(
379+
os.environ["TEST_SRCDIR"],
380+
"rules_python",
381+
"examples",
382+
"wheel",
383+
"example_minimal_library-0.1._BUILD_TIMESTAMP_-py3-none-any.whl",
384+
)
385+
386+
with zipfile.ZipFile(filename) as zf:
387+
metadata_file = None
388+
for f in zf.namelist():
389+
self.assertNotIn("_BUILD_TIMESTAMP_", f)
390+
if os.path.basename(f) == "METADATA":
391+
metadata_file = f
392+
self.assertIsNotNone(metadata_file)
393+
394+
version = None
395+
with zf.open(metadata_file) as fp:
396+
for line in fp:
397+
if line.startswith(b'Version:'):
398+
version = line.decode().split()[-1]
399+
self.assertIsNotNone(version)
400+
self.assertNotIn("{BUILD_TIMESTAMP}", version)
401+
377402

378403
if __name__ == "__main__":
379404
unittest.main()

python/packaging.bzl

Lines changed: 1 addition & 2 deletions
67E6
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,11 @@ def _py_wheel_impl(ctx):
167167

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

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

177176
if ctx.attr.author:
178177
metadata_contents.append("Author: %s" % ctx.attr.author)

tools/wheelmaker.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +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):
170+
def add_metadata(self, metadata, 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 += "\n"
174+
metadata += "Version: " + version
175+
metadata += "\n\n"
175176
# setuptools seems to insert UNKNOWN as description when none is
176177
# provided.
177178
metadata += description if description else "UNKNOWN"
@@ -397,7 +398,7 @@ def main() -> None:
397398
with open(arguments.metadata_file, "rt", encoding="utf-8") as metadata_file:
398399
metadata = metadata_file.read()
399400

400-
maker.add_metadata(metadata=metadata, description=description)
401+
maker.add_metadata(metadata=metadata, description=description, version=version)
401402

402403
if arguments.entry_points_file:
403404
maker.add_file(

0 commit comments

Comments
 (0)
0