From 3f1a73a2b282ed55044541d3c28e0618d8b1de14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Sun, 15 Aug 2021 23:58:47 +0300 Subject: [PATCH] Fixed wheel pack duplicating WHEEL contents on build number change Fixes #415. --- docs/news.rst | 4 ++++ src/wheel/cli/pack.py | 5 ++++- tests/cli/test_pack.py | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/news.rst b/docs/news.rst index 2653b0a5..4fc4f4ca 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -1,6 +1,10 @@ Release Notes ============= +**UNRELEASED** + +- Fixed ``wheel pack`` duplicating the ``WHEEL`` contents when the build number has changed (#415) + **0.37.0 (2021-08-09)** - Added official Python 3.10 support diff --git a/src/wheel/cli/pack.py b/src/wheel/cli/pack.py index 1e77fdbd..9403c51d 100644 --- a/src/wheel/cli/pack.py +++ b/src/wheel/cli/pack.py @@ -57,9 +57,12 @@ def pack(directory, dest_dir, build_number): replacement = ('Build: %s\r\n' % build_number).encode('ascii') if build_number else b'' with open(wheel_file_path, 'rb+') as f: wheel_file_content = f.read() - if not BUILD_NUM_RE.subn(replacement, wheel_file_content)[1]: + wheel_file_content, num_replaced = BUILD_NUM_RE.subn(replacement, + wheel_file_content) + if not num_replaced: wheel_file_content += replacement + f.seek(0) f.truncate() f.write(wheel_file_content) diff --git a/tests/cli/test_pack.py b/tests/cli/test_pack.py index ff36d6f7..3f689a47 100644 --- a/tests/cli/test_pack.py +++ b/tests/cli/test_pack.py @@ -1,4 +1,5 @@ import os +from textwrap import dedent from zipfile import ZipFile import pytest @@ -47,7 +48,15 @@ def test_pack(tmpdir_factory, tmpdir, build_tag_arg, existing_build_tag, filenam assert new_record_lines == old_record_lines expected_build_num = build_tag_arg or existing_build_tag + expected_wheel_content = dedent("""\ + Wheel-Version: 1.0 + Generator: bdist_wheel (0.30.0) + Root-Is-Purelib: false + Tag: py2-none-any + Tag: py3-none-any + """.replace('\n', '\r\n')) if expected_build_num: - assert ('Build: %s\r\n' % expected_build_num).encode() in new_wheel_file_content - else: - assert b'Build: ' not in new_wheel_file_content + expected_wheel_content += 'Build: %s\r\n' % expected_build_num + + expected_wheel_content = expected_wheel_content.encode('ascii') + assert new_wheel_file_content == expected_wheel_content