m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

923 lines
35 KiB

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