req_install.py 31 KB

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