Debian Codegraph @

.evergreen/legacy_config_generator/evergreen_config_generator/tasks.py

raw
1 # Copyright 2009-present MongoDB, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import copy
16 import itertools
17 from collections import OrderedDict as OD
18 from itertools import chain, product
19 from typing import ClassVar, Iterable, Literal, Mapping, MutableMapping, Sequence, Union
20
21 from evergreen_config_generator import ConfigObject
22 from evergreen_config_generator.functions import func
23
24 from . import MutableValueMapping, Value, ValueSequence
25
26 DependencySpec = Union[str, Mapping[str, Value]]
27
28
29 class Task(ConfigObject):
30 def __init__(
31 self,
32 task_name: str | None = None,
33 commands: Iterable[Value] = (),
34 tags: Iterable[str] = (),
35 depends_on: Iterable[DependencySpec] = (),
36 exec_timeout_secs: int | None = None,
37 allowed_requesters: Iterable[str] = (),
38 ):
39 self._name = task_name
40 self._tags = list(tags)
41 self.options: MutableValueMapping = OD()
42 self.commands: ValueSequence = list(commands)
43 self.exec_timeout_secs = exec_timeout_secs
44 self._depends_on = list(map(self._normal_dep, depends_on))
45 self.allowed_requesters = list(allowed_requesters)
46
47 if exec_timeout_secs is not None:
48 self.options['exec_timeout_secs'] = exec_timeout_secs
49
50 @property
51 def dependencies(self) -> Sequence[Mapping[str, Value]]:
52 main = list(self._depends_on)
53 main.extend(map(self._normal_dep, self.additional_dependencies()))
54 return tuple(main)
55
56 def _normal_dep(self, spec: DependencySpec) -> Mapping[str, Value]:
57 if isinstance(spec, str):
58 return OD([('name', spec)])
59 return spec
60
61 @property
62 def tags(self) -> Sequence[str]:
63 return tuple(sorted(chain(self.additional_tags(), self._tags)))
64
65 def pre_commands(self) -> Iterable[Value]:
66 return ()
67
68 def main_commands(self) -> Iterable[Value]:
69 return ()
70
71 def post_commands(self) -> Iterable[Value]:
72 return ()
73
74 def additional_dependencies(self) -> Iterable[DependencySpec]:
75 return ()
76
77 @property
78 def name(self) -> str:
79 assert self._name is not None, f'Task {self} did not set a name, and did not override the "name" property'
80 return self._name
81
82 def additional_tags(self) -> Iterable[str]:
83 return ()
84
85 def add_dependency(self, dependency: DependencySpec):
86 if isinstance(dependency, str):
87 dependency = OD([('name', dependency)])
88
89 self._depends_on.append(dependency)
90
91 def to_dict(self):
92 task: MutableValueMapping = super().to_dict() # type: ignore
93 assert isinstance(task, MutableMapping)
94 if self.tags:
95 task['tags'] = list(self.tags)
96 task.update(self.options)
97 deps: Sequence[MutableValueMapping] = list(self.dependencies) # type: ignore
98 if deps:
99 if len(deps) == 1:
100 task['depends_on'] = OD(deps[0])
101 else:
102 task['depends_on'] = copy.deepcopy(deps)
103 task['commands'] = list(
104 itertools.chain(
105 self.pre_commands(),
106 self.main_commands(),
107 self.commands,
108 self.post_commands(),
109 )
110 )
111 if len(self.allowed_requesters) > 0:
112 task['allowed_requesters'] = self.allowed_requesters
113 return task
114
115
116 NamedTask = Task
117
118
119 class FuncTask(NamedTask):
120 def __init__(
121 self,
122 task_name: str,
123 functions: Iterable[str],
124 tags: Iterable[str] = (),
125 depends_on: Iterable[DependencySpec] = (),
126 exec_timeout_secs: int | None = None,
127 ):
128 commands = [func(func_name) for func_name in functions]
129 super().__init__(task_name, commands, tags=tags, depends_on=depends_on, exec_timeout_secs=exec_timeout_secs)
130 super(FuncTask, self).__init__(task_name, commands=commands)
131
132
133 class Prohibited(Exception):
134 pass
135
136
137 def require(rule: bool) -> None:
138 if not rule:
139 raise Prohibited()
140
141
142 def prohibit(rule: bool) -> None:
143 if rule:
144 raise Prohibited()
145
146
147 def both_or_neither(rule0: bool, rule1: bool) -> None:
148 if rule0:
149 require(rule1)
150 else:
151 prohibit(rule1)
152
153
154 class SettingsAccess:
155 def __init__(self, inst: 'MatrixTask') -> None:
156 self._task = inst
157
158 def __getattr__(self, __setting: str) -> str | bool:
159 return self._task.setting_value(__setting)
160
161
162 class MatrixTask(Task):
163 axes: ClassVar[Mapping[str, Sequence[str | bool]]] = OD()
164
165 def __init__(self, settings: Mapping[str, str | bool]):
166 super().__init__()
167 self._settings = {k: v for k, v in settings.items()}
168 for axis, options in type(self).axes.items():
169 if axis not in self._settings:
170 self._settings[axis] = options[0]
171
172 def display(self, axis_name: str) -> str:
173 value = self.setting_value(axis_name)
174 if value is False:
175 # E.g., if self.auth is False, return 'noauth'.
176 return f'no{axis_name}'
177 elif value is True:
178 return axis_name
179 else:
180 return value
181
182 def on_off(self, key: str, val: str) -> Literal['on', 'off']:
183 return 'on' if self.setting_value(key) == val else 'off'
184
185 @property
186 def name(self) -> str:
187 return '-'.join(self.name_parts())
188
189 def name_parts(self) -> Iterable[str]:
190 raise NotImplementedError
191
192 @property
193 def settings(self) -> SettingsAccess:
194 return SettingsAccess(self)
195
196 def setting_value(self, axis: str) -> str | bool:
197 assert axis in type(self).axes.keys(), (
198 f'Attempted to inspect setting "{axis}", which is not defined for this task type'
199 )
200 return self._settings[axis]
201
202 def setting_eq(self, axis: str, val: str | bool) -> bool:
203 current = self.setting_value(axis)
204 options = type(self).axes[axis]
205 assert val in options, (
206 f'Looking for value "{val}" on setting "{axis}", but that is not a supported option (Expects one of {options})'
207 )
208 return current == val
209
210 def is_valid_combination(self) -> bool:
211 try:
212 return self.do_is_valid_combination()
213 except Prohibited:
214 print(f'Ignoring invalid combination {self.name!r}')
215 return False
216
217 def do_is_valid_combination(self) -> bool:
218 return True
219
220 @classmethod
221 def matrix(cls):
222 for cell in product(*cls.axes.values()):
223 axis_values = dict(zip(cls.axes, cell))
224 task = cls(settings=axis_values)
225 if task.allowed:
226 yield task
227
228 @property
229 def allowed(self):
230 try:
231 return self.do_is_valid_combination()
232 except Prohibited:
233 return False