Skip to content

Commit 8f36db4

Browse files
feat: more featureful set-name for from-deps (#484)
* Replace `set-name` with a more featureful version * style: pre-commit.ci auto fixes [...] --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6bf7be9 commit 8f36db4

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

src/taskgraph/transforms/from_deps.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from taskgraph.util.attributes import attrmatch
2121
from taskgraph.util.dependencies import GROUP_BY_MAP, get_dependencies
2222
from taskgraph.util.schema import Schema, validate_schema
23+
from taskgraph.util.set_name import SET_NAME_MAP
2324

2425
FROM_DEPS_SCHEMA = Schema(
2526
{
@@ -41,12 +42,14 @@
4142
"set-name",
4243
description=dedent(
4344
"""
44-
When True, `from_deps` will derive a name for the generated
45-
tasks from the name of the primary dependency. Defaults to
46-
True.
45+
UPDATE ME AND DOCS
4746
""".lstrip()
4847
),
49-
): bool,
48+
): Any(
49+
None,
50+
*SET_NAME_MAP,
51+
{Any(*SET_NAME_MAP): object},
52+
),
5053
Optional(
5154
"with-attributes",
5255
description=dedent(
@@ -170,7 +173,7 @@ def from_deps(config, tasks):
170173
groups = func(config, deps)
171174

172175
# Split the task, one per group.
173-
set_name = from_deps.get("set-name", True)
176+
set_name = from_deps.get("set-name", "strip-kind")
174177
copy_attributes = from_deps.get("copy-attributes", False)
175178
unique_kinds = from_deps.get("unique-kinds", True)
176179
fetches = from_deps.get("fetches", [])
@@ -203,10 +206,8 @@ def from_deps(config, tasks):
203206
primary_dep = [dep for dep in group if dep.kind == primary_kind][0]
204207

205208
if set_name:
206-
if primary_dep.label.startswith(primary_kind):
207-
new_task["name"] = primary_dep.label[len(primary_kind) + 1 :]
208-
else:
209-
new_task["name"] = primary_dep.label
209+
func = SET_NAME_MAP[set_name]
210+
new_task["name"] = func(config, deps, primary_dep, primary_kind)
210211

211212
if copy_attributes:
212213
attrs = new_task.setdefault("attributes", {})

src/taskgraph/util/set_name.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
# Define a collection of set_name functions
6+
# Note: this is stored here instead of where it is used in the `from_deps`
7+
# transform to give consumers a chance to register their own `set_name`
8+
# handlers before the `from_deps` schema is created.
9+
SET_NAME_MAP = {}
10+
11+
12+
def set_name(name, schema=None):
13+
def wrapper(func):
14+
assert (
15+
name not in SET_NAME_MAP
16+
), f"duplicate set_name function name {name} ({func} and {SET_NAME_MAP[name]})"
17+
SET_NAME_MAP[name] = func
18+
func.schema = schema
19+
return func
20+
21+
return wrapper
22+
23+
24+
@set_name("strip-kind")
25+
def set_name_strip_kind(config, tasks, primary_dep, primary_kind):
26+
if primary_dep.label.startswith(primary_kind):
27+
return primary_dep.label[len(primary_kind) + 1 :]
28+
else:
29+
return primary_dep.label
30+
31+
32+
@set_name("retain-kind")
33+
def set_name_retain_kind(config, tasks, primary_dep, primary_kind):
34+
return primary_dep.label

test/test_transforms_from_deps.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ def assert_dont_set_name(tasks):
9292
assert tasks[0]["name"] == "a-special-name"
9393

9494

95+
def assert_set_name_strip_kind(tasks):
96+
handle_exception(tasks)
97+
assert len(tasks) == 2
98+
assert tasks[0]["name"] == "a"
99+
assert tasks[1]["name"] == "b"
100+
101+
102+
def assert_set_name_retain_kind(tasks):
103+
handle_exception(tasks)
104+
assert len(tasks) == 2
105+
assert tasks[0]["name"] == "a"
106+
assert tasks[1]["name"] == "bar-b"
107+
108+
95109
def assert_group_by_all_with_fetch(tasks):
96110
handle_exception(tasks)
97111
assert len(tasks) == 1
@@ -165,7 +179,7 @@ def assert_group_by_all_with_fetch(tasks):
165179
"name": "a-special-name",
166180
"from-deps": {
167181
"group-by": "all",
168-
"set-name": False,
182+
"set-name": None,
169183
},
170184
},
171185
# kind config
@@ -174,6 +188,32 @@ def assert_group_by_all_with_fetch(tasks):
174188
None,
175189
id="dont_set_name",
176190
),
191+
pytest.param(
192+
# task
193+
{
194+
"from-deps": {
195+
"set-name": "strip-kind",
196+
},
197+
},
198+
# kind config
199+
None,
200+
# deps
201+
None,
202+
id="set_name_strip_kind",
203+
),
204+
pytest.param(
205+
# task
206+
{
207+
"from-deps": {
208+
"set-name": "retain-kind",
209+
},
210+
},
211+
# kind config
212+
None,
213+
# deps
214+
None,
215+
id="set_name_retain_kind",
216+
),
177217
pytest.param(
178218
# task
179219
{"from-deps": {}},

0 commit comments

Comments
 (0)