req_install.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. import functools
  2. import logging
  3. import os
  4. import shutil
  5. import sys
  6. import uuid
  7. import zipfile
  8. from optparse import Values
  9. from pathlib import Path
  10. from typing import Any, Collection, Dict, Iterable, List, Optional, Sequence, Union
  11. from pip._vendor.packaging.markers import Marker
  12. from pip._vendor.packaging.requirements import Requirement
  13. from pip._vendor.packaging.specifiers import SpecifierSet
  14. from pip._vendor.packaging.utils import canonicalize_name
  15. from pip._vendor.packaging.version import Version
  16. from pip._vendor.packaging.version import parse as parse_version
  17. from pip._vendor.pyproject_hooks import BuildBackendHookCaller
  18. from pip._internal.build_env import BuildEnvironment, NoOpBuildEnvironment
  19. from pip._internal.exceptions import InstallationError, PreviousBuildDirError
  20. from pip._internal.locations import get_scheme
  21. from pip._internal.metadata import (
  22. BaseDistribution,
  23. get_default_environment,
  24. get_directory_distribution,
  25. get_wheel_distribution,
  26. )
  27. from pip._internal.metadata.base import FilesystemWheel
  28. from pip._internal.models.direct_url import DirectUrl
  29. from pip._internal.models.link import Link
  30. from pip._internal.operations.build.metadata import generate_metadata
  31. from pip._internal.operations.build.metadata_editable import generate_editable_metadata
  32. from pip._internal.operations.build.metadata_legacy import (
  33. generate_metadata as generate_metadata_legacy,
  34. )
  35. from pip._internal.operations.install.editable_legacy import (
  36. install_editable as install_editable_legacy,
  37. )
  38. from pip._internal.operations.install.wheel import install_wheel
  39. from pip._internal.pyproject import load_pyproject_toml, make_pyproject_path
  40. from pip._internal.req.req_uninstall import UninstallPathSet
  41. from pip._internal.utils.deprecation import deprecated
  42. from pip._internal.utils.hashes import Hashes
  43. from pip._internal.utils.misc import (
  44. ConfiguredBuildBackendHookCaller,
  45. ask_path_exists,
  46. backup_dir,
  47. display_path,
  48. hide_url,
  49. is_installable_dir,
  50. redact_auth_from_requirement,
  51. redact_auth_from_url,
  52. )
  53. from pip._internal.utils.packaging import get_requirement
  54. from pip._internal.utils.subprocess import runner_with_spinner_message
  55. from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
  56. from pip._internal.utils.unpacking import unpack_file
  57. from pip._internal.utils.virtualenv import running_under_virtualenv
  58. from pip._internal.vcs import vcs
  59. logger = logging.getLogger(__name__)
  60. class InstallRequirement:
  61. """
  62. Represents something that may be installed later on, may have information
  63. about where to fetch the relevant requirement and also contains logic for
  64. installing the said requirement.
  65. """
  66. def __init__(
  67. self,
  68. req: Optional[Requirement],
  69. comes_from: Optional[Union[str, "InstallRequirement"]],
  70. editable: bool = False,
  71. link: Optional[Link] = None,
  72. markers: Optional[Marker] = None,
  73. use_pep517: Optional[bool] = None,
  74. isolated: bool = False,
  75. *,
  76. global_options: Optional[List[str]] = None,
  77. hash_options: Optional[Dict[str, List[str]]] = None,
  78. config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
  79. constraint: bool = False,
  80. extras: Collection[str] = (),
  81. user_supplied: bool = False,
  82. permit_editable_wheels: bool = False,
  83. ) -> None:
  84. assert req is None or isinstance(req, Requirement), req
  85. self.req = req
  86. self.comes_from = comes_from
  87. self.constraint = constraint
  88. self.editable = editable
  89. self.permit_editable_wheels = permit_editable_wheels
  90. # source_dir is the local directory where the linked requirement is
  91. # located, or unpacked. In case unpacking is needed, creating and
  92. # populating source_dir is done by the RequirementPreparer. Note this
  93. # is not necessarily the directory where pyproject.toml or setup.py is
  94. # located - that one is obtained via unpacked_source_directory.
  95. self.source_dir: Optional[str] = None
  96. if self.editable:
  97. assert link
  98. if link.is_file:
  99. self.source_dir = os.path.normpath(os.path.abspath(link.file_path))
  100. # original_link is the direct URL that was provided by the user for the
  101. # requirement, either directly or via a constraints file.
  102. if link is None and req and req.url:
  103. # PEP 508 URL requirement
  104. link = Link(req.url)
  105. self.link = self.original_link = link
  106. # When this InstallRequirement is a wheel obtained from the cache of locally
  107. # built wheels, this is the source link corresponding to the cache entry, which
  108. # was used to download and build the cached wheel.
  109. self.cached_wheel_source_link: Optional[Link] = None
  110. # Information about the location of the artifact that was downloaded . This
  111. # property is guaranteed to be set in resolver results.
  112. self.download_info: Optional[DirectUrl] = None
  113. # Path to any downloaded or already-existing package.
  114. self.local_file_path: Optional[str] = None
  115. if self.link and self.link.is_file:
  116. self.local_file_path = self.link.file_path
  117. if extras:
  118. self.extras = extras
  119. elif req:
  120. self.extras = req.extras
  121. else:
  122. self.extras = set()
  123. if markers is None and req:
  124. markers = req.marker
  125. self.markers = markers
  126. # This holds the Distribution object if this requirement is already installed.
  127. self.satisfied_by: Optional[BaseDistribution] = None
  128. # Whether the installation process should try to uninstall an existing
  129. # distribution before installing this requirement.
  130. self.should_reinstall = False
  131. # Temporary build location
  132. self._temp_build_dir: Optional[TempDirectory] = None
  133. # Set to True after successful installation
  134. self.install_succeeded: Optional[bool] = None
  135. # Supplied options
  136. self.global_options = global_options if global_options else []
  137. self.hash_options = hash_options if hash_options else {}
  138. self.config_settings = config_settings
  139. # Set to True after successful preparation of this requirement
  140. self.prepared = False
  141. # User supplied requirement are explicitly requested for installation
  142. # by the user via CLI arguments or requirements files, as opposed to,
  143. # e.g. dependencies, extras or constraints.
  144. self.user_supplied = user_supplied
  145. self.isolated = isolated
  146. self.build_env: BuildEnvironment = NoOpBuildEnvironment()
  147. # For PEP 517, the directory where we request the project metadata
  148. # gets stored. We need this to pass to build_wheel, so the backend
  149. # can ensure that the wheel matches the metadata (see the PEP for
  150. # details).
  151. self.metadata_directory: Optional[str] = None
  152. # The static build requirements (from pyproject.toml)
  153. self.pyproject_requires: Optional[List[str]] = None
  154. # Build requirements that we will check are available
  155. self.requirements_to_check: List[str] = []
  156. # The PEP 517 backend we should use to build the project
  157. self.pep517_backend: Optional[BuildBackendHookCaller] = None
  158. # Are we using PEP 517 for this requirement?
  159. # After pyproject.toml has been loaded, the only valid values are True
  160. # and False. Before loading, None is valid (meaning "use the default").
  161. # Setting an explicit value before loading pyproject.toml is supported,
  162. # but after loading this flag should be treated as read only.
  163. self.use_pep517 = use_pep517
  164. # If config settings are provided, enforce PEP 517.
  165. if self.config_settings:
  166. if self.use_pep517 is False:
  167. logger.warning(
  168. "--no-use-pep517 ignored for %s "
  169. "because --config-settings are specified.",
  170. self,
  171. )
  172. self.use_pep517 = True
  173. # This requirement needs more preparation before it can be built
  174. self.needs_more_preparation = False
  175. # This requirement needs to be unpacked before it can be installed.
  176. self._archive_source: Optional[Path] = None
  177. def __str__(self) -> str:
  178. if self.req:
  179. s = redact_auth_from_requirement(self.req)
  180. if self.link:
  181. s += f" from {redact_auth_from_url(self.link.url)}"
  182. elif self.link:
  183. s = redact_auth_from_url(self.link.url)
  184. else:
  185. s = "<InstallRequirement>"
  186. if self.satisfied_by is not None:
  187. if self.satisfied_by.location is not None:
  188. location = display_path(self.satisfied_by.location)
  189. else:
  190. location = "<memory>"
  191. s += f" in {location}"
  192. if self.comes_from:
  193. if isinstance(self.comes_from, str):
  194. comes_from: Optional[str] = self.comes_from
  195. else:
  196. comes_from = self.comes_from.from_path()
  197. if comes_from:
  198. s += f" (from {comes_from})"
  199. return s
  200. def __repr__(self) -> str:
  201. return (
  202. f"<{self.__class__.__name__} object: "
  203. f"{str(self)} editable={self.editable!r}>"
  204. )
  205. def format_debug(self) -> str:
  206. """An un-tested helper for getting state, for debugging."""
  207. attributes = vars(self)
  208. names = sorted(attributes)
  209. state = (f"{attr}={attributes[attr]!r}" for attr in sorted(names))
  210. return "<{name} object: {{{state}}}>".format(
  211. name=self.__class__.__name__,
  212. state=", ".join(state),
  213. )
  214. # Things that are valid for all kinds of requirements?
  215. @property
  216. def name(self) -> Optional[str]:
  217. if self.req is None:
  218. return None
  219. return self.req.name
  220. @functools.cached_property
  221. def supports_pyproject_editable(self) -> bool:
  222. if not self.use_pep517:
  223. return False
  224. assert self.pep517_backend
  225. with self.build_env:
  226. runner = runner_with_spinner_message(
  227. "Checking if build backend supports build_editable"
  228. )
  229. with self.pep517_backend.subprocess_runner(runner):
  230. return "build_editable" in self.pep517_backend._supported_features()
  231. @property
  232. def specifier(self) -> SpecifierSet:
  233. assert self.req is not None
  234. return self.req.specifier
  235. @property
  236. def is_direct(self) -> bool:
  237. """Whether this requirement was specified as a direct URL."""
  238. return self.original_link is not None
  239. @property
  240. def is_pinned(self) -> bool:
  241. """Return whether I am pinned to an exact version.
  242. For example, some-package==1.2 is pinned; some-package>1.2 is not.
  243. """
  244. assert self.req is not None
  245. specifiers = self.req.specifier
  246. return len(specifiers) == 1 and next(iter(specifiers)).operator in {"==", "==="}
  247. def match_markers(self, extras_requested: Optional[Iterable[str]] = None) -> bool:
  248. if not extras_requested:
  249. # Provide an extra to safely evaluate the markers
  250. # without matching any extra
  251. extras_requested = ("",)
  252. if self.markers is not None:
  253. return any(
  254. self.markers.evaluate({"extra": extra}) for extra in extras_requested
  255. )
  256. else:
  257. return True
  258. @property
  259. def has_hash_options(self) -> bool:
  260. """Return whether any known-good hashes are specified as options.
  261. These activate --require-hashes mode; hashes specified as part of a
  262. URL do not.
  263. """
  264. return bool(self.hash_options)
  265. def hashes(self, trust_internet: bool = True) -> Hashes:
  266. """Return a hash-comparer that considers my option- and URL-based
  267. hashes to be known-good.
  268. Hashes in URLs--ones embedded in the requirements file, not ones
  269. downloaded from an index server--are almost peers with ones from
  270. flags. They satisfy --require-hashes (whether it was implicitly or
  271. explicitly activated) but do not activate it. md5 and sha224 are not
  272. allowed in flags, which should nudge people toward good algos. We
  273. always OR all hashes together, even ones from URLs.
  274. :param trust_internet: Whether to trust URL-based (#md5=...) hashes
  275. downloaded from the internet, as by populate_link()
  276. """
  277. good_hashes = self.hash_options.copy()
  278. if trust_internet:
  279. link = self.link
  280. elif self.is_direct and self.user_supplied:
  281. link = self.original_link
  282. else:
  283. link = None
  284. if link and link.hash:
  285. assert link.hash_name is not None
  286. good_hashes.setdefault(link.hash_name, []).append(link.hash)
  287. return Hashes(good_hashes)
  288. def from_path(self) -> Optional[str]:
  289. """Format a nice indicator to show where this "comes from" """
  290. if self.req is None:
  291. return None
  292. s = str(self.req)
  293. if self.comes_from:
  294. comes_from: Optional[str]
  295. if isinstance(self.comes_from, str):
  296. comes_from = self.comes_from
  297. else:
  298. comes_from = self.comes_from.from_path()
  299. if comes_from:
  300. s += "->" + comes_from
  301. return s
  302. def ensure_build_location(
  303. self, build_dir: str, autodelete: bool, parallel_builds: bool
  304. ) -> str:
  305. assert build_dir is not None
  306. if self._temp_build_dir is not None:
  307. assert self._temp_build_dir.path
  308. return self._temp_build_dir.path
  309. if self.req is None:
  310. # Some systems have /tmp as a symlink which confuses custom
  311. # builds (such as numpy). Thus, we ensure that the real path
  312. # is returned.
  313. self._temp_build_dir = TempDirectory(
  314. kind=tempdir_kinds.REQ_BUILD, globally_managed=True
  315. )
  316. return self._temp_build_dir.path
  317. # This is the only remaining place where we manually determine the path
  318. # for the temporary directory. It is only needed for editables where
  319. # it is the value of the --src option.
  320. # When parallel builds are enabled, add a UUID to the build directory
  321. # name so multiple builds do not interfere with each other.
  322. dir_name: str = canonicalize_name(self.req.name)
  323. if parallel_builds:
  324. dir_name = f"{dir_name}_{uuid.uuid4().hex}"
  325. # FIXME: Is there a better place to create the build_dir? (hg and bzr
  326. # need this)
  327. if not os.path.exists(build_dir):
  328. logger.debug("Creating directory %s", build_dir)
  329. os.makedirs(build_dir)
  330. actual_build_dir = os.path.join(build_dir, dir_name)
  331. # `None` indicates that we respect the globally-configured deletion
  332. # settings, which is what we actually want when auto-deleting.
  333. delete_arg = None if autodelete else False
  334. return TempDirectory(
  335. path=actual_build_dir,
  336. delete=delete_arg,
  337. kind=tempdir_kinds.REQ_BUILD,
  338. globally_managed=True,
  339. ).path
  340. def _set_requirement(self) -> None:
  341. """Set requirement after generating metadata."""
  342. assert self.req is None
  343. assert self.metadata is not None
  344. assert self.source_dir is not None
  345. # Construct a Requirement object from the generated metadata
  346. if isinstance(parse_version(self.metadata["Version"]), Version):
  347. op = "=="
  348. else:
  349. op = "==="
  350. self.req = get_requirement(
  351. "".join(
  352. [
  353. self.metadata["Name"],
  354. op,
  355. self.metadata["Version"],
  356. ]
  357. )
  358. )
  359. def warn_on_mismatching_name(self) -> None:
  360. assert self.req is not None
  361. metadata_name = canonicalize_name(self.metadata["Name"])
  362. if canonicalize_name(self.req.name) == metadata_name:
  363. # Everything is fine.
  364. return
  365. # If we're here, there's a mismatch. Log a warning about it.
  366. logger.warning(
  367. "Generating metadata for package %s "
  368. "produced metadata for project name %s. Fix your "
  369. "#egg=%s fragments.",
  370. self.name,
  371. metadata_name,
  372. self.name,
  373. )
  374. self.req = get_requirement(metadata_name)
  375. def check_if_exists(self, use_user_site: bool) -> None:
  376. """Find an installed distribution that satisfies or conflicts
  377. with this requirement, and set self.satisfied_by or
  378. self.should_reinstall appropriately.
  379. """
  380. if self.req is None:
  381. return
  382. existing_dist = get_default_environment().get_distribution(self.req.name)
  383. if not existing_dist:
  384. return
  385. version_compatible = self.req.specifier.contains(
  386. existing_dist.version,
  387. prereleases=True,
  388. )
  389. if not version_compatible:
  390. self.satisfied_by = None
  391. if use_user_site:
  392. if existing_dist.in_usersite:
  393. self.should_reinstall = True
  394. elif running_under_virtualenv() and existing_dist.in_site_packages:
  395. raise InstallationError(
  396. f"Will not install to the user site because it will "
  397. f"lack sys.path precedence to {existing_dist.raw_name} "
  398. f"in {existing_dist.location}"
  399. )
  400. else:
  401. self.should_reinstall = True
  402. else:
  403. if self.editable:
  404. self.should_reinstall = True
  405. # when installing editables, nothing pre-existing should ever
  406. # satisfy
  407. self.satisfied_by = None
  408. else:
  409. self.satisfied_by = existing_dist
  410. # Things valid for wheels
  411. @property
  412. def is_wheel(self) -> bool:
  413. if not self.link:
  414. return False
  415. return self.link.is_wheel
  416. @property
  417. def is_wheel_from_cache(self) -> bool:
  418. # When True, it means that this InstallRequirement is a local wheel file in the
  419. # cache of locally built wheels.
  420. return self.cached_wheel_source_link is not None
  421. # Things valid for sdists
  422. @property
  423. def unpacked_source_directory(self) -> str:
  424. assert self.source_dir, f"No source dir for {self}"
  425. return os.path.join(
  426. self.source_dir, self.link and self.link.subdirectory_fragment or ""
  427. )
  428. @property
  429. def setup_py_path(self) -> str:
  430. assert self.source_dir, f"No source dir for {self}"
  431. setup_py = os.path.join(self.unpacked_source_directory, "setup.py")
  432. return setup_py
  433. @property
  434. def setup_cfg_path(self) -> str:
  435. assert self.source_dir, f"No source dir for {self}"
  436. setup_cfg = os.path.join(self.unpacked_source_directory, "setup.cfg")
  437. return setup_cfg
  438. @property
  439. def pyproject_toml_path(self) -> str:
  440. assert self.source_dir, f"No source dir for {self}"
  441. return make_pyproject_path(self.unpacked_source_directory)
  442. def load_pyproject_toml(self) -> None:
  443. """Load the pyproject.toml file.
  444. After calling this routine, all of the attributes related to PEP 517
  445. processing for this requirement have been set. In particular, the
  446. use_pep517 attribute can be used to determine whether we should
  447. follow the PEP 517 or legacy (setup.py) code path.
  448. """
  449. pyproject_toml_data = load_pyproject_toml(
  450. self.use_pep517, self.pyproject_toml_path, self.setup_py_path, str(self)
  451. )
  452. if pyproject_toml_data is None:
  453. assert not self.config_settings
  454. self.use_pep517 = False
  455. return
  456. self.use_pep517 = True
  457. requires, backend, check, backend_path = pyproject_toml_data
  458. self.requirements_to_check = check
  459. self.pyproject_requires = requires
  460. self.pep517_backend = ConfiguredBuildBackendHookCaller(
  461. self,
  462. self.unpacked_source_directory,
  463. backend,
  464. backend_path=backend_path,
  465. )
  466. def isolated_editable_sanity_check(self) -> None:
  467. """Check that an editable requirement if valid for use with PEP 517/518.
  468. This verifies that an editable that has a pyproject.toml either supports PEP 660
  469. or as a setup.py or a setup.cfg
  470. """
  471. if (
  472. self.editable
  473. and self.use_pep517
  474. and not self.supports_pyproject_editable
  475. and not os.path.isfile(self.setup_py_path)
  476. and not os.path.isfile(self.setup_cfg_path)
  477. ):
  478. raise InstallationError(
  479. f"Project {self} has a 'pyproject.toml' and its build "
  480. f"backend is missing the 'build_editable' hook. Since it does not "
  481. f"have a 'setup.py' nor a 'setup.cfg', "
  482. f"it cannot be installed in editable mode. "
  483. f"Consider using a build backend that supports PEP 660."
  484. )
  485. def prepare_metadata(self) -> None:
  486. """Ensure that project metadata is available.
  487. Under PEP 517 and PEP 660, call the backend hook to prepare the metadata.
  488. Under legacy processing, call setup.py egg-info.
  489. """
  490. assert self.source_dir, f"No source dir for {self}"
  491. details = self.name or f"from {self.link}"
  492. if self.use_pep517:
  493. assert self.pep517_backend is not None
  494. if (
  495. self.editable
  496. and self.permit_editable_wheels
  497. and self.supports_pyproject_editable
  498. ):
  499. self.metadata_directory = generate_editable_metadata(
  500. build_env=self.build_env,
  501. backend=self.pep517_backend,
  502. details=details,
  503. )
  504. else:
  505. self.metadata_directory = generate_metadata(
  506. build_env=self.build_env,
  507. backend=self.pep517_backend,
  508. details=details,
  509. )
  510. else:
  511. self.metadata_directory = generate_metadata_legacy(
  512. build_env=self.build_env,
  513. setup_py_path=self.setup_py_path,
  514. source_dir=self.unpacked_source_directory,
  515. isolated=self.isolated,
  516. details=details,
  517. )
  518. # Act on the newly generated metadata, based on the name and version.
  519. if not self.name:
  520. self._set_requirement()
  521. else:
  522. self.warn_on_mismatching_name()
  523. self.assert_source_matches_version()
  524. @property
  525. def metadata(self) -> Any:
  526. if not hasattr(self, "_metadata"):
  527. self._metadata = self.get_dist().metadata
  528. return self._metadata
  529. def get_dist(self) -> BaseDistribution:
  530. if self.metadata_directory:
  531. return get_directory_distribution(self.metadata_directory)
  532. elif self.local_file_path and self.is_wheel:
  533. assert self.req is not None
  534. return get_wheel_distribution(
  535. FilesystemWheel(self.local_file_path),
  536. canonicalize_name(self.req.name),
  537. )
  538. raise AssertionError(
  539. f"InstallRequirement {self} has no metadata directory and no wheel: "
  540. f"can't make a distribution."
  541. )
  542. def assert_source_matches_version(self) -> None:
  543. assert self.source_dir, f"No source dir for {self}"
  544. version = self.metadata["version"]
  545. if self.req and self.req.specifier and version not in self.req.specifier:
  546. logger.warning(
  547. "Requested %s, but installing version %s",
  548. self,
  549. version,
  550. )
  551. else:
  552. logger.debug(
  553. "Source in %s has version %s, which satisfies requirement %s",
  554. display_path(self.source_dir),
  555. version,
  556. self,
  557. )
  558. # For both source distributions and editables
  559. def ensure_has_source_dir(
  560. self,
  561. parent_dir: str,
  562. autodelete: bool = False,
  563. parallel_builds: bool = False,
  564. ) -> None:
  565. """Ensure that a source_dir is set.
  566. This will create a temporary build dir if the name of the requirement
  567. isn't known yet.
  568. :param parent_dir: The ideal pip parent_dir for the source_dir.
  569. Generally src_dir for editables and build_dir for sdists.
  570. :return: self.source_dir
  571. """
  572. if self.source_dir is None:
  573. self.source_dir = self.ensure_build_location(
  574. parent_dir,
  575. autodelete=autodelete,
  576. parallel_builds=parallel_builds,
  577. )
  578. def needs_unpacked_archive(self, archive_source: Path) -> None:
  579. assert self._archive_source is None
  580. self._archive_source = archive_source
  581. def ensure_pristine_source_checkout(self) -> None:
  582. """Ensure the source directory has not yet been built in."""
  583. assert self.source_dir is not None
  584. if self._archive_source is not None:
  585. unpack_file(str(self._archive_source), self.source_dir)
  586. elif is_installable_dir(self.source_dir):
  587. # If a checkout exists, it's unwise to keep going.
  588. # version inconsistencies are logged later, but do not fail
  589. # the installation.
  590. raise PreviousBuildDirError(
  591. f"pip can't proceed with requirements '{self}' due to a "
  592. f"pre-existing build directory ({self.source_dir}). This is likely "
  593. "due to a previous installation that failed . pip is "
  594. "being responsible and not assuming it can delete this. "
  595. "Please delete it and try again."
  596. )
  597. # For editable installations
  598. def update_editable(self) -> None:
  599. if not self.link:
  600. logger.debug(
  601. "Cannot update repository at %s; repository location is unknown",
  602. self.source_dir,
  603. )
  604. return
  605. assert self.editable
  606. assert self.source_dir
  607. if self.link.scheme == "file":
  608. # Static paths don't get updated
  609. return
  610. vcs_backend = vcs.get_backend_for_scheme(self.link.scheme)
  611. # Editable requirements are validated in Requirement constructors.
  612. # So here, if it's neither a path nor a valid VCS URL, it's a bug.
  613. assert vcs_backend, f"Unsupported VCS URL {self.link.url}"
  614. hidden_url = hide_url(self.link.url)
  615. vcs_backend.obtain(self.source_dir, url=hidden_url, verbosity=0)
  616. # Top-level Actions
  617. def uninstall(
  618. self, auto_confirm: bool = False, verbose: bool = False
  619. ) -> Optional[UninstallPathSet]:
  620. """
  621. Uninstall the distribution currently satisfying this requirement.
  622. Prompts before removing or modifying files unless
  623. ``auto_confirm`` is True.
  624. Refuses to delete or modify files outside of ``sys.prefix`` -
  625. thus uninstallation within a virtual environment can only
  626. modify that virtual environment, even if the virtualenv is
  627. linked to global site-packages.
  628. """
  629. assert self.req
  630. dist = get_default_environment().get_distribution(self.req.name)
  631. if not dist:
  632. logger.warning("Skipping %s as it is not installed.", self.name)
  633. return None
  634. logger.info("Found existing installation: %s", dist)
  635. uninstalled_pathset = UninstallPathSet.from_dist(dist)
  636. uninstalled_pathset.remove(auto_confirm, verbose)
  637. return uninstalled_pathset
  638. def _get_archive_name(self, path: str, parentdir: str, rootdir: str) -> str:
  639. def _clean_zip_name(name: str, prefix: str) -> str:
  640. assert name.startswith(
  641. prefix + os.path.sep
  642. ), f"name {name!r} doesn't start with prefix {prefix!r}"
  643. name = name[len(prefix) + 1 :]
  644. name = name.replace(os.path.sep, "/")
  645. return name
  646. assert self.req is not None
  647. path = os.path.join(parentdir, path)
  648. name = _clean_zip_name(path, rootdir)
  649. return self.req.name + "/" + name
  650. def archive(self, build_dir: Optional[str]) -> None:
  651. """Saves archive to provided build_dir.
  652. Used for saving downloaded VCS requirements as part of `pip download`.
  653. """
  654. assert self.source_dir
  655. if build_dir is None:
  656. return
  657. create_archive = True
  658. archive_name = "{}-{}.zip".format(self.name, self.metadata["version"])
  659. archive_path = os.path.join(build_dir, archive_name)
  660. if os.path.exists(archive_path):
  661. response = ask_path_exists(
  662. f"The file {display_path(archive_path)} exists. (i)gnore, (w)ipe, "
  663. "(b)ackup, (a)bort ",
  664. ("i", "w", "b", "a"),
  665. )
  666. if response == "i":
  667. create_archive = False
  668. elif response == "w":
  669. logger.warning("Deleting %s", display_path(archive_path))
  670. os.remove(archive_path)
  671. elif response == "b":
  672. dest_file = backup_dir(archive_path)
  673. logger.warning(
  674. "Backing up %s to %s",
  675. display_path(archive_path),
  676. display_path(dest_file),
  677. )
  678. shutil.move(archive_path, dest_file)
  679. elif response == "a":
  680. sys.exit(-1)
  681. if not create_archive:
  682. return
  683. zip_output = zipfile.ZipFile(
  684. archive_path,
  685. "w",
  686. zipfile.ZIP_DEFLATED,
  687. allowZip64=True,
  688. )
  689. with zip_output:
  690. dir = os.path.normcase(os.path.abspath(self.unpacked_source_directory))
  691. for dirpath, dirnames, filenames in os.walk(dir):
  692. for dirname in dirnames:
  693. dir_arcname = self._get_archive_name(
  694. dirname,
  695. parentdir=dirpath,
  696. rootdir=dir,
  697. )
  698. zipdir = zipfile.ZipInfo(dir_arcname + "/")
  699. zipdir.external_attr = 0x1ED << 16 # 0o755
  700. zip_output.writestr(zipdir, "")
  701. for filename in filenames:
  702. file_arcname = self._get_archive_name(
  703. filename,
  704. parentdir=dirpath,
  705. rootdir=dir,
  706. )
  707. filename = os.path.join(dirpath, filename)
  708. zip_output.write(filename, file_arcname)
  709. logger.info("Saved %s", display_path(archive_path))
  710. def install(
  711. self,
  712. global_options: Optional[Sequence[str]] = None,
  713. root: Optional[str] = None,
  714. home: Optional[str] = None,
  715. prefix: Optional[str] = None,
  716. warn_script_location: bool = True,
  717. use_user_site: bool = False,
  718. pycompile: bool = True,
  719. ) -> None:
  720. assert self.req is not None
  721. scheme = get_scheme(
  722. self.req.name,
  723. user=use_user_site,
  724. home=home,
  725. root=root,
  726. isolated=self.isolated,
  727. prefix=prefix,
  728. )
  729. if self.editable and not self.is_wheel:
  730. deprecated(
  731. reason=(
  732. f"Legacy editable install of {self} (setup.py develop) "
  733. "is deprecated."
  734. ),
  735. replacement=(
  736. "to add a pyproject.toml or enable --use-pep517, "
  737. "and use setuptools >= 64. "
  738. "If the resulting installation is not behaving as expected, "
  739. "try using --config-settings editable_mode=compat. "
  740. "Please consult the setuptools documentation for more information"
  741. ),
  742. gone_in="25.0",
  743. issue=11457,
  744. )
  745. if self.config_settings:
  746. logger.warning(
  747. "--config-settings ignored for legacy editable install of %s. "
  748. "Consider upgrading to a version of setuptools "
  749. "that supports PEP 660 (>= 64).",
  750. self,
  751. )
  752. install_editable_legacy(
  753. global_options=global_options if global_options is not None else [],
  754. prefix=prefix,
  755. home=home,
  756. use_user_site=use_user_site,
  757. name=self.req.name,
  758. setup_py_path=self.setup_py_path,
  759. isolated=self.isolated,
  760. build_env=self.build_env,
  761. unpacked_source_directory=self.unpacked_source_directory,
  762. )
  763. self.install_succeeded = True
  764. return
  765. assert self.is_wheel
  766. assert self.local_file_path
  767. install_wheel(
  768. self.req.name,
  769. self.local_file_path,
  770. scheme=scheme,
  771. req_description=str(self.req),
  772. pycompile=pycompile,
  773. warn_script_location=warn_script_location,
  774. direct_url=self.download_info if self.is_direct else None,
  775. requested=self.user_supplied,
  776. )
  777. self.install_succeeded = True
  778. def check_invalid_constraint_type(req: InstallRequirement) -> str:
  779. # Check for unsupported forms
  780. problem = ""
  781. if not req.name:
  782. problem = "Unnamed requirements are not allowed as constraints"
  783. elif req.editable:
  784. problem = "Editable requirements are not allowed as constraints"
  785. elif req.extras:
  786. problem = "Constraints cannot have extras"
  787. if problem:
  788. deprecated(
  789. reason=(
  790. "Constraints are only allowed to take the form of a package "
  791. "name and a version specifier. Other forms were originally "
  792. "permitted as an accident of the implementation, but were "
  793. "undocumented. The new implementation of the resolver no "
  794. "longer supports these forms."
  795. ),
  796. replacement="replacing the constraint with a requirement",
  797. # No plan yet for when the new resolver becomes default
  798. gone_in=None,
  799. issue=8210,
  800. )
  801. return problem
  802. def _has_option(options: Values, reqs: List[InstallRequirement], option: str) -> bool:
  803. if getattr(options, option, None):
  804. return True
  805. for req in reqs:
  806. if getattr(req, option, None):
  807. return True
  808. return False
  809. def check_legacy_setup_py_options(
  810. options: Values,
  811. reqs: List[InstallRequirement],
  812. ) -> None:
  813. has_build_options = _has_option(options, reqs, "build_options")
  814. has_global_options = _has_option(options, reqs, "global_options")
  815. if has_build_options or has_global_options:
  816. deprecated(
  817. reason="--build-option and --global-option are deprecated.",
  818. issue=11859,
  819. replacement="to use --config-settings",
  820. gone_in="25.0",
  821. )
  822. logger.warning(
  823. "Implying --no-binary=:all: due to the presence of "
  824. "--build-option / --global-option. "
  825. )
  826. options.format_control.disallow_binaries()