4from typing
import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, cast
23from .base
import Candidate, Requirement
24from .factory
import Factory
29 Result = RLResult[Requirement, Candidate, str]
36 _allowed_strategies = {
"eager",
"only-if-needed",
"to-satisfy-only"}
40 preparer: RequirementPreparer,
41 finder: PackageFinder,
42 wheel_cache: Optional[WheelCache],
43 make_install_req: InstallRequirementProvider,
45 ignore_dependencies: bool,
46 ignore_installed: bool,
47 ignore_requires_python: bool,
48 force_reinstall: bool,
49 upgrade_strategy: str,
50 py_version_info: Optional[Tuple[int, ...]] =
None,
58 make_install_req=make_install_req,
59 wheel_cache=wheel_cache,
60 use_user_site=use_user_site,
61 force_reinstall=force_reinstall,
62 ignore_installed=ignore_installed,
63 ignore_requires_python=ignore_requires_python,
64 py_version_info=py_version_info,
68 self._result: Optional[Result] =
None
71 self, root_reqs: List[InstallRequirement], check_supported_wheels: bool
73 collected = self.
factory.collect_root_requirements(root_reqs)
85 resolver: RLResolver[Requirement, Candidate, str] =
RLResolver(
91 limit_how_complex_resolution_can_be = 200000
96 except ResolutionImpossible
as e:
97 error = self.
factory.get_installation_error(
98 cast(
"ResolutionImpossible[Requirement, Candidate]", e),
103 req_set =
RequirementSet(check_supported_wheels=check_supported_wheels)
111 installed_dist = self.
factory.get_dist_to_uninstall(candidate)
112 if installed_dist
is None:
115 elif self.
factory.force_reinstall:
130 "%s is already installed with the same version as the "
131 "provided wheel. Use --force-reinstall to force an "
132 "installation of the wheel.",
147 "The candidate selected for download or install is a "
148 "yanked version: {name!r} candidate (version {version} "
149 "at {link})\nReason for being yanked: {reason}"
168 self, req_set: RequirementSet
169 ) -> List[InstallRequirement]:
170 """Get order for installation of requirements in RequirementSet.
172 The returned list contains a requirement before another that depends on
173 it. This helps ensure that the environment is kept consistent as they
174 get installed one-by-one.
176 The current implementation creates a topological ordering of the
177 dependency graph, giving more weight to packages with less
178 or no dependencies, while breaking any cycles in the graph at
179 arbitrary points. We make no guarantees about where the cycle
180 would be broken, other than it *would* be broken.
182 assert self._result
is not None,
"must call resolve() first"
188 graph = self._result.graph
191 sorted_items = sorted(
196 return [ireq
for _, ireq
in sorted_items]
200 graph:
"DirectedGraph[Optional[str]]", requirement_keys: Set[str]
201) -> Dict[Optional[str], int]:
202 """Assign weights to each node based on how "deep" they are.
204 This implementation may change at any point in the future without prior
207 We first simplify the dependency graph by pruning any leaves and giving them
208 the highest weight: a package without any dependencies should be installed
209 first. This is done again and again in the same way, giving ever less weight
210 to the newly found leaves. The loop stops when no leaves are left: all
211 remaining packages have at least one dependency left in the graph.
213 Then we continue with the remaining graph, by taking the length for the
214 longest path to any node from root, ignoring any paths that contain a single
215 node twice (i.e. cycles). This is done through a depth-first search through
216 the graph, while keeping track of the path to the node.
218 Cycles in the graph result would result in node being revisited while also
219 being on its own path. In this case, take no action. This helps ensure we
220 don't get stuck in a cycle.
222 When assigning weight, the longer path (i.e. larger length) is preferred.
224 We are only interested in the weights of packages that are in the
227 path: Set[Optional[str]] = set()
228 weights: Dict[Optional[str], int] = {}
230 def visit(node: Optional[str]) ->
None:
241 if node
not in requirement_keys:
245 weights[node] = max(last_known_parent_count,
len(path))
267 weight =
len(graph) - 1
269 if leaf
not in requirement_keys:
271 weights[leaf] = weight
283 assert not difference, difference
289 item: Tuple[str, InstallRequirement],
290 weights: Dict[Optional[str], int],
292 """Key function used to sort install requirements for installation.
294 Based on the "weight" mapping calculated in ``get_installation_order()``.
295 The canonical package name is returned as the second member as a tie-
296 breaker to ensure the result is predictable, which is useful in tests.
298 name = canonicalize_name(item[0])
299 return weights[name], name
List[InstallRequirement] get_installation_order(self, RequirementSet req_set)
RequirementSet resolve(self, List[InstallRequirement] root_reqs, bool check_supported_wheels)
__init__(self, RequirementPreparer preparer, PackageFinder finder, Optional[WheelCache] wheel_cache, InstallRequirementProvider make_install_req, bool use_user_site, bool ignore_dependencies, bool ignore_installed, bool ignore_requires_python, bool force_reinstall, str upgrade_strategy, Optional[Tuple[int,...]] py_version_info=None)
Dict[Optional[str], int] get_topological_weights("DirectedGraph[Optional[str]]" graph, Set[str] requirement_keys)
Tuple[int, str] _req_set_item_sorter(Tuple[str, InstallRequirement] item, Dict[Optional[str], int] weights)