You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.1 KiB

4 years ago
  1. from __future__ import print_function
  2. import os.path
  3. import re
  4. import sys
  5. from wheel.cli import WheelError
  6. from wheel.wheelfile import WheelFile
  7. DIST_INFO_RE = re.compile(r"^(?P<namever>(?P<name>.+?)-(?P<ver>\d.*?))\.dist-info$")
  8. def pack(directory, dest_dir):
  9. """Repack a previously unpacked wheel directory into a new wheel file.
  10. The .dist-info/WHEEL file must contain one or more tags so that the target
  11. wheel file name can be determined.
  12. :param directory: The unpacked wheel directory
  13. :param dest_dir: Destination directory (defaults to the current directory)
  14. """
  15. # Find the .dist-info directory
  16. dist_info_dirs = [fn for fn in os.listdir(directory)
  17. if os.path.isdir(os.path.join(directory, fn)) and DIST_INFO_RE.match(fn)]
  18. if len(dist_info_dirs) > 1:
  19. raise WheelError('Multiple .dist-info directories found in {}'.format(directory))
  20. elif not dist_info_dirs:
  21. raise WheelError('No .dist-info directories found in {}'.format(directory))
  22. # Determine the target wheel filename
  23. dist_info_dir = dist_info_dirs[0]
  24. name_version = DIST_INFO_RE.match(dist_info_dir).group('namever')
  25. # Read the tags from .dist-info/WHEEL
  26. with open(os.path.join(directory, dist_info_dir, 'WHEEL')) as f:
  27. tags = [line.split(' ')[1].rstrip() for line in f if line.startswith('Tag: ')]
  28. if not tags:
  29. raise WheelError('No tags present in {}/WHEEL; cannot determine target wheel filename'
  30. .format(dist_info_dir))
  31. # Reassemble the tags for the wheel file
  32. impls = sorted({tag.split('-')[0] for tag in tags})
  33. abivers = sorted({tag.split('-')[1] for tag in tags})
  34. platforms = sorted({tag.split('-')[2] for tag in tags})
  35. tagline = '-'.join(['.'.join(impls), '.'.join(abivers), '.'.join(platforms)])
  36. # Repack the wheel
  37. wheel_path = os.path.join(dest_dir, '{}-{}.whl'.format(name_version, tagline))
  38. with WheelFile(wheel_path, 'w') as wf:
  39. print("Repacking wheel as {}...".format(wheel_path), end='')
  40. sys.stdout.flush()
  41. wf.write_files(directory)
  42. print('OK')