req_install.py 31 KB

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