req_command.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. """Contains the Command base classes that depend on PipSession.
  2. The classes in this module are in a separate module so the commands not
  3. needing download / PackageFinder capability don't unnecessarily import the
  4. PackageFinder machinery and all its vendored dependencies, etc.
  5. """
  6. import logging
  7. import os
  8. import sys
  9. from functools import partial
  10. from optparse import Values
  11. from typing import Any, List, Optional, Tuple
  12. from pip._internal.cache import WheelCache
  13. from pip._internal.cli import cmdoptions
  14. from pip._internal.cli.base_command import Command
  15. from pip._internal.cli.command_context import CommandContextMixIn
  16. from pip._internal.exceptions import CommandError, PreviousBuildDirError
  17. from pip._internal.index.collector import LinkCollector
  18. from pip._internal.index.package_finder import PackageFinder
  19. from pip._internal.models.selection_prefs import SelectionPreferences
  20. from pip._internal.models.target_python import TargetPython
  21. from pip._internal.network.session import PipSession
  22. from pip._internal.operations.prepare import RequirementPreparer
  23. from pip._internal.req.constructors import (
  24. install_req_from_editable,
  25. install_req_from_line,
  26. install_req_from_parsed_requirement,
  27. install_req_from_req_string,
  28. )
  29. from pip._internal.req.req_file import parse_requirements
  30. from pip._internal.req.req_install import InstallRequirement
  31. from pip._internal.req.req_tracker import RequirementTracker
  32. from pip._internal.resolution.base import BaseResolver
  33. from pip._internal.self_outdated_check import pip_self_version_check
  34. from pip._internal.utils.deprecation import deprecated
  35. from pip._internal.utils.temp_dir import (
  36. TempDirectory,
  37. TempDirectoryTypeRegistry,
  38. tempdir_kinds,
  39. )
  40. from pip._internal.utils.virtualenv import running_under_virtualenv
  41. logger = logging.getLogger(__name__)
  42. class SessionCommandMixin(CommandContextMixIn):
  43. """
  44. A class mixin for command classes needing _build_session().
  45. """
  46. def __init__(self) -> None:
  47. super().__init__()
  48. self._session: Optional[PipSession] = None
  49. @classmethod
  50. def _get_index_urls(cls, options: Values) -> Optional[List[str]]:
  51. """Return a list of index urls from user-provided options."""
  52. index_urls = []
  53. if not getattr(options, "no_index", False):
  54. url = getattr(options, "index_url", None)
  55. if url:
  56. index_urls.append(url)
  57. urls = getattr(options, "extra_index_urls", None)
  58. if urls:
  59. index_urls.extend(urls)
  60. # Return None rather than an empty list
  61. return index_urls or None
  62. def get_default_session(self, options: Values) -> PipSession:
  63. """Get a default-managed session."""
  64. if self._session is None:
  65. self._session = self.enter_context(self._build_session(options))
  66. # there's no type annotation on requests.Session, so it's
  67. # automatically ContextManager[Any] and self._session becomes Any,
  68. # then https://github.com/python/mypy/issues/7696 kicks in
  69. assert self._session is not None
  70. return self._session
  71. def _build_session(
  72. self,
  73. options: Values,
  74. retries: Optional[int] = None,
  75. timeout: Optional[int] = None,
  76. ) -> PipSession:
  77. assert not options.cache_dir or os.path.isabs(options.cache_dir)
  78. session = PipSession(
  79. cache=(
  80. os.path.join(options.cache_dir, "http") if options.cache_dir else None
  81. ),
  82. retries=retries if retries is not None else options.retries,
  83. trusted_hosts=options.trusted_hosts,
  84. index_urls=self._get_index_urls(options),
  85. )
  86. # Handle custom ca-bundles from the user
  87. if options.cert:
  88. session.verify = options.cert
  89. # Handle SSL client certificate
  90. if options.client_cert:
  91. session.cert = options.client_cert
  92. # Handle timeouts
  93. if options.timeout or timeout:
  94. session.timeout = timeout if timeout is not None else options.timeout
  95. # Handle configured proxies
  96. if options.proxy:
  97. session.proxies = {
  98. "http": options.proxy,
  99. "https": options.proxy,
  100. }
  101. # Determine if we can prompt the user for authentication or not
  102. session.auth.prompting = not options.no_input
  103. return session
  104. class IndexGroupCommand(Command, SessionCommandMixin):
  105. """
  106. Abstract base class for commands with the index_group options.
  107. This also corresponds to the commands that permit the pip version check.
  108. """
  109. def handle_pip_version_check(self, options: Values) -> None:
  110. """
  111. Do the pip version check if not disabled.
  112. This overrides the default behavior of not doing the check.
  113. """
  114. # Make sure the index_group options are present.
  115. assert hasattr(options, "no_index")
  116. if options.disable_pip_version_check or options.no_index:
  117. return
  118. # Otherwise, check if we're using the latest version of pip available.
  119. session = self._build_session(
  120. options, retries=0, timeout=min(5, options.timeout)
  121. )
  122. with session:
  123. pip_self_version_check(session, options)
  124. KEEPABLE_TEMPDIR_TYPES = [
  125. tempdir_kinds.BUILD_ENV,
  126. tempdir_kinds.EPHEM_WHEEL_CACHE,
  127. tempdir_kinds.REQ_BUILD,
  128. ]
  129. def warn_if_run_as_root() -> None:
  130. """Output a warning for sudo users on Unix.
  131. In a virtual environment, sudo pip still writes to virtualenv.
  132. On Windows, users may run pip as Administrator without issues.
  133. This warning only applies to Unix root users outside of virtualenv.
  134. """
  135. if running_under_virtualenv():
  136. return
  137. if not hasattr(os, "getuid"):
  138. return
  139. # On Windows, there are no "system managed" Python packages. Installing as
  140. # Administrator via pip is the correct way of updating system environments.
  141. #
  142. # We choose sys.platform over utils.compat.WINDOWS here to enable Mypy platform
  143. # checks: https://mypy.readthedocs.io/en/stable/common_issues.html
  144. if sys.platform == "win32" or sys.platform == "cygwin":
  145. return
  146. if os.getuid() != 0:
  147. return
  148. logger.warning(
  149. "Running pip as the 'root' user can result in broken permissions and "
  150. "conflicting behaviour with the system package manager. "
  151. "It is recommended to use a virtual environment instead: "
  152. "https://pip.pypa.io/warnings/venv"
  153. )
  154. def with_cleanup(func: Any) -> Any:
  155. """Decorator for common logic related to managing temporary
  156. directories.
  157. """
  158. def configure_tempdir_registry(registry: TempDirectoryTypeRegistry) -> None:
  159. for t in KEEPABLE_TEMPDIR_TYPES:
  160. registry.set_delete(t, False)
  161. def wrapper(
  162. self: RequirementCommand, options: Values, args: List[Any]
  163. ) -> Optional[int]:
  164. assert self.tempdir_registry is not None
  165. if options.no_clean:
  166. configure_tempdir_registry(self.tempdir_registry)
  167. try:
  168. return func(self, options, args)
  169. except PreviousBuildDirError:
  170. # This kind of conflict can occur when the user passes an explicit
  171. # build directory with a pre-existing folder. In that case we do
  172. # not want to accidentally remove it.
  173. configure_tempdir_registry(self.tempdir_registry)
  174. raise
  175. return wrapper
  176. class RequirementCommand(IndexGroupCommand):
  177. def __init__(self, *args: Any, **kw: Any) -> None:
  178. super().__init__(*args, **kw)
  179. self.cmd_opts.add_option(cmdoptions.no_clean())
  180. @staticmethod
  181. def determine_resolver_variant(options: Values) -> str:
  182. """Determines which resolver should be used, based on the given options."""
  183. if "legacy-resolver" in options.deprecated_features_enabled:
  184. return "legacy"
  185. return "2020-resolver"
  186. @classmethod
  187. def make_requirement_preparer(
  188. cls,
  189. temp_build_dir: TempDirectory,
  190. options: Values,
  191. req_tracker: RequirementTracker,
  192. session: PipSession,
  193. finder: PackageFinder,
  194. use_user_site: bool,
  195. download_dir: Optional[str] = None,
  196. ) -> RequirementPreparer:
  197. """
  198. Create a RequirementPreparer instance for the given parameters.
  199. """
  200. temp_build_dir_path = temp_build_dir.path
  201. assert temp_build_dir_path is not None
  202. resolver_variant = cls.determine_resolver_variant(options)
  203. if resolver_variant == "2020-resolver":
  204. lazy_wheel = "fast-deps" in options.features_enabled
  205. if lazy_wheel:
  206. logger.warning(
  207. "pip is using lazily downloaded wheels using HTTP "
  208. "range requests to obtain dependency information. "
  209. "This experimental feature is enabled through "
  210. "--use-feature=fast-deps and it is not ready for "
  211. "production."
  212. )
  213. else:
  214. lazy_wheel = False
  215. if "fast-deps" in options.features_enabled:
  216. logger.warning(
  217. "fast-deps has no effect when used with the legacy resolver."
  218. )
  219. in_tree_build = "out-of-tree-build" not in options.deprecated_features_enabled
  220. if "in-tree-build" in options.features_enabled:
  221. deprecated(
  222. reason="In-tree builds are now the default.",
  223. replacement="to remove the --use-feature=in-tree-build flag",
  224. gone_in="22.1",
  225. )
  226. if "out-of-tree-build" in options.deprecated_features_enabled:
  227. deprecated(
  228. reason="Out-of-tree builds are deprecated.",
  229. replacement=None,
  230. gone_in="22.1",
  231. )
  232. return RequirementPreparer(
  233. build_dir=temp_build_dir_path,
  234. src_dir=options.src_dir,
  235. download_dir=download_dir,
  236. build_isolation=options.build_isolation,
  237. req_tracker=req_tracker,
  238. session=session,
  239. progress_bar=options.progress_bar,
  240. finder=finder,
  241. require_hashes=options.require_hashes,
  242. use_user_site=use_user_site,
  243. lazy_wheel=lazy_wheel,
  244. in_tree_build=in_tree_build,
  245. )
  246. @classmethod
  247. def make_resolver(
  248. cls,
  249. preparer: RequirementPreparer,
  250. finder: PackageFinder,
  251. options: Values,
  252. wheel_cache: Optional[WheelCache] = None,
  253. use_user_site: bool = False,
  254. ignore_installed: bool = True,
  255. ignore_requires_python: bool = False,
  256. force_reinstall: bool = False,
  257. upgrade_strategy: str = "to-satisfy-only",
  258. use_pep517: Optional[bool] = None,
  259. py_version_info: Optional[Tuple[int, ...]] = None,
  260. ) -> BaseResolver:
  261. """
  262. Create a Resolver instance for the given parameters.
  263. """
  264. make_install_req = partial(
  265. install_req_from_req_string,
  266. isolated=options.isolated_mode,
  267. use_pep517=use_pep517,
  268. )
  269. resolver_variant = cls.determine_resolver_variant(options)
  270. # The long import name and duplicated invocation is needed to convince
  271. # Mypy into correctly typechecking. Otherwise it would complain the
  272. # "Resolver" class being redefined.
  273. if resolver_variant == "2020-resolver":
  274. import pip._internal.resolution.resolvelib.resolver
  275. return pip._internal.resolution.resolvelib.resolver.Resolver(
  276. preparer=preparer,
  277. finder=finder,
  278. wheel_cache=wheel_cache,
  279. make_install_req=make_install_req,
  280. use_user_site=use_user_site,
  281. ignore_dependencies=options.ignore_dependencies,
  282. ignore_installed=ignore_installed,
  283. ignore_requires_python=ignore_requires_python,
  284. force_reinstall=force_reinstall,
  285. upgrade_strategy=upgrade_strategy,
  286. py_version_info=py_version_info,
  287. )
  288. import pip._internal.resolution.legacy.resolver
  289. return pip._internal.resolution.legacy.resolver.Resolver(
  290. preparer=preparer,
  291. finder=finder,
  292. wheel_cache=wheel_cache,
  293. make_install_req=make_install_req,
  294. use_user_site=use_user_site,
  295. ignore_dependencies=options.ignore_dependencies,
  296. ignore_installed=ignore_installed,
  297. ignore_requires_python=ignore_requires_python,
  298. force_reinstall=force_reinstall,
  299. upgrade_strategy=upgrade_strategy,
  300. py_version_info=py_version_info,
  301. )
  302. def get_requirements(
  303. self,
  304. args: List[str],
  305. options: Values,
  306. finder: PackageFinder,
  307. session: PipSession,
  308. ) -> List[InstallRequirement]:
  309. """
  310. Parse command-line arguments into the corresponding requirements.
  311. """
  312. requirements: List[InstallRequirement] = []
  313. for filename in options.constraints:
  314. for parsed_req in parse_requirements(
  315. filename,
  316. constraint=True,
  317. finder=finder,
  318. options=options,
  319. session=session,
  320. ):
  321. req_to_add = install_req_from_parsed_requirement(
  322. parsed_req,
  323. isolated=options.isolated_mode,
  324. user_supplied=False,
  325. )
  326. requirements.append(req_to_add)
  327. for req in args:
  328. req_to_add = install_req_from_line(
  329. req,
  330. None,
  331. isolated=options.isolated_mode,
  332. use_pep517=options.use_pep517,
  333. user_supplied=True,
  334. )
  335. requirements.append(req_to_add)
  336. for req in options.editables:
  337. req_to_add = install_req_from_editable(
  338. req,
  339. user_supplied=True,
  340. isolated=options.isolated_mode,
  341. use_pep517=options.use_pep517,
  342. )
  343. requirements.append(req_to_add)
  344. # NOTE: options.require_hashes may be set if --require-hashes is True
  345. for filename in options.requirements:
  346. for parsed_req in parse_requirements(
  347. filename, finder=finder, options=options, session=session
  348. ):
  349. req_to_add = install_req_from_parsed_requirement(
  350. parsed_req,
  351. isolated=options.isolated_mode,
  352. use_pep517=options.use_pep517,
  353. user_supplied=True,
  354. )
  355. requirements.append(req_to_add)
  356. # If any requirement has hash options, enable hash checking.
  357. if any(req.has_hash_options for req in requirements):
  358. options.require_hashes = True
  359. if not (args or options.editables or options.requirements):
  360. opts = {"name": self.name}
  361. if options.find_links:
  362. raise CommandError(
  363. "You must give at least one requirement to {name} "
  364. '(maybe you meant "pip {name} {links}"?)'.format(
  365. **dict(opts, links=" ".join(options.find_links))
  366. )
  367. )
  368. else:
  369. raise CommandError(
  370. "You must give at least one requirement to {name} "
  371. '(see "pip help {name}")'.format(**opts)
  372. )
  373. return requirements
  374. @staticmethod
  375. def trace_basic_info(finder: PackageFinder) -> None:
  376. """
  377. Trace basic information about the provided objects.
  378. """
  379. # Display where finder is looking for packages
  380. search_scope = finder.search_scope
  381. locations = search_scope.get_formatted_locations()
  382. if locations:
  383. logger.info(locations)
  384. def _build_package_finder(
  385. self,
  386. options: Values,
  387. session: PipSession,
  388. target_python: Optional[TargetPython] = None,
  389. ignore_requires_python: Optional[bool] = None,
  390. ) -> PackageFinder:
  391. """
  392. Create a package finder appropriate to this requirement command.
  393. :param ignore_requires_python: Whether to ignore incompatible
  394. "Requires-Python" values in links. Defaults to False.
  395. """
  396. link_collector = LinkCollector.create(session, options=options)
  397. selection_prefs = SelectionPreferences(
  398. allow_yanked=True,
  399. format_control=options.format_control,
  400. allow_all_prereleases=options.pre,
  401. prefer_binary=options.prefer_binary,
  402. ignore_requires_python=ignore_requires_python,
  403. )
  404. return PackageFinder.create(
  405. link_collector=link_collector,
  406. selection_prefs=selection_prefs,
  407. target_python=target_python,
  408. )