pack.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. BUILD_NUM_RE = re.compile(br'Build: (\d\w*)$')
  9. def pack(directory, dest_dir, build_number):
  10. """Repack a previously unpacked wheel directory into a new wheel file.
  11. The .dist-info/WHEEL file must contain one or more tags so that the target
  12. wheel file name can be determined.
  13. :param directory: The unpacked wheel directory
  14. :param dest_dir: Destination directory (defaults to the current directory)
  15. """
  16. # Find the .dist-info directory
  17. dist_info_dirs = [fn for fn in os.listdir(directory)
  18. if os.path.isdir(os.path.join(directory, fn)) and DIST_INFO_RE.match(fn)]
  19. if len(dist_info_dirs) > 1:
  20. raise WheelError('Multiple .dist-info directories found in {}'.format(directory))
  21. elif not dist_info_dirs:
  22. raise WheelError('No .dist-info directories found in {}'.format(directory))
  23. # Determine the target wheel filename
  24. dist_info_dir = dist_info_dirs[0]
  25. name_version = DIST_INFO_RE.match(dist_info_dir).group('namever')
  26. # Read the tags and the existing build number from .dist-info/WHEEL
  27. existing_build_number = None
  28. wheel_file_path = os.path.join(directory, dist_info_dir, 'WHEEL')
  29. with open(wheel_file_path) as f:
  30. tags = []
  31. for line in f:
  32. if line.startswith('Tag: '):
  33. tags.append(line.split(' ')[1].rstrip())
  34. elif line.startswith('Build: '):
  35. existing_build_number = line.split(' ')[1].rstrip()
  36. if not tags:
  37. raise WheelError('No tags present in {}/WHEEL; cannot determine target wheel filename'
  38. .format(dist_info_dir))
  39. # Set the wheel file name and add/replace/remove the Build tag in .dist-info/WHEEL
  40. build_number = build_number if build_number is not None else existing_build_number
  41. if build_number is not None:
  42. if build_number:
  43. name_version += '-' + build_number
  44. if build_number != existing_build_number:
  45. replacement = ('Build: %s\r\n' % build_number).encode('ascii') if build_number else b''
  46. with open(wheel_file_path, 'rb+') as f:
  47. wheel_file_content = f.read()
  48. wheel_file_content, num_replaced = BUILD_NUM_RE.subn(replacement,
  49. wheel_file_content)
  50. if not num_replaced:
  51. wheel_file_content += replacement
  52. f.seek(0)
  53. f.truncate()
  54. f.write(wheel_file_content)
  55. # Reassemble the tags for the wheel file
  56. impls = sorted({tag.split('-')[0] for tag in tags})
  57. abivers = sorted({tag.split('-')[1] for tag in tags})
  58. platforms = sorted({tag.split('-')[2] for tag in tags})
  59. tagline = '-'.join(['.'.join(impls), '.'.join(abivers), '.'.join(platforms)])
  60. # Repack the wheel
  61. wheel_path = os.path.join(dest_dir, '{}-{}.whl'.format(name_version, tagline))
  62. with WheelFile(wheel_path, 'w') as wf:
  63. print("Repacking wheel as {}...".format(wheel_path), end='')
  64. sys.stdout.flush()
  65. wf.write_files(directory)
  66. print('OK')