From ed68274e8a1f01d0b204d5aa0ebccc1c9892a088 Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Fri, 12 Jan 2024 18:13:37 -0500 Subject: [PATCH] Allow renaming the wheels to a customized version --- whl_mod.py | 53 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/whl_mod.py b/whl_mod.py index 799636c..8f161cc 100755 --- a/whl_mod.py +++ b/whl_mod.py @@ -23,7 +23,11 @@ def get_strip_exe(): def add_requirements_to_wheel( - wheel: str, project: str, version: str, reqs: typing.List[str] + wheel: str, + project: str, + version: str, + out_version: str, + reqs: typing.List[str], ): whldir = os.path.dirname(wheel) if whldir == "": @@ -39,20 +43,28 @@ def add_requirements_to_wheel( unpacked_root = os.path.join(tmpdir, f"{project}-{version}") # Find and modify the metadata record - metadata_path = os.path.join( - unpacked_root, f"{project}-{version}.dist-info", "METADATA" - ) + dist_info_path = os.path.join(unpacked_root, f"{project}-{version}.dist-info") + metadata_path = os.path.join(dist_info_path, "METADATA") + with open(metadata_path, "r+") as fp: lines = fp.readlines() - # Find the first empty line and insert our extra requirements there - i = 0 - for i, line in enumerate(lines): - if line.strip() == "": - break + # Insert additional requirements? + if reqs: + i = 0 + for i, line in enumerate(lines): + if line.strip() == "": + break + + for req in reversed(reqs): + lines.insert(i, f"Requires-Dist: {req}\n") - for req in reversed(reqs): - lines.insert(i, f"Requires-Dist: {req}\n") + # If we're changing the version, do that too + if version != out_version: + for i in range(len(lines)): + if lines[i].startswith("Version:"): + lines[i] = f"Version: {out_version}\n" + break print("-" * 72) for line in lines: @@ -72,6 +84,17 @@ def add_requirements_to_wheel( print("+", *args) subprocess.check_call(args) + # If we are changing the version, then delete RECORD and rename dist-info + if version != out_version: + os.unlink(os.path.join(dist_info_path, "RECORD")) + new_dist_info_path = os.path.join( + unpacked_root, f"{project}-{out_version}.dist-info" + ) + os.rename(dist_info_path, new_dist_info_path) + + # Everything worked, delete the old wheel + os.unlink(wheel) + # pack the wheel back up args = [sys.executable, "-m", "wheel", "pack", unpacked_root, "-d", whldir] subprocess.check_call(args) @@ -95,6 +118,10 @@ def add_requirements_to_wheel( except KeyError: parser.error(f"{project} not found in {args.config}") + out_version = pkgdata.get("mod_version", None) + if out_version is None: + out_version = version + reqs = pkgdata.get("install_requirements") - if reqs: - add_requirements_to_wheel(args.wheel, project, version, reqs) + if reqs or out_version != version: + add_requirements_to_wheel(args.wheel, project, version, out_version, reqs)