comparison env/lib/python3.9/site-packages/pip/_internal/req/__init__.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import collections
2 import logging
3
4 from pip._internal.utils.logging import indent_log
5 from pip._internal.utils.typing import MYPY_CHECK_RUNNING
6
7 from .req_file import parse_requirements
8 from .req_install import InstallRequirement
9 from .req_set import RequirementSet
10
11 if MYPY_CHECK_RUNNING:
12 from typing import Iterator, List, Optional, Sequence, Tuple
13
14 __all__ = [
15 "RequirementSet", "InstallRequirement",
16 "parse_requirements", "install_given_reqs",
17 ]
18
19 logger = logging.getLogger(__name__)
20
21
22 class InstallationResult:
23 def __init__(self, name):
24 # type: (str) -> None
25 self.name = name
26
27 def __repr__(self):
28 # type: () -> str
29 return f"InstallationResult(name={self.name!r})"
30
31
32 def _validate_requirements(
33 requirements, # type: List[InstallRequirement]
34 ):
35 # type: (...) -> Iterator[Tuple[str, InstallRequirement]]
36 for req in requirements:
37 assert req.name, f"invalid to-be-installed requirement: {req}"
38 yield req.name, req
39
40
41 def install_given_reqs(
42 requirements, # type: List[InstallRequirement]
43 install_options, # type: List[str]
44 global_options, # type: Sequence[str]
45 root, # type: Optional[str]
46 home, # type: Optional[str]
47 prefix, # type: Optional[str]
48 warn_script_location, # type: bool
49 use_user_site, # type: bool
50 pycompile, # type: bool
51 ):
52 # type: (...) -> List[InstallationResult]
53 """
54 Install everything in the given list.
55
56 (to be called after having downloaded and unpacked the packages)
57 """
58 to_install = collections.OrderedDict(_validate_requirements(requirements))
59
60 if to_install:
61 logger.info(
62 'Installing collected packages: %s',
63 ', '.join(to_install.keys()),
64 )
65
66 installed = []
67
68 with indent_log():
69 for req_name, requirement in to_install.items():
70 if requirement.should_reinstall:
71 logger.info('Attempting uninstall: %s', req_name)
72 with indent_log():
73 uninstalled_pathset = requirement.uninstall(
74 auto_confirm=True
75 )
76 else:
77 uninstalled_pathset = None
78
79 try:
80 requirement.install(
81 install_options,
82 global_options,
83 root=root,
84 home=home,
85 prefix=prefix,
86 warn_script_location=warn_script_location,
87 use_user_site=use_user_site,
88 pycompile=pycompile,
89 )
90 except Exception:
91 # if install did not succeed, rollback previous uninstall
92 if uninstalled_pathset and not requirement.install_succeeded:
93 uninstalled_pathset.rollback()
94 raise
95 else:
96 if uninstalled_pathset and requirement.install_succeeded:
97 uninstalled_pathset.commit()
98
99 installed.append(InstallationResult(req_name))
100
101 return installed