1"""Contains the Command base classes that depend on PipSession.
3The classes in this module are in a separate module so the commands not
4needing download / PackageFinder capability don't unnecessarily import the
5PackageFinder machinery and all its vendored dependencies, etc.
11from functools
import partial
12from optparse
import Values
13from typing
import TYPE_CHECKING, Any, List, Optional, Tuple
28 install_req_from_editable,
29 install_req_from_line,
30 install_req_from_parsed_requirement,
31 install_req_from_req_string,
39 TempDirectoryTypeRegistry,
45 from ssl
import SSLContext
52 raise CommandError(
"The truststore feature is only available for Python 3.10+")
57 logger.warning(
"Disabling truststore since ssl support is missing")
64 "To use the truststore feature, 'truststore' must be installed into "
65 "pip's current environment."
74 A class mixin for command classes needing _build_session().
79 self.
_session: Optional[PipSession] =
None
83 """Return a list of index urls from user-provided options."""
85 if not getattr(options,
"no_index",
False):
86 url =
getattr(options,
"index_url",
None)
89 urls =
getattr(options,
"extra_index_urls",
None)
93 return index_urls
or None
96 """Get a default-managed session."""
108 retries: Optional[int] =
None,
109 timeout: Optional[int] =
None,
110 fallback_to_certifi: bool =
False,
119 if not fallback_to_certifi:
125 session = PipSession(
126 cache=
os.path.join(cache_dir,
"http")
if cache_dir
else None,
130 ssl_context=ssl_context,
162 Abstract base class for commands with the index_group options.
164 This also corresponds to the commands that permit the pip version check.
169 Do the pip version check if not disabled.
171 This overrides the default behavior of not doing the check.
174 assert hasattr(options,
"no_index")
188 fallback_to_certifi=
True,
191 pip_self_version_check(session, options)
194KEEPABLE_TEMPDIR_TYPES = [
202 """Output a warning for sudo users on Unix.
204 In a virtual environment, sudo pip still writes to virtualenv.
205 On Windows, users may run pip as Administrator without issues.
206 This warning only applies to Unix root users outside of virtualenv.
208 if running_under_virtualenv():
224 "Running pip as the 'root' user can result in broken permissions and "
225 "conflicting behaviour with the system package manager. "
226 "It is recommended to use a virtual environment instead: "
227 "https://pip.pypa.io/warnings/venv"
231def with_cleanup(func: Any) -> Any:
232 """Decorator for common logic related to managing temporary
237 for t
in KEEPABLE_TEMPDIR_TYPES:
241 self: RequirementCommand, options: Values, args: List[Any]
243 assert self.tempdir_registry
is not None
248 return func(self, options, args)
249 except PreviousBuildDirError:
267 """Determines which resolver should be used, based on the given options."""
271 return "2020-resolver"
276 temp_build_dir: TempDirectory,
278 build_tracker: BuildTracker,
280 finder: PackageFinder,
282 download_dir: Optional[str] =
None,
284 ) -> RequirementPreparer:
286 Create a RequirementPreparer instance for the given parameters.
289 assert temp_build_dir_path
is not None
290 legacy_resolver =
False
293 if resolver_variant ==
"2020-resolver":
297 "pip is using lazily downloaded wheels using HTTP "
298 "range requests to obtain dependency information. "
299 "This experimental feature is enabled through "
300 "--use-feature=fast-deps and it is not ready for "
304 legacy_resolver =
True
308 "fast-deps has no effect when used with the legacy resolver."
312 build_dir=temp_build_dir_path,
314 download_dir=download_dir,
317 build_tracker=build_tracker,
322 use_user_site=use_user_site,
323 lazy_wheel=lazy_wheel,
325 legacy_resolver=legacy_resolver,
331 preparer: RequirementPreparer,
332 finder: PackageFinder,
334 wheel_cache: Optional[WheelCache] =
None,
335 use_user_site: bool =
False,
336 ignore_installed: bool =
True,
337 ignore_requires_python: bool =
False,
338 force_reinstall: bool =
False,
339 upgrade_strategy: str =
"to-satisfy-only",
340 use_pep517: Optional[bool] =
None,
341 py_version_info: Optional[Tuple[int, ...]] =
None,
344 Create a Resolver instance for the given parameters.
346 make_install_req = partial(
347 install_req_from_req_string,
349 use_pep517=use_pep517,
355 if resolver_variant ==
"2020-resolver":
361 wheel_cache=wheel_cache,
362 make_install_req=make_install_req,
363 use_user_site=use_user_site,
365 ignore_installed=ignore_installed,
366 ignore_requires_python=ignore_requires_python,
367 force_reinstall=force_reinstall,
368 upgrade_strategy=upgrade_strategy,
369 py_version_info=py_version_info,
376 wheel_cache=wheel_cache,
377 make_install_req=make_install_req,
378 use_user_site=use_user_site,
380 ignore_installed=ignore_installed,
381 ignore_requires_python=ignore_requires_python,
382 force_reinstall=force_reinstall,
383 upgrade_strategy=upgrade_strategy,
384 py_version_info=py_version_info,
391 finder: PackageFinder,
393 ) -> List[InstallRequirement]:
395 Parse command-line arguments into the corresponding requirements.
397 requirements: List[InstallRequirement] = []
399 for parsed_req
in parse_requirements(
406 req_to_add = install_req_from_parsed_requirement(
414 req_to_add = install_req_from_line(
420 config_settings=
getattr(options,
"config_settings",
None),
425 req_to_add = install_req_from_editable(
430 config_settings=
getattr(options,
"config_settings",
None),
436 for parsed_req
in parse_requirements(
437 filename, finder=finder, options=options, session=session
439 req_to_add = install_req_from_parsed_requirement(
455 opts = {
"name": self.
name}
458 "You must give at least one requirement to {name} "
459 '(maybe you meant "pip {name} {links}"?)'.format(
465 "You must give at least one requirement to {name} "
466 '(see "pip help {name}")'.format(**opts)
474 Trace basic information about the provided objects.
486 target_python: Optional[TargetPython] =
None,
487 ignore_requires_python: Optional[bool] =
None,
490 Create a package finder appropriate to this requirement command.
492 :param ignore_requires_python: Whether to ignore incompatible
493 "Requires-Python" values in links. Defaults to False.
501 ignore_requires_python=ignore_requires_python,
505 link_collector=link_collector,
506 selection_prefs=selection_prefs,
507 target_python=target_python,
_T enter_context(self, ContextManager[_T] context_provider)
None handle_pip_version_check(self, Values options)
List[InstallRequirement] get_requirements(self, List[str] args, Values options, PackageFinder finder, PipSession session)
PackageFinder _build_package_finder(self, Values options, PipSession session, Optional[TargetPython] target_python=None, Optional[bool] ignore_requires_python=None)
None trace_basic_info(PackageFinder finder)
None __init__(self, *Any args, **Any kw)
str determine_resolver_variant(Values options)
BaseResolver make_resolver(cls, RequirementPreparer preparer, PackageFinder finder, Values options, Optional[WheelCache] wheel_cache=None, bool use_user_site=False, bool ignore_installed=True, bool ignore_requires_python=False, bool force_reinstall=False, str upgrade_strategy="to-satisfy-only", Optional[bool] use_pep517=None, Optional[Tuple[int,...]] py_version_info=None)
RequirementPreparer make_requirement_preparer(cls, TempDirectory temp_build_dir, Values options, BuildTracker build_tracker, PipSession session, PackageFinder finder, bool use_user_site, Optional[str] download_dir=None, int verbosity=0)
PipSession _build_session(self, Values options, Optional[int] retries=None, Optional[int] timeout=None, bool fallback_to_certifi=False)
PipSession get_default_session(self, Values options)
Optional[List[str]] _get_index_urls(cls, Values options)
Optional["SSLContext"] _create_truststore_ssl_context()
None warn_if_run_as_root()