wheel_builder.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. """Orchestrator for building wheels from InstallRequirements.
  2. """
  3. import logging
  4. import os.path
  5. import re
  6. import shutil
  7. from typing import Any, Callable, Iterable, List, Optional, Tuple
  8. from pip._vendor.packaging.utils import canonicalize_name, canonicalize_version
  9. from pip._vendor.packaging.version import InvalidVersion, Version
  10. from pip._internal.cache import WheelCache
  11. from pip._internal.exceptions import InvalidWheelFilename, UnsupportedWheel
  12. from pip._internal.metadata import FilesystemWheel, get_wheel_distribution
  13. from pip._internal.models.link import Link
  14. from pip._internal.models.wheel import Wheel
  15. from pip._internal.operations.build.wheel import build_wheel_pep517
  16. from pip._internal.operations.build.wheel_editable import build_wheel_editable
  17. from pip._internal.operations.build.wheel_legacy import build_wheel_legacy
  18. from pip._internal.req.req_install import InstallRequirement
  19. from pip._internal.utils.logging import indent_log
  20. from pip._internal.utils.misc import ensure_dir, hash_file, is_wheel_installed
  21. from pip._internal.utils.setuptools_build import make_setuptools_clean_args
  22. from pip._internal.utils.subprocess import call_subprocess
  23. from pip._internal.utils.temp_dir import TempDirectory
  24. from pip._internal.utils.urls import path_to_url
  25. from pip._internal.vcs import vcs
  26. logger = logging.getLogger(__name__)
  27. _egg_info_re = re.compile(r"([a-z0-9_.]+)-([a-z0-9_.!+-]+)", re.IGNORECASE)
  28. BinaryAllowedPredicate = Callable[[InstallRequirement], bool]
  29. BuildResult = Tuple[List[InstallRequirement], List[InstallRequirement]]
  30. def _contains_egg_info(s: str) -> bool:
  31. """Determine whether the string looks like an egg_info.
  32. :param s: The string to parse. E.g. foo-2.1
  33. """
  34. return bool(_egg_info_re.search(s))
  35. def _should_build(
  36. req: InstallRequirement,
  37. need_wheel: bool,
  38. check_binary_allowed: BinaryAllowedPredicate,
  39. ) -> bool:
  40. """Return whether an InstallRequirement should be built into a wheel."""
  41. if req.constraint:
  42. # never build requirements that are merely constraints
  43. return False
  44. if req.is_wheel:
  45. if need_wheel:
  46. logger.info(
  47. "Skipping %s, due to already being wheel.",
  48. req.name,
  49. )
  50. return False
  51. if need_wheel:
  52. # i.e. pip wheel, not pip install
  53. return True
  54. # From this point, this concerns the pip install command only
  55. # (need_wheel=False).
  56. if not req.source_dir:
  57. return False
  58. if req.editable:
  59. # we only build PEP 660 editable requirements
  60. return req.supports_pyproject_editable()
  61. if req.use_pep517:
  62. return True
  63. if not check_binary_allowed(req):
  64. logger.info(
  65. "Skipping wheel build for %s, due to binaries being disabled for it.",
  66. req.name,
  67. )
  68. return False
  69. if not is_wheel_installed():
  70. # we don't build legacy requirements if wheel is not installed
  71. logger.info(
  72. "Using legacy 'setup.py install' for %s, "
  73. "since package 'wheel' is not installed.",
  74. req.name,
  75. )
  76. return False
  77. return True
  78. def should_build_for_wheel_command(
  79. req: InstallRequirement,
  80. ) -> bool:
  81. return _should_build(req, need_wheel=True, check_binary_allowed=_always_true)
  82. def should_build_for_install_command(
  83. req: InstallRequirement,
  84. check_binary_allowed: BinaryAllowedPredicate,
  85. ) -> bool:
  86. return _should_build(
  87. req, need_wheel=False, check_binary_allowed=check_binary_allowed
  88. )
  89. def _should_cache(
  90. req: InstallRequirement,
  91. ) -> Optional[bool]:
  92. """
  93. Return whether a built InstallRequirement can be stored in the persistent
  94. wheel cache, assuming the wheel cache is available, and _should_build()
  95. has determined a wheel needs to be built.
  96. """
  97. if req.editable or not req.source_dir:
  98. # never cache editable requirements
  99. return False
  100. if req.link and req.link.is_vcs:
  101. # VCS checkout. Do not cache
  102. # unless it points to an immutable commit hash.
  103. assert not req.editable
  104. assert req.source_dir
  105. vcs_backend = vcs.get_backend_for_scheme(req.link.scheme)
  106. assert vcs_backend
  107. if vcs_backend.is_immutable_rev_checkout(req.link.url, req.source_dir):
  108. return True
  109. return False
  110. assert req.link
  111. base, ext = req.link.splitext()
  112. if _contains_egg_info(base):
  113. return True
  114. # Otherwise, do not cache.
  115. return False
  116. def _get_cache_dir(
  117. req: InstallRequirement,
  118. wheel_cache: WheelCache,
  119. ) -> str:
  120. """Return the persistent or temporary cache directory where the built
  121. wheel need to be stored.
  122. """
  123. cache_available = bool(wheel_cache.cache_dir)
  124. assert req.link
  125. if cache_available and _should_cache(req):
  126. cache_dir = wheel_cache.get_path_for_link(req.link)
  127. else:
  128. cache_dir = wheel_cache.get_ephem_path_for_link(req.link)
  129. return cache_dir
  130. def _always_true(_: Any) -> bool:
  131. return True
  132. def _verify_one(req: InstallRequirement, wheel_path: str) -> None:
  133. canonical_name = canonicalize_name(req.name or "")
  134. w = Wheel(os.path.basename(wheel_path))
  135. if canonicalize_name(w.name) != canonical_name:
  136. raise InvalidWheelFilename(
  137. "Wheel has unexpected file name: expected {!r}, "
  138. "got {!r}".format(canonical_name, w.name),
  139. )
  140. dist = get_wheel_distribution(FilesystemWheel(wheel_path), canonical_name)
  141. dist_verstr = str(dist.version)
  142. if canonicalize_version(dist_verstr) != canonicalize_version(w.version):
  143. raise InvalidWheelFilename(
  144. "Wheel has unexpected file name: expected {!r}, "
  145. "got {!r}".format(dist_verstr, w.version),
  146. )
  147. metadata_version_value = dist.metadata_version
  148. if metadata_version_value is None:
  149. raise UnsupportedWheel("Missing Metadata-Version")
  150. try:
  151. metadata_version = Version(metadata_version_value)
  152. except InvalidVersion:
  153. msg = f"Invalid Metadata-Version: {metadata_version_value}"
  154. raise UnsupportedWheel(msg)
  155. if metadata_version >= Version("1.2") and not isinstance(dist.version, Version):
  156. raise UnsupportedWheel(
  157. "Metadata 1.2 mandates PEP 440 version, "
  158. "but {!r} is not".format(dist_verstr)
  159. )
  160. def _build_one(
  161. req: InstallRequirement,
  162. output_dir: str,
  163. verify: bool,
  164. build_options: List[str],
  165. global_options: List[str],
  166. editable: bool,
  167. ) -> Optional[str]:
  168. """Build one wheel.
  169. :return: The filename of the built wheel, or None if the build failed.
  170. """
  171. artifact = "editable" if editable else "wheel"
  172. try:
  173. ensure_dir(output_dir)
  174. except OSError as e:
  175. logger.warning(
  176. "Building %s for %s failed: %s",
  177. artifact,
  178. req.name,
  179. e,
  180. )
  181. return None
  182. # Install build deps into temporary directory (PEP 518)
  183. with req.build_env:
  184. wheel_path = _build_one_inside_env(
  185. req, output_dir, build_options, global_options, editable
  186. )
  187. if wheel_path and verify:
  188. try:
  189. _verify_one(req, wheel_path)
  190. except (InvalidWheelFilename, UnsupportedWheel) as e:
  191. logger.warning("Built %s for %s is invalid: %s", artifact, req.name, e)
  192. return None
  193. return wheel_path
  194. def _build_one_inside_env(
  195. req: InstallRequirement,
  196. output_dir: str,
  197. build_options: List[str],
  198. global_options: List[str],
  199. editable: bool,
  200. ) -> Optional[str]:
  201. with TempDirectory(kind="wheel") as temp_dir:
  202. assert req.name
  203. if req.use_pep517:
  204. assert req.metadata_directory
  205. assert req.pep517_backend
  206. if global_options:
  207. logger.warning(
  208. "Ignoring --global-option when building %s using PEP 517", req.name
  209. )
  210. if build_options:
  211. logger.warning(
  212. "Ignoring --build-option when building %s using PEP 517", req.name
  213. )
  214. if editable:
  215. wheel_path = build_wheel_editable(
  216. name=req.name,
  217. backend=req.pep517_backend,
  218. metadata_directory=req.metadata_directory,
  219. tempd=temp_dir.path,
  220. )
  221. else:
  222. wheel_path = build_wheel_pep517(
  223. name=req.name,
  224. backend=req.pep517_backend,
  225. metadata_directory=req.metadata_directory,
  226. tempd=temp_dir.path,
  227. )
  228. else:
  229. wheel_path = build_wheel_legacy(
  230. name=req.name,
  231. setup_py_path=req.setup_py_path,
  232. source_dir=req.unpacked_source_directory,
  233. global_options=global_options,
  234. build_options=build_options,
  235. tempd=temp_dir.path,
  236. )
  237. if wheel_path is not None:
  238. wheel_name = os.path.basename(wheel_path)
  239. dest_path = os.path.join(output_dir, wheel_name)
  240. try:
  241. wheel_hash, length = hash_file(wheel_path)
  242. shutil.move(wheel_path, dest_path)
  243. logger.info(
  244. "Created wheel for %s: filename=%s size=%d sha256=%s",
  245. req.name,
  246. wheel_name,
  247. length,
  248. wheel_hash.hexdigest(),
  249. )
  250. logger.info("Stored in directory: %s", output_dir)
  251. return dest_path
  252. except Exception as e:
  253. logger.warning(
  254. "Building wheel for %s failed: %s",
  255. req.name,
  256. e,
  257. )
  258. # Ignore return, we can't do anything else useful.
  259. if not req.use_pep517:
  260. _clean_one_legacy(req, global_options)
  261. return None
  262. def _clean_one_legacy(req: InstallRequirement, global_options: List[str]) -> bool:
  263. clean_args = make_setuptools_clean_args(
  264. req.setup_py_path,
  265. global_options=global_options,
  266. )
  267. logger.info("Running setup.py clean for %s", req.name)
  268. try:
  269. call_subprocess(
  270. clean_args, command_desc="python setup.py clean", cwd=req.source_dir
  271. )
  272. return True
  273. except Exception:
  274. logger.error("Failed cleaning build dir for %s", req.name)
  275. return False
  276. def build(
  277. requirements: Iterable[InstallRequirement],
  278. wheel_cache: WheelCache,
  279. verify: bool,
  280. build_options: List[str],
  281. global_options: List[str],
  282. ) -> BuildResult:
  283. """Build wheels.
  284. :return: The list of InstallRequirement that succeeded to build and
  285. the list of InstallRequirement that failed to build.
  286. """
  287. if not requirements:
  288. return [], []
  289. # Build the wheels.
  290. logger.info(
  291. "Building wheels for collected packages: %s",
  292. ", ".join(req.name for req in requirements), # type: ignore
  293. )
  294. with indent_log():
  295. build_successes, build_failures = [], []
  296. for req in requirements:
  297. assert req.name
  298. cache_dir = _get_cache_dir(req, wheel_cache)
  299. wheel_file = _build_one(
  300. req,
  301. cache_dir,
  302. verify,
  303. build_options,
  304. global_options,
  305. req.editable and req.permit_editable_wheels,
  306. )
  307. if wheel_file:
  308. # Update the link for this.
  309. req.link = Link(path_to_url(wheel_file))
  310. req.local_file_path = req.link.file_path
  311. assert req.link.is_wheel
  312. build_successes.append(req)
  313. else:
  314. build_failures.append(req)
  315. # notify success/failure
  316. if build_successes:
  317. logger.info(
  318. "Successfully built %s",
  319. " ".join([req.name for req in build_successes]), # type: ignore
  320. )
  321. if build_failures:
  322. logger.info(
  323. "Failed to build %s",
  324. " ".join([req.name for req in build_failures]), # type: ignore
  325. )
  326. # Return a list of requirements that failed to build
  327. return build_successes, build_failures