req_install.py 34 KB

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