req_install.py 32 KB

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