| 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 split one task definition into many tasks, governed by a |
| 7 |
matrix defined in the definition. |
| 8 |
""" |
| 9 |
|
| 10 |
from copy import deepcopy |
| 11 |
from textwrap import dedent |
| 12 |
|
| 13 |
from voluptuous import Extra, Optional, Required |
| 14 |
|
| 15 |
from taskgraph.transforms.base import TransformSequence |
| 16 |
from taskgraph.util.schema import Schema |
| 17 |
from taskgraph.util.templates import substitute_task_fields |
| 18 |
|
| 19 |
MATRIX_SCHEMA = Schema( |
| 20 |
{ |
| 21 |
Required("name"): str, |
| 22 |
Optional("matrix"): { |
| 23 |
Optional( |
| 24 |
"exclude", |
| 25 |
description=dedent( |
| 26 |
""" |
| 27 |
Exclude the specified combination(s) of matrix values from the |
| 28 |
final list of tasks. |
| 29 |
|
| 30 |
If only a subset of the possible rows are present in the |
| 31 |
exclusion rule, then *all* combinations including that subset |
| 32 |
subset will be excluded. |
| 33 |
""".lstrip() |
| 34 |
), |
| 35 |
): [{str: str}], |
| 36 |
Optional( |
| 37 |
"set-name", |
| 38 |
description=dedent( |
| 39 |
""" |
| 40 |
Sets the task name to the specified format string. |
| 41 |
|
| 42 |
Useful for cases where the default of joining matrix values by |
| 43 |
a dash is not desired. |
| 44 |
""".lstrip() |
| 45 |
), |
| 46 |
): str, |
| 47 |
Optional( |
| 48 |
"substitution-fields", |
| 49 |
description=dedent( |
| 50 |
""" |
| 51 |
List of fields in the task definition to substitute matrix values into. |
| 52 |
|
| 53 |
If not specified, all fields in the task definition will be |
| 54 |
substituted. |
| 55 |
""" |
| 56 |
), |
| 57 |
): [str], |
| 58 |
Extra: [str], |
| 59 |
}, |
| 60 |
Extra: object, |
| 61 |
}, |
| 62 |
) |
| 63 |
"""Schema for matrix transforms.""" |
| 64 |
|
| 65 |
transforms = TransformSequence() |
| 66 |
transforms.add_validate(MATRIX_SCHEMA) |
| 67 |
|
| 68 |
|
| 69 |
def _resolve_matrix(tasks, key, values, exclude): |
| 70 |
for task in tasks: |
| 71 |
for value in values: |
| 72 |
new_task = deepcopy(task) |
| 73 |
new_task["name"] = f"{new_task['name']}-{value}" |
| 74 |
|
| 75 |
matrix = new_task.setdefault("attributes", {}).setdefault("matrix", {}) |
| 76 |
matrix[key] = value |
| 77 |
|
| 78 |
for rule in exclude: |
| 79 |
if all(matrix.get(k) == v for k, v in rule.items()): |
| 80 |
break |
| 81 |
else: |
| 82 |
yield new_task |
| 83 |
|
| 84 |
|
| 85 |
@transforms.add |
| 86 |
def split_matrix(config, tasks): |
| 87 |
for task in tasks: |
| 88 |
if "matrix" not in task: |
| 89 |
yield task |
| 90 |
continue |
| 91 |
|
| 92 |
matrix = task.pop("matrix") |
| 93 |
set_name = matrix.pop("set-name", None) |
| 94 |
fields = matrix.pop("substitution-fields", task.keys()) |
| 95 |
exclude = matrix.pop("exclude", {}) |
| 96 |
|
| 97 |
new_tasks = [task] |
| 98 |
for key, values in matrix.items(): |
| 99 |
new_tasks = _resolve_matrix(new_tasks, key, values, exclude) |
| 100 |
|
| 101 |
for new_task in new_tasks: |
| 102 |
if set_name: |
| 103 |
if "name" not in fields: |
| 104 |
fields.append("name") |
| 105 |
new_task["name"] = set_name |
| 106 |
|
| 107 |
substitute_task_fields( |
| 108 |
new_task, |
| 109 |
fields, |
| 110 |
matrix=new_task["attributes"]["matrix"], |
| 111 |
) |
| 112 |
yield new_task |