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.
 
 
 
 

59 line
2.2 KiB

  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, build_number):
  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. # Add the build number if specific
  26. if build_number:
  27. name_version += '-' + build_number
  28. # Read the tags from .dist-info/WHEEL
  29. with open(os.path.join(directory, dist_info_dir, 'WHEEL')) as f:
  30. tags = [line.split(' ')[1].rstrip() for line in f if line.startswith('Tag: ')]
  31. if not tags:
  32. raise WheelError('No tags present in {}/WHEEL; cannot determine target wheel filename'
  33. .format(dist_info_dir))
  34. # Reassemble the tags for the wheel file
  35. impls = sorted({tag.split('-')[0] for tag in tags})
  36. abivers = sorted({tag.split('-')[1] for tag in tags})
  37. platforms = sorted({tag.split('-')[2] for tag in tags})
  38. tagline = '-'.join(['.'.join(impls), '.'.join(abivers), '.'.join(platforms)])
  39. # Repack the wheel
  40. wheel_path = os.path.join(dest_dir, '{}-{}.whl'.format(name_version, tagline))
  41. with WheelFile(wheel_path, 'w') as wf:
  42. print("Repacking wheel as {}...".format(wheel_path), end='')
  43. sys.stdout.flush()
  44. wf.write_files(directory)
  45. print('OK')