pack.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import annotations
  2. import email.policy
  3. import os.path
  4. import re
  5. from email.generator import BytesGenerator
  6. from email.parser import BytesParser
  7. from wheel.cli import WheelError
  8. from wheel.wheelfile import WheelFile
  9. DIST_INFO_RE = re.compile(r"^(?P<namever>(?P<name>.+?)-(?P<ver>\d.*?))\.dist-info$")
  10. def pack(directory: str, dest_dir: str, build_number: str | None) -> None:
  11. """Repack a previously unpacked wheel directory into a new wheel file.
  12. The .dist-info/WHEEL file must contain one or more tags so that the target
  13. wheel file name can be determined.
  14. :param directory: The unpacked wheel directory
  15. :param dest_dir: Destination directory (defaults to the current directory)
  16. """
  17. # Find the .dist-info directory
  18. dist_info_dirs = [
  19. fn
  20. for fn in os.listdir(directory)
  21. if os.path.isdir(os.path.join(directory, fn)) and DIST_INFO_RE.match(fn)
  22. ]
  23. if len(dist_info_dirs) > 1:
  24. raise WheelError(f"Multiple .dist-info directories found in {directory}")
  25. elif not dist_info_dirs:
  26. raise WheelError(f"No .dist-info directories found in {directory}")
  27. # Determine the target wheel filename
  28. dist_info_dir = dist_info_dirs[0]
  29. name_version = DIST_INFO_RE.match(dist_info_dir).group("namever")
  30. # Read the tags and the existing build number from .dist-info/WHEEL
  31. wheel_file_path = os.path.join(directory, dist_info_dir, "WHEEL")
  32. with open(wheel_file_path, "rb") as f:
  33. info = BytesParser(policy=email.policy.compat32).parse(f)
  34. tags: list[str] = info.get_all("Tag", [])
  35. existing_build_number = info.get("Build")
  36. if not tags:
  37. raise WheelError(
  38. f"No tags present in {dist_info_dir}/WHEEL; cannot determine target "
  39. f"wheel filename"
  40. )
  41. # Set the wheel file name and add/replace/remove the Build tag in .dist-info/WHEEL
  42. build_number = build_number if build_number is not None else existing_build_number
  43. if build_number is not None:
  44. del info["Build"]
  45. if build_number:
  46. info["Build"] = build_number
  47. name_version += "-" + build_number
  48. if build_number != existing_build_number:
  49. with open(wheel_file_path, "wb") as f:
  50. BytesGenerator(f, maxheaderlen=0).flatten(info)
  51. # Reassemble the tags for the wheel file
  52. tagline = compute_tagline(tags)
  53. # Repack the wheel
  54. wheel_path = os.path.join(dest_dir, f"{name_version}-{tagline}.whl")
  55. with WheelFile(wheel_path, "w") as wf:
  56. print(f"Repacking wheel as {wheel_path}...", end="", flush=True)
  57. wf.write_files(directory)
  58. print("OK")
  59. def compute_tagline(tags: list[str]) -> str:
  60. """Compute a tagline from a list of tags.
  61. :param tags: A list of tags
  62. :return: A tagline
  63. """
  64. impls = sorted({tag.split("-")[0] for tag in tags})
  65. abivers = sorted({tag.split("-")[1] for tag in tags})
  66. platforms = sorted({tag.split("-")[2] for tag in tags})
  67. return "-".join([".".join(impls), ".".join(abivers), ".".join(platforms)])