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.

90 lines
3.0 KiB

7 years ago
  1. #!/usr/bin/env python
  2. import distutils.dist
  3. import os.path
  4. import re
  5. import shutil
  6. import sys
  7. import tempfile
  8. import zipfile
  9. from argparse import ArgumentParser
  10. from distutils.archive_util import make_archive
  11. from glob import iglob
  12. import wheel.bdist_wheel
  13. from wheel.wininst2wheel import _bdist_wheel_tag
  14. egg_info_re = re.compile(r'''(?P<name>.+?)-(?P<ver>.+?)
  15. (-(?P<pyver>.+?))?(-(?P<arch>.+?))?.egg''', re.VERBOSE)
  16. def egg2wheel(egg_path, dest_dir):
  17. egg_info = egg_info_re.match(os.path.basename(egg_path)).groupdict()
  18. dir = tempfile.mkdtemp(suffix="_e2w")
  19. if os.path.isfile(egg_path):
  20. # assume we have a bdist_egg otherwise
  21. egg = zipfile.ZipFile(egg_path)
  22. egg.extractall(dir)
  23. else:
  24. # support buildout-style installed eggs directories
  25. for pth in os.listdir(egg_path):
  26. src = os.path.join(egg_path, pth)
  27. if os.path.isfile(src):
  28. shutil.copy2(src, dir)
  29. else:
  30. shutil.copytree(src, os.path.join(dir, pth))
  31. dist_info = "%s-%s" % (egg_info['name'], egg_info['ver'])
  32. abi = 'none'
  33. pyver = egg_info['pyver'].replace('.', '')
  34. arch = (egg_info['arch'] or 'any').replace('.', '_').replace('-', '_')
  35. if arch != 'any':
  36. # assume all binary eggs are for CPython
  37. pyver = 'cp' + pyver[2:]
  38. wheel_name = '-'.join((
  39. dist_info,
  40. pyver,
  41. abi,
  42. arch
  43. ))
  44. root_is_purelib = egg_info['arch'] is None
  45. if root_is_purelib:
  46. bw = wheel.bdist_wheel.bdist_wheel(distutils.dist.Distribution())
  47. else:
  48. bw = _bdist_wheel_tag(distutils.dist.Distribution())
  49. bw.root_is_pure = root_is_purelib
  50. bw.python_tag = pyver
  51. bw.plat_name_supplied = True
  52. bw.plat_name = egg_info['arch'] or 'any'
  53. if not root_is_purelib:
  54. bw.full_tag_supplied = True
  55. bw.full_tag = (pyver, abi, arch)
  56. dist_info_dir = os.path.join(dir, '%s.dist-info' % dist_info)
  57. bw.egg2dist(os.path.join(dir, 'EGG-INFO'),
  58. dist_info_dir)
  59. bw.write_wheelfile(dist_info_dir, generator='egg2wheel')
  60. bw.write_record(dir, dist_info_dir)
  61. filename = make_archive(os.path.join(dest_dir, wheel_name), 'zip', root_dir=dir)
  62. os.rename(filename, filename[:-3] + 'whl')
  63. shutil.rmtree(dir)
  64. def main():
  65. parser = ArgumentParser()
  66. parser.add_argument('eggs', nargs='*', help="Eggs to convert")
  67. parser.add_argument('--dest-dir', '-d', default=os.path.curdir,
  68. help="Directory to store wheels (default %(default)s)")
  69. parser.add_argument('--verbose', '-v', action='store_true')
  70. args = parser.parse_args()
  71. for pat in args.eggs:
  72. for egg in iglob(pat):
  73. if args.verbose:
  74. sys.stdout.write("{0}... ".format(egg))
  75. egg2wheel(egg, args.dest_dir)
  76. if args.verbose:
  77. sys.stdout.write("OK\n")
  78. if __name__ == "__main__":
  79. main()

Powered by TurnKey Linux.