wheel.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. """Wheels support."""
  2. from distutils.util import get_platform
  3. from distutils import log
  4. import email
  5. import itertools
  6. import os
  7. import posixpath
  8. import re
  9. import zipfile
  10. import pkg_resources
  11. import setuptools
  12. from pkg_resources import parse_version
  13. from setuptools.extern.packaging.tags import sys_tags
  14. from setuptools.extern.packaging.utils import canonicalize_name
  15. from setuptools.extern.six import PY3
  16. from setuptools.command.egg_info import write_requirements
  17. __metaclass__ = type
  18. WHEEL_NAME = re.compile(
  19. r"""^(?P<project_name>.+?)-(?P<version>\d.*?)
  20. ((-(?P<build>\d.*?))?-(?P<py_version>.+?)-(?P<abi>.+?)-(?P<platform>.+?)
  21. )\.whl$""",
  22. re.VERBOSE).match
  23. NAMESPACE_PACKAGE_INIT = '''\
  24. try:
  25. __import__('pkg_resources').declare_namespace(__name__)
  26. except ImportError:
  27. __path__ = __import__('pkgutil').extend_path(__path__, __name__)
  28. '''
  29. def unpack(src_dir, dst_dir):
  30. '''Move everything under `src_dir` to `dst_dir`, and delete the former.'''
  31. for dirpath, dirnames, filenames in os.walk(src_dir):
  32. subdir = os.path.relpath(dirpath, src_dir)
  33. for f in filenames:
  34. src = os.path.join(dirpath, f)
  35. dst = os.path.join(dst_dir, subdir, f)
  36. os.renames(src, dst)
  37. for n, d in reversed(list(enumerate(dirnames))):
  38. src = os.path.join(dirpath, d)
  39. dst = os.path.join(dst_dir, subdir, d)
  40. if not os.path.exists(dst):
  41. # Directory does not exist in destination,
  42. # rename it and prune it from os.walk list.
  43. os.renames(src, dst)
  44. del dirnames[n]
  45. # Cleanup.
  46. for dirpath, dirnames, filenames in os.walk(src_dir, topdown=True):
  47. assert not filenames
  48. os.rmdir(dirpath)
  49. class Wheel:
  50. def __init__(self, filename):
  51. match = WHEEL_NAME(os.path.basename(filename))
  52. if match is None:
  53. raise ValueError('invalid wheel name: %r' % filename)
  54. self.filename = filename
  55. for k, v in match.groupdict().items():
  56. setattr(self, k, v)
  57. def tags(self):
  58. '''List tags (py_version, abi, platform) supported by this wheel.'''
  59. return itertools.product(
  60. self.py_version.split('.'),
  61. self.abi.split('.'),
  62. self.platform.split('.'),
  63. )
  64. def is_compatible(self):
  65. '''Is the wheel is compatible with the current platform?'''
  66. supported_tags = set((t.interpreter, t.abi, t.platform) for t in sys_tags())
  67. return next((True for t in self.tags() if t in supported_tags), False)
  68. def egg_name(self):
  69. return pkg_resources.Distribution(
  70. project_name=self.project_name, version=self.version,
  71. platform=(None if self.platform == 'any' else get_platform()),
  72. ).egg_name() + '.egg'
  73. def get_dist_info(self, zf):
  74. # find the correct name of the .dist-info dir in the wheel file
  75. for member in zf.namelist():
  76. dirname = posixpath.dirname(member)
  77. if (dirname.endswith('.dist-info') and
  78. canonicalize_name(dirname).startswith(
  79. canonicalize_name(self.project_name))):
  80. return dirname
  81. raise ValueError("unsupported wheel format. .dist-info not found")
  82. def install_as_egg(self, destination_eggdir):
  83. '''Install wheel as an egg directory.'''
  84. with zipfile.ZipFile(self.filename) as zf:
  85. self._install_as_egg(destination_eggdir, zf)
  86. def _install_as_egg(self, destination_eggdir, zf):
  87. dist_basename = '%s-%s' % (self.project_name, self.version)
  88. dist_info = self.get_dist_info(zf)
  89. dist_data = '%s.data' % dist_basename
  90. egg_info = os.path.join(destination_eggdir, 'EGG-INFO')
  91. self._convert_metadata(zf, destination_eggdir, dist_info, egg_info)
  92. self._move_data_entries(destination_eggdir, dist_data)
  93. self._fix_namespace_packages(egg_info, destination_eggdir)
  94. @staticmethod
  95. def _convert_metadata(zf, destination_eggdir, dist_info, egg_info):
  96. def get_metadata(name):
  97. with zf.open(posixpath.join(dist_info, name)) as fp:
  98. value = fp.read().decode('utf-8') if PY3 else fp.read()
  99. return email.parser.Parser().parsestr(value)
  100. wheel_metadata = get_metadata('WHEEL')
  101. # Check wheel format version is supported.
  102. wheel_version = parse_version(wheel_metadata.get('Wheel-Version'))
  103. wheel_v1 = (
  104. parse_version('1.0') <= wheel_version < parse_version('2.0dev0')
  105. )
  106. if not wheel_v1:
  107. raise ValueError(
  108. 'unsupported wheel format version: %s' % wheel_version)
  109. # Extract to target directory.
  110. os.mkdir(destination_eggdir)
  111. zf.extractall(destination_eggdir)
  112. # Convert metadata.
  113. dist_info = os.path.join(destination_eggdir, dist_info)
  114. dist = pkg_resources.Distribution.from_location(
  115. destination_eggdir, dist_info,
  116. metadata=pkg_resources.PathMetadata(destination_eggdir, dist_info),
  117. )
  118. # Note: Evaluate and strip markers now,
  119. # as it's difficult to convert back from the syntax:
  120. # foobar; "linux" in sys_platform and extra == 'test'
  121. def raw_req(req):
  122. req.marker = None
  123. return str(req)
  124. install_requires = list(sorted(map(raw_req, dist.requires())))
  125. extras_require = {
  126. extra: sorted(
  127. req
  128. for req in map(raw_req, dist.requires((extra,)))
  129. if req not in install_requires
  130. )
  131. for extra in dist.extras
  132. }
  133. os.rename(dist_info, egg_info)
  134. os.rename(
  135. os.path.join(egg_info, 'METADATA'),
  136. os.path.join(egg_info, 'PKG-INFO'),
  137. )
  138. setup_dist = setuptools.Distribution(
  139. attrs=dict(
  140. install_requires=install_requires,
  141. extras_require=extras_require,
  142. ),
  143. )
  144. # Temporarily disable info traces.
  145. log_threshold = log._global_log.threshold
  146. log.set_threshold(log.WARN)
  147. try:
  148. write_requirements(
  149. setup_dist.get_command_obj('egg_info'),
  150. None,
  151. os.path.join(egg_info, 'requires.txt'),
  152. )
  153. finally:
  154. log.set_threshold(log_threshold)
  155. @staticmethod
  156. def _move_data_entries(destination_eggdir, dist_data):
  157. """Move data entries to their correct location."""
  158. dist_data = os.path.join(destination_eggdir, dist_data)
  159. dist_data_scripts = os.path.join(dist_data, 'scripts')
  160. if os.path.exists(dist_data_scripts):
  161. egg_info_scripts = os.path.join(
  162. destination_eggdir, 'EGG-INFO', 'scripts')
  163. os.mkdir(egg_info_scripts)
  164. for entry in os.listdir(dist_data_scripts):
  165. # Remove bytecode, as it's not properly handled
  166. # during easy_install scripts install phase.
  167. if entry.endswith('.pyc'):
  168. os.unlink(os.path.join(dist_data_scripts, entry))
  169. else:
  170. os.rename(
  171. os.path.join(dist_data_scripts, entry),
  172. os.path.join(egg_info_scripts, entry),
  173. )
  174. os.rmdir(dist_data_scripts)
  175. for subdir in filter(os.path.exists, (
  176. os.path.join(dist_data, d)
  177. for d in ('data', 'headers', 'purelib', 'platlib')
  178. )):
  179. unpack(subdir, destination_eggdir)
  180. if os.path.exists(dist_data):
  181. os.rmdir(dist_data)
  182. @staticmethod
  183. def _fix_namespace_packages(egg_info, destination_eggdir):
  184. namespace_packages = os.path.join(
  185. egg_info, 'namespace_packages.txt')
  186. if os.path.exists(namespace_packages):
  187. with open(namespace_packages) as fp:
  188. namespace_packages = fp.read().split()
  189. for mod in namespace_packages:
  190. mod_dir = os.path.join(destination_eggdir, *mod.split('.'))
  191. mod_init = os.path.join(mod_dir, '__init__.py')
  192. if not os.path.exists(mod_dir):
  193. os.mkdir(mod_dir)
  194. if not os.path.exists(mod_init):
  195. with open(mod_init, 'w') as fp:
  196. fp.write(NAMESPACE_PACKAGE_INIT)