| 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 |
import logging |
| 7 |
|
| 8 |
from taskgraph.util.templates import merge |
| 9 |
from taskgraph.util.yaml import load_yaml |
| 10 |
|
| 11 |
logger = logging.getLogger(__name__) |
| 12 |
|
| 13 |
|
| 14 |
def loader(kind, path, config, params, loaded_tasks): |
| 15 |
""" |
| 16 |
Get the input elements that will be transformed into tasks in a generic |
| 17 |
way. The elements themselves are free-form, and become the input to the |
| 18 |
first transform. |
| 19 |
|
| 20 |
By default, this reads tasks from the `tasks` key, or from yaml files |
| 21 |
named by `tasks-from`. The entities are read from mappings, and the |
| 22 |
keys to those mappings are added in the `name` key of each entity. |
| 23 |
|
| 24 |
If there is a `task-defaults` config, then every task is merged with it. |
| 25 |
This provides a simple way to set default values for all tasks of a kind. |
| 26 |
The `task-defaults` key can also be specified in a yaml file pointed to by |
| 27 |
`tasks-from`. In this case it will only apply to tasks defined in the same |
| 28 |
file. |
| 29 |
|
| 30 |
Other kind implementations can use a different loader function to |
| 31 |
produce inputs and hand them to `transform_inputs`. |
| 32 |
""" |
| 33 |
|
| 34 |
def generate_tasks(): |
| 35 |
defaults = config.get("task-defaults") |
| 36 |
for name, task in config.get("tasks", {}).items(): |
| 37 |
if defaults: |
| 38 |
task = merge(defaults, task) |
| 39 |
task["task-from"] = "kind.yml" |
| 40 |
yield name, task |
| 41 |
|
| 42 |
for filename in config.get("tasks-from", []): |
| 43 |
tasks = load_yaml(path, filename) |
| 44 |
|
| 45 |
file_defaults = tasks.pop("task-defaults", None) |
| 46 |
if defaults: |
| 47 |
file_defaults = merge(defaults, file_defaults or {}) |
| 48 |
|
| 49 |
for name, task in tasks.items(): |
| 50 |
if file_defaults: |
| 51 |
task = merge(file_defaults, task) |
| 52 |
task["task-from"] = filename |
| 53 |
yield name, task |
| 54 |
|
| 55 |
for name, task in generate_tasks(): |
| 56 |
task["name"] = name |
| 57 |
logger.debug(f"Generating tasks for {kind} {name}") |
| 58 |
yield task |