Skip to content

Commit 053d51e

Browse files
glandiumjcristau
authored andcommitted
Add a function to get tasks from multiple kinds in one pass
This is especially useful when some of the kinds the caller is interested in is a dependency of another kind.
1 parent a9d9a40 commit 053d51e

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/taskgraph/generator.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,19 +446,28 @@ def verify(self, name, *args, **kwargs):
446446
return name, args[0]
447447

448448

449-
def load_tasks_for_kind(parameters, kind, root_dir=None):
449+
def load_tasks_for_kinds(parameters, kinds, root_dir=None):
450450
"""
451-
Get all the tasks of a given kind.
451+
Get all the tasks of the given kinds.
452452
453453
This function is designed to be called from outside of taskgraph.
454454
"""
455455
# make parameters read-write
456456
parameters = dict(parameters)
457-
parameters["target-kinds"] = [kind]
457+
parameters["target-kinds"] = kinds
458458
parameters = parameters_loader(spec=None, strict=False, overrides=parameters)
459459
tgg = TaskGraphGenerator(root_dir=root_dir, parameters=parameters)
460460
return {
461461
task.task["metadata"]["name"]: task
462462
for task in tgg.full_task_set
463-
if task.kind == kind
463+
if task.kind in kinds
464464
}
465+
466+
467+
def load_tasks_for_kind(parameters, kind, root_dir=None):
468+
"""
469+
Get all the tasks of a given kind.
470+
471+
This function is designed to be called from outside of taskgraph.
472+
"""
473+
return load_tasks_for_kinds(parameters, [kind], root_dir)

test/test_generator.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pytest_taskgraph import FakeKind, WithFakeKind, fake_load_graph_config
88

99
from taskgraph import generator, graph
10-
from taskgraph.generator import Kind, load_tasks_for_kind
10+
from taskgraph.generator import Kind, load_tasks_for_kind, load_tasks_for_kinds
1111
from taskgraph.loader.default import loader as default_loader
1212

1313

@@ -184,6 +184,21 @@ def test_load_tasks_for_kind(monkeypatch):
184184
"_example-kind",
185185
"/root",
186186
)
187+
assert "docker-image-t-1" not in tasks
188+
assert (
189+
"_example-kind-t-1" in tasks
190+
and tasks["_example-kind-t-1"].label == "_example-kind-t-1"
191+
)
192+
193+
tasks = load_tasks_for_kinds(
194+
{"_kinds": [("_example-kind", []), ("docker-image", [])]},
195+
["_example-kind", "docker-image"],
196+
"/root",
197+
)
198+
assert (
199+
"docker-image-t-1" in tasks
200+
and tasks["docker-image-t-0"].label == "docker-image-t-0"
201+
)
187202
assert (
188203
"_example-kind-t-1" in tasks
189204
and tasks["_example-kind-t-1"].label == "_example-kind-t-1"

0 commit comments

Comments
 (0)