Debian Codegraph @

third_party/python/taskcluster_taskgraph/taskgraph/transforms/from_deps.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 """
6 Transforms used to create tasks based on the kind dependencies, filtering on
7 common attributes like the ``build-type``.
8
9 These transforms are useful when follow-up tasks are needed for some
10 indeterminate subset of existing tasks. For example, running a signing task
11 after each build task, whatever builds may exist.
12 """
13
14 from copy import deepcopy
15 from textwrap import dedent
16
17 from voluptuous import Any, Extra, Optional, Required
18
19 from taskgraph.transforms.base import TransformSequence
20 from taskgraph.transforms.run import fetches_schema
21 from taskgraph.util.attributes import attrmatch
22 from taskgraph.util.dependencies import GROUP_BY_MAP, get_dependencies
23 from taskgraph.util.schema import Schema, validate_schema
24 from taskgraph.util.set_name import SET_NAME_MAP
25
26 FROM_DEPS_SCHEMA = Schema(
27 {
28 Required("from-deps"): {
29 Optional(
30 "kinds",
31 description=dedent(
32 """
33 Limit dependencies to specified kinds (defaults to all kinds in
34 `kind-dependencies`).
35
36 The first kind in the list is the "primary" kind. The
37 dependency of this kind will be used to derive the label
38 and copy attributes (if `copy-attributes` is True).
39 """.lstrip()
40 ),
41 ): list,
42 Optional(
43 "set-name",
44 description=dedent(
45 """
46 UPDATE ME AND DOCS
47 """.lstrip()
48 ),
49 ): Any(
50 None,
51 False,
52 *SET_NAME_MAP,
53 {Any(*SET_NAME_MAP): object},
54 ),
55 Optional(
56 "with-attributes",
57 description=dedent(
58 """
59 Limit dependencies to tasks whose attributes match
60 using :func:`~taskgraph.util.attributes.attrmatch`.
61 """.lstrip()
62 ),
63 ): {str: Any(list, str)},
64 Optional(
65 "group-by",
66 description=dedent(
67 """
68 Group cross-kind dependencies using the given group-by
69 function. One task will be created for each group. If not
70 specified, the 'single' function will be used which creates
71 a new task for each individual dependency.
72 """.lstrip()
73 ),
74 ): Any(
75 None,
76 *GROUP_BY_MAP,
77 {Any(*GROUP_BY_MAP): object},
78 ),
79 Optional(
80 "copy-attributes",
81 description=dedent(
82 """
83 If True, copy attributes from the dependency matching the
84 first kind in the `kinds` list (whether specified explicitly
85 or taken from `kind-dependencies`).
86 """.lstrip()
87 ),
88 ): bool,
89 Optional(
90 "unique-kinds",
91 description=dedent(
92 """
93 If true (the default), there must be only a single unique task
94 for each kind in a dependency group. Setting this to false
95 disables that requirement.
96 """.lstrip()
97 ),
98 ): bool,
99 Optional(
100 "fetches",
101 description=dedent(
102 """
103 If present, a `fetches` entry will be added for each task
104 dependency. Attributes of the upstream task may be used as
105 substitution values in the `artifact` or `dest` values of the
106 `fetches` entry.
107 """.lstrip()
108 ),
109 ): {str: [fetches_schema]},
110 },
111 Extra: object,
112 },
113 )
114 """Schema for from_deps transforms."""
115
116 transforms = TransformSequence()
117 transforms.add_validate(FROM_DEPS_SCHEMA)
118
119
120 @transforms.add
121 def from_deps(config, tasks):
122 for task in tasks:
123 # Setup and error handling.
124 from_deps = task.pop("from-deps")
125 kind_deps = config.config.get("kind-dependencies", [])
126 kinds = from_deps.get("kinds", kind_deps)
127
128 invalid = set(kinds) - set(kind_deps)
129 if invalid:
130 invalid = "\n".join(sorted(invalid))
131 raise Exception(
132 dedent(
133 f"""
134 The `from-deps.kinds` key contains the following kinds
135 that are not defined in `kind-dependencies`:
136 {invalid}
137 """.lstrip()
138 )
139 )
140
141 if not kinds:
142 raise Exception(
143 dedent(
144 """
145 The `from_deps` transforms require at least one kind defined
146 in `kind-dependencies`!
147 """.lstrip()
148 )
149 )
150
151 # Resolve desired dependencies.
152 with_attributes = from_deps.get("with-attributes")
153 deps = [
154 task
155 for task in config.kind_dependencies_tasks.values()
156 if task.kind in kinds
157 if not with_attributes or attrmatch(task.attributes, **with_attributes)
158 ]
159
160 # Resolve groups.
161 group_by = from_deps.get("group-by", "single")
162 groups = set()
163
164 if isinstance(group_by, dict):
165 assert len(group_by) == 1
166 group_by, arg = group_by.popitem()
167 func = GROUP_BY_MAP[group_by]
168 if func.schema:
169 validate_schema(
170 func.schema, arg, f"Invalid group-by {group_by} argument"
171 )
172 groups = func(config, deps, arg)
173 else:
174 func = GROUP_BY_MAP[group_by]
175 groups = func(config, deps)
176
177 # Split the task, one per group.
178 set_name = from_deps.get("set-name", "strip-kind")
179 copy_attributes = from_deps.get("copy-attributes", False)
180 unique_kinds = from_deps.get("unique-kinds", True)
181 fetches = from_deps.get("fetches", [])
182 for group in groups:
183 # Verify there is only one task per kind in each group.
184 group_kinds = {t.kind for t in group}
185 if unique_kinds and len(group_kinds) < len(group):
186 raise Exception(
187 "The from_deps transforms only allow a single task per kind in a group!"
188 )
189
190 new_task = deepcopy(task)
191 new_task.setdefault("dependencies", {})
192 new_task["dependencies"].update(
193 {dep.kind if unique_kinds else dep.label: dep.label for dep in group}
194 )
195
196 # Set name and copy attributes from the primary kind.
197 for kind in kinds:
198 if kind in group_kinds:
199 primary_kind = kind
200 break
201 else:
202 raise Exception("Could not detect primary kind!")
203
204 new_task.setdefault("attributes", {})["primary-kind-dependency"] = (
205 primary_kind
206 )
207
208 primary_dep = [dep for dep in group if dep.kind == primary_kind][0]
209
210 if set_name:
211 func = SET_NAME_MAP[set_name]
212 new_task["name"] = func(config, deps, primary_dep, primary_kind)
213
214 if copy_attributes:
215 attrs = new_task.setdefault("attributes", {})
216 new_task["attributes"] = primary_dep.attributes.copy()
217 new_task["attributes"].update(attrs)
218
219 if fetches:
220 task_fetches = new_task.setdefault("fetches", {})
221
222 for dep_task in get_dependencies(config, new_task):
223 # Nothing to do if this kind has no fetches listed
224 if dep_task.kind not in fetches:
225 continue
226
227 fetches_from_dep = []
228 for kind, kind_fetches in fetches.items():
229 if kind != dep_task.kind:
230 continue
231
232 for fetch in kind_fetches:
233 entry = fetch.copy()
234 entry["artifact"] = entry["artifact"].format(
235 **dep_task.attributes
236 )
237 if "dest" in entry:
238 entry["dest"] = entry["dest"].format(
239 **dep_task.attributes
240 )
241 fetches_from_dep.append(entry)
242
243 task_fetches[dep_task.label] = fetches_from_dep
244
245 yield new_task