req_install.py 32 KB

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