Debian Codegraph @

third_party/python/taskcluster_taskgraph/taskgraph/transforms/cached_tasks.py

raw
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 from collections import deque
7
8 import taskgraph
9 from taskgraph.transforms.base import TransformSequence
10 from taskgraph.util.cached_tasks import add_optimization
11
12 transforms = TransformSequence()
13
14
15 def order_tasks(config, tasks):
16 """Iterate image tasks in an order where parent tasks come first."""
17 kind_prefix = config.kind + "-"
18
19 pending = deque(tasks)
20 task_labels = {task["label"] for task in pending}
21 emitted = set()
22 while True:
23 try:
24 task = pending.popleft()
25 except IndexError:
26 break
27 parents = {
28 task
29 for task in task.get("dependencies", {}).values()
30 if task.startswith(kind_prefix)
31 }
32 if parents and not emitted.issuperset(parents & task_labels):
33 pending.append(task)
34 continue
35 emitted.add(task["label"])
36 yield task
37
38
39 def format_task_digest(cached_task):
40 return "/".join(
41 [
42 cached_task["type"],
43 cached_task["name"],
44 cached_task["digest"],
45 ]
46 )
47
48
49 @transforms.add
50 def cache_task(config, tasks):
51 if taskgraph.fast:
52 for task in tasks:
53 yield task
54 return
55
56 digests = {}
57 for task in config.kind_dependencies_tasks.values():
58 if "cached_task" in task.attributes:
59 digests[task.label] = format_task_digest(task.attributes["cached_task"])
60
61 for task in order_tasks(config, tasks):
62 cache = task.pop("cache", None)
63 if cache is None:
64 yield task
65 continue
66
67 dependency_digests = []
68 for p in task.get("dependencies", {}).values():
69 if p in digests:
70 dependency_digests.append(digests[p])
71 else:
72 raise Exception(
73 "Cached task {} has uncached parent task: {}".format(
74 task["label"], p
75 )
76 )
77
78 digest_data = cache["digest-data"] + sorted(dependency_digests)
79
80 # Chain of trust affects task artifacts therefore it should influence
81 # cache digest.
82 if task.get("worker", {}).get("chain-of-trust"):
83 digest_data.append(str(task["worker"]["chain-of-trust"]))
84
85 add_optimization(
86 config,
87 task,
88 cache_type=cache["type"],
89 cache_name=cache["name"],
90 digest_data=digest_data,
91 )
92 digests[task["label"]] = format_task_digest(task["attributes"]["cached_task"])
93
94 yield task