| 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 |
from dataclasses import dataclass, field |
| 6 |
from typing import Any, Dict, List, Union |
| 7 |
|
| 8 |
|
| 9 |
@dataclass |
| 10 |
class Task: |
| 11 |
""" |
| 12 |
Representation of a task in a TaskGraph. Each Task has, at creation: |
| 13 |
|
| 14 |
- kind: the name of the task kind |
| 15 |
- label; the label for this task |
| 16 |
- attributes: a dictionary of attributes for this task (used for filtering) |
| 17 |
- task: the task definition (JSON-able dictionary) |
| 18 |
- optimization: optimization to apply to the task (see taskgraph.optimize) |
| 19 |
- dependencies: tasks this one depends on, in the form {name: label}, for example |
| 20 |
{'build': 'build-linux64/opt', 'docker-image': 'docker-image-desktop-test'} |
| 21 |
- soft_dependencies: tasks this one may depend on if they are available post |
| 22 |
optimisation. They are set as a list of tasks label. |
| 23 |
- if_dependencies: only run this task if at least one of these dependencies |
| 24 |
are present. |
| 25 |
|
| 26 |
And later, as the task-graph processing proceeds: |
| 27 |
|
| 28 |
- task_id -- TaskCluster taskId under which this task will be created |
| 29 |
|
| 30 |
This class is just a convenience wrapper for the data type and managing |
| 31 |
display, comparison, serialization, etc. It has no functionality of its own. |
| 32 |
""" |
| 33 |
|
| 34 |
kind: str |
| 35 |
label: str |
| 36 |
attributes: Dict |
| 37 |
task: Dict |
| 38 |
description: str = "" |
| 39 |
task_id: Union[str, None] = field(default=None, init=False) |
| 40 |
optimization: Union[Dict[str, Any], None] = field(default=None) |
| 41 |
dependencies: Dict = field(default_factory=dict) |
| 42 |
soft_dependencies: List = field(default_factory=list) |
| 43 |
if_dependencies: List = field(default_factory=list) |
| 44 |
|
| 45 |
def __post_init__(self): |
| 46 |
self.attributes["kind"] = self.kind |
| 47 |
|
| 48 |
def to_json(self): |
| 49 |
rv = { |
| 50 |
"kind": self.kind, |
| 51 |
"label": self.label, |
| 52 |
"description": self.description, |
| 53 |
"attributes": self.attributes, |
| 54 |
"dependencies": self.dependencies, |
| 55 |
"soft_dependencies": self.soft_dependencies, |
| 56 |
"if_dependencies": self.if_dependencies, |
| 57 |
"optimization": self.optimization, |
| 58 |
"task": self.task, |
| 59 |
} |
| 60 |
if self.task_id: |
| 61 |
rv["task_id"] = self.task_id |
| 62 |
return rv |
| 63 |
|
| 64 |
@classmethod |
| 65 |
def from_json(cls, task_dict): |
| 66 |
""" |
| 67 |
Given a data structure as produced by taskgraph.to_json, re-construct |
| 68 |
the original Task object. This is used to "resume" the task-graph |
| 69 |
generation process, for example in Action tasks. |
| 70 |
""" |
| 71 |
rv = cls( |
| 72 |
kind=task_dict["kind"], |
| 73 |
label=task_dict["label"], |
| 74 |
description=task_dict.get("description", ""), |
| 75 |
attributes=task_dict["attributes"], |
| 76 |
task=task_dict["task"], |
| 77 |
optimization=task_dict["optimization"], |
| 78 |
dependencies=task_dict.get("dependencies"), |
| 79 |
soft_dependencies=task_dict.get("soft_dependencies"), |
| 80 |
if_dependencies=task_dict.get("if_dependencies"), |
| 81 |
) |
| 82 |
if "task_id" in task_dict: |
| 83 |
rv.task_id = task_dict["task_id"] |
| 84 |
return rv |