]> jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_internal/req/__init__.py
init: venv aand flask
[dlqueue.git] / venv / lib / python3.11 / site-packages / pip / _internal / req / __init__.py
1 import collections
2 import logging
3 from typing import Generator, List, Optional, Sequence, Tuple
4
5 from pip._internal.utils.logging import indent_log
6
7 from .req_file import parse_requirements
8 from .req_install import InstallRequirement
9 from .req_set import RequirementSet
10
11 __all__ = [
12 "RequirementSet",
13 "InstallRequirement",
14 "parse_requirements",
15 "install_given_reqs",
16 ]
17
18 logger = logging.getLogger(__name__)
19
20
21 class InstallationResult:
22 def __init__(self, name: str) -> None:
23 self.name = name
24
25 def __repr__(self) -> str:
26 return f"InstallationResult(name={self.name!r})"
27
28
29 def _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
35
36
37 def 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.
49
50 (to be called after having downloaded and unpacked the packages)
51 """
52 to_install = collections.OrderedDict(_validate_requirements(requirements))
53
54 if to_install:
55 logger.info(
56 "Installing collected packages: %s",
57 ", ".join(to_install.keys()),
58 )
59
60 installed = []
61
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
70
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()
89
90 installed.append(InstallationResult(req_name))
91
92 return installed