Files
firefox/taskcluster/taskgraph/types.py
T
Dustin J. Mitchell 6df2fabd03 Bug 1277417: output task information in JSON or just labels; r=ahal
The JSON output is suitable for processing with `jq` to extract features of
interest.

MozReview-Commit-ID: 5wpV7sXlOz3
2016-06-07 03:09:48 +00:00

87 lines
2.9 KiB
Python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import, print_function, unicode_literals
class Task(object):
"""
Representation of a task in a TaskGraph.
Each has, at creation:
- kind: Kind instance that created this task
- label; the label for this task
- attributes: a dictionary of attributes for this task (used for filtering)
- task: the task definition (JSON-able dictionary)
- extra: extra kind-specific metadata
And later, as the task-graph processing proceeds:
- optimization_key -- key for finding equivalent tasks in the TC index
- task_id -- TC taskId under which this task will be created
"""
def __init__(self, kind, label, attributes=None, task=None, **extra):
self.kind = kind
self.label = label
self.attributes = attributes or {}
self.task = task or {}
self.extra = extra
self.task_id = None
if not (all(isinstance(x, basestring) for x in self.attributes.iterkeys()) and
all(isinstance(x, basestring) for x in self.attributes.itervalues())):
raise TypeError("attribute names and values must be strings")
def __str__(self):
return "{} ({})".format(self.task_id or self.label,
self.task['metadata']['description'].strip())
class TaskGraph(object):
"""
Representation of a task graph.
A task graph is a combination of a Graph and a dictionary of tasks indexed
by label. TaskGraph instances should be treated as immutable.
"""
def __init__(self, tasks, graph):
assert set(tasks) == graph.nodes
self.tasks = tasks
self.graph = graph
def to_json(self):
"Return a JSON-able object representing the task graph, as documented"
named_links_dict = self.graph.named_links_dict()
# this dictionary may be keyed by label or by taskid, so let's just call it 'key'
tasks = {}
for key in self.graph.visit_postorder():
task = self.tasks[key]
task_json = {
'label': task.label,
'attributes': task.attributes,
'dependencies': named_links_dict.get(key, {}),
'task': task.task
}
if task.task_id:
task_json['task_id'] = task.task_id
tasks[key] = task_json
return tasks
def __getitem__(self, label):
"Get a task by label"
return self.tasks[label]
def __iter__(self):
"Iterate over tasks in undefined order"
return self.tasks.itervalues()
def __repr__(self):
return "<TaskGraph graph={!r} tasks={!r}>".format(self.graph, self.tasks)
def __eq__(self, other):
return self.tasks == other.tasks and self.graph == other.graph