Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_internal/req/__init__.py: 35%
40 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:48 +0000
1import collections
2import logging
3from typing import Generator, List, Optional, Sequence, Tuple
5from pip._internal.utils.logging import indent_log
7from .req_file import parse_requirements
8from .req_install import InstallRequirement
9from .req_set import RequirementSet
11__all__ = [
12 "RequirementSet",
13 "InstallRequirement",
14 "parse_requirements",
15 "install_given_reqs",
16]
18logger = logging.getLogger(__name__)
21class InstallationResult:
22 def __init__(self, name: str) -> None:
23 self.name = name
25 def __repr__(self) -> str:
26 return f"InstallationResult(name={self.name!r})"
29def _validate_requirements(
30 requirements: List[InstallRequirement],
31) -> Generator[Tuple[str, InstallRequirement], None, None]:
32 for req in requirements:
33 assert req.name, f"invalid to-be-installed requirement: {req}"
34 yield req.name, req
37def install_given_reqs(
38 requirements: List[InstallRequirement],
39 global_options: Sequence[str],
40 root: Optional[str],
41 home: Optional[str],
42 prefix: Optional[str],
43 warn_script_location: bool,
44 use_user_site: bool,
45 pycompile: bool,
46) -> List[InstallationResult]:
47 """
48 Install everything in the given list.
50 (to be called after having downloaded and unpacked the packages)
51 """
52 to_install = collections.OrderedDict(_validate_requirements(requirements))
54 if to_install:
55 logger.info(
56 "Installing collected packages: %s",
57 ", ".join(to_install.keys()),
58 )
60 installed = []
62 with indent_log():
63 for req_name, requirement in to_install.items():
64 if requirement.should_reinstall:
65 logger.info("Attempting uninstall: %s", req_name)
66 with indent_log():
67 uninstalled_pathset = requirement.uninstall(auto_confirm=True)
68 else:
69 uninstalled_pathset = None
71 try:
72 requirement.install(
73 global_options,
74 root=root,
75 home=home,
76 prefix=prefix,
77 warn_script_location=warn_script_location,
78 use_user_site=use_user_site,
79 pycompile=pycompile,
80 )
81 except Exception:
82 # if install did not succeed, rollback previous uninstall
83 if uninstalled_pathset and not requirement.install_succeeded:
84 uninstalled_pathset.rollback()
85 raise
86 else:
87 if uninstalled_pathset and requirement.install_succeeded:
88 uninstalled_pathset.commit()
90 installed.append(InstallationResult(req_name))
92 return installed