| 1 |
from textwrap import dedent |
| 2 |
|
| 3 |
from voluptuous import ALLOW_EXTRA, Any, Optional, Required |
| 4 |
|
| 5 |
from taskgraph.transforms.base import TransformSequence |
| 6 |
from taskgraph.util.schema import Schema |
| 7 |
from taskgraph.util.templates import deep_get, substitute_task_fields |
| 8 |
from taskgraph.util.yaml import load_yaml |
| 9 |
|
| 10 |
SCHEMA = Schema( |
| 11 |
{ |
| 12 |
Optional("name"): str, |
| 13 |
Required( |
| 14 |
"task-context", |
| 15 |
description=dedent( |
| 16 |
""" |
| 17 |
`task-context` can be used to substitute values into any field in a |
| 18 |
task with data that is not known until `taskgraph` runs. |
| 19 |
|
| 20 |
This data can be provided via `from-parameters` or `from-file`, |
| 21 |
which can pull in values from parameters and a defined yml file |
| 22 |
respectively. |
| 23 |
|
| 24 |
Data may also be provided directly in the `from-object` section of |
| 25 |
`task-context`. This can be useful in `kinds` that define most of |
| 26 |
their contents in `task-defaults`, but have some values that may |
| 27 |
differ for various concrete `tasks` in the `kind`. |
| 28 |
|
| 29 |
If the same key is found in multiple places the order of precedence |
| 30 |
is as follows: |
| 31 |
- Parameters |
| 32 |
- `from-object` keys |
| 33 |
- File |
| 34 |
|
| 35 |
That is to say: parameters will always override anything else. |
| 36 |
|
| 37 |
""".lstrip(), |
| 38 |
), |
| 39 |
): { |
| 40 |
Optional( |
| 41 |
"from-parameters", |
| 42 |
description=dedent( |
| 43 |
""" |
| 44 |
Retrieve task context values from parameters. A single |
| 45 |
parameter may be provided or a list of parameters in |
| 46 |
priority order. The latter can be useful in implementing a |
| 47 |
"default" value if some other parameter is not provided. |
| 48 |
""".lstrip() |
| 49 |
), |
| 50 |
): {str: Any([str], str)}, |
| 51 |
Optional( |
| 52 |
"from-file", |
| 53 |
description=dedent( |
| 54 |
""" |
| 55 |
Retrieve task context values from a yaml file. The provided |
| 56 |
file should usually only contain top level keys and values |
| 57 |
(eg: nested objects will not be interpolated - they will be |
| 58 |
substituted as text representations of the object). |
| 59 |
""".lstrip() |
| 60 |
), |
| 61 |
): str, |
| 62 |
Optional( |
| 63 |
"from-object", |
| 64 |
description="Key/value pairs to be used as task context", |
| 65 |
): object, |
| 66 |
Required( |
| 67 |
"substitution-fields", |
| 68 |
description=dedent( |
| 69 |
""" |
| 70 |
A list of fields in the task to substitute the provided values |
| 71 |
into. |
| 72 |
""".lstrip() |
| 73 |
), |
| 74 |
): [str], |
| 75 |
}, |
| 76 |
}, |
| 77 |
extra=ALLOW_EXTRA, |
| 78 |
) |
| 79 |
|
| 80 |
transforms = TransformSequence() |
| 81 |
transforms.add_validate(SCHEMA) |
| 82 |
|
| 83 |
|
| 84 |
@transforms.add |
| 85 |
def render_task(config, tasks): |
| 86 |
for task in tasks: |
| 87 |
sub_config = task.pop("task-context") |
| 88 |
params_context = {} |
| 89 |
for var, path in sub_config.pop("from-parameters", {}).items(): |
| 90 |
if isinstance(path, str): |
| 91 |
params_context[var] = deep_get(config.params, path) |
| 92 |
else: |
| 93 |
for choice in path: |
| 94 |
value = deep_get(config.params, choice) |
| 95 |
if value is not None: |
| 96 |
params_context[var] = value |
| 97 |
break |
| 98 |
|
| 99 |
file_context = {} |
| 100 |
from_file = sub_config.pop("from-file", None) |
| 101 |
if from_file: |
| 102 |
file_context = load_yaml(from_file) |
| 103 |
|
| 104 |
fields = sub_config.pop("substitution-fields") |
| 105 |
|
| 106 |
subs = {} |
| 107 |
subs.update(file_context) |
| 108 |
# We've popped away the configuration; everything left in `sub_config` is |
| 109 |
# substitution key/value pairs. |
| 110 |
subs.update(sub_config.pop("from-object", {})) |
| 111 |
subs.update(params_context) |
| 112 |
if "name" in task: |
| 113 |
subs.setdefault("name", task["name"]) |
| 114 |
|
| 115 |
# Now that we have our combined context, we can substitute. |
| 116 |
substitute_task_fields(task, fields, **subs) |
| 117 |
yield task |