Debian Codegraph @

lib/ansible/galaxy/dependency_resolution/reporters.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright: (c) 2020-2021, Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4 """Requirement reporter implementations."""
5
6 from __future__ import annotations
7
8 from collections import defaultdict
9
10 try:
11 from resolvelib import BaseReporter
12 except ImportError:
13 class BaseReporter: # type: ignore[no-redef]
14 pass
15
16 try:
17 from resolvelib.resolvers import Criterion
18 except ImportError:
19 class Criterion: # type: ignore[no-redef]
20 pass
21
22 from ansible.utils.display import Display
23 from .dataclasses import Candidate, Requirement
24
25
26 display = Display()
27
28
29 _CLI_APP_NAME = 'ansible-galaxy'
30 _MESSAGES_AT_REJECT_COUNT = {
31 1: (
32 f'{_CLI_APP_NAME} is looking at multiple versions of {{fqcn}} to '
33 'determine which version is compatible with other '
34 'requirements. This could take a while.'
35 ),
36 8: (
37 f'{_CLI_APP_NAME} is looking at multiple versions of {{fqcn}} to '
38 'determine which version is compatible with other '
39 'requirements. This could take a while.'
40 ),
41 13: (
42 'This is taking longer than usual. You might need to provide '
43 'the dependency resolver with stricter constraints to reduce '
44 'runtime. If you want to abort this run, press Ctrl + C.'
45 ),
46 }
47
48
49 class CollectionDependencyReporter(BaseReporter):
50 """A dependency reporter for Ansible Collections.
51
52 This is a proxy class allowing us to abstract away importing resolvelib
53 outside of the `ansible.galaxy.dependency_resolution` Python package.
54 """
55
56 def __init__(self) -> None:
57 """Initialize the collection rejection counter."""
58 super().__init__()
59
60 self.reject_count_by_fqcn: defaultdict[str, int] = defaultdict(int)
61
62 def _maybe_log_rejection_message(self, candidate: Candidate) -> bool:
63 """Print out rejection messages on pre-defined limit hits."""
64 # Inspired by https://github.com/pypa/pip/commit/9731131
65 self.reject_count_by_fqcn[candidate.fqcn] += 1
66
67 collection_rejections_count = self.reject_count_by_fqcn[candidate.fqcn]
68
69 if collection_rejections_count not in _MESSAGES_AT_REJECT_COUNT:
70 return False
71
72 collection_rejection_message = _MESSAGES_AT_REJECT_COUNT[
73 collection_rejections_count
74 ]
75 display.display(collection_rejection_message.format(fqcn=candidate.fqcn))
76
77 return True
78
79 def rejecting_candidate( # resolvelib >= 0.9.0
80 self,
81 criterion: Criterion[Candidate, Requirement],
82 candidate: Candidate,
83 ) -> None:
84 """Augment rejection messages with conflict details."""
85 if not self._maybe_log_rejection_message(candidate):
86 return
87
88 msg = 'Will try a different candidate, due to conflict:'
89 for req_info in criterion.information:
90 req, parent = req_info.requirement, req_info.parent
91 msg += '\n '
92 if parent:
93 msg += f'{parent !s} depends on '
94 else:
95 msg += 'The user requested '
96 msg += str(req)
97 display.v(msg)
98
99 def backtracking(self, candidate: Candidate) -> None: # resolvelib < 0.9.0
100 """Print out rejection messages on pre-defined limit hits."""
101 self._maybe_log_rejection_message(candidate)