25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

237 lines
8.4 KiB

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