Skip to content

Commit

Permalink
Allow renaming the wheels to a customized version
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuald committed Jan 12, 2024
1 parent cda49dd commit ed68274
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions whl_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "":
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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)

0 comments on commit ed68274

Please sign in to comment.