Skip to content

Commit 2014a29

Browse files
committed
refactor: use Schema.from_dict in run transforms
1 parent 0626c37 commit 2014a29

4 files changed

Lines changed: 98 additions & 80 deletions

File tree

‎src/taskgraph/transforms/run/__init__.py‎

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,36 @@
3333
# Fetches may be accepted in other transforms and eventually passed along
3434
# to a `task` (eg: from_deps). Defining this here allows them to reuse
3535
# the schema and avoid duplication.
36-
class FetchesEntrySchema(Schema):
37-
artifact: str
38-
dest: Optional[str] = None
39-
extract: Optional[bool] = None
40-
verify_hash: Optional[bool] = None
41-
42-
43-
class WhenConfig(Schema):
44-
# This task only needs to be run if a file matching one of the given
45-
# patterns has changed in the push. The patterns use the mozpack
46-
# match function (python/mozbuild/mozpack/path.py).
47-
files_changed: Optional[list[str]] = None
36+
FetchesEntrySchema = Schema.from_dict(
37+
{
38+
"artifact": str,
39+
"dest": Optional[str],
40+
"extract": Optional[bool],
41+
"verify-hash": Optional[bool],
42+
},
43+
name="FetchesEntrySchema",
44+
)
4845

46+
WhenConfig = Schema.from_dict(
47+
{
48+
# This task only needs to be run if a file matching one of the given
49+
# patterns has changed in the push. The patterns use the mozpack
50+
# match function (python/mozbuild/mozpack/path.py).
51+
"files-changed": Optional[list[str]],
52+
},
53+
name="WhenConfig",
54+
)
4955

50-
class RunConfig(Schema, forbid_unknown_fields=False, kw_only=True):
51-
# The key to a run implementation in a peer module to this one.
52-
using: str
53-
# Base work directory used to set up the task.
54-
workdir: Optional[str] = None
56+
RunConfig = Schema.from_dict(
57+
{
58+
# The key to a run implementation in a peer module to this one.
59+
"using": str,
60+
# Base work directory used to set up the task.
61+
"workdir": Optional[str],
62+
},
63+
name="RunConfig",
64+
forbid_unknown_fields=False,
65+
)
5566

5667

5768
#: Schema for a run transforms
@@ -405,8 +416,10 @@ def wrap(func):
405416
return wrap
406417

407418

408-
class AlwaysOptimizedRunSchema(Schema):
409-
using: Literal["always-optimized"]
419+
AlwaysOptimizedRunSchema = Schema.from_dict(
420+
{"using": Literal["always-optimized"]},
421+
name="AlwaysOptimizedRunSchema",
422+
)
410423

411424

412425
@run_task_using("always-optimized", "always-optimized", AlwaysOptimizedRunSchema)

‎src/taskgraph/transforms/run/index_search.py‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919

2020
#: Schema for run.using index-search
21-
class IndexSearchRunSchema(Schema):
22-
using: Literal["index-search"]
23-
# A list of indexes in decreasing order of priority at which to lookup for this
24-
# task. This is interpolated with the graph parameters.
25-
index_search: list[str]
26-
27-
28-
run_task_schema = IndexSearchRunSchema
21+
run_task_schema = Schema.from_dict(
22+
{
23+
"using": Literal["index-search"],
24+
# A list of indexes in decreasing order of priority at which to lookup for this
25+
# task. This is interpolated with the graph parameters.
26+
"index-search": list[str],
27+
},
28+
name="IndexSearchRunSchema",
29+
)
2930

3031

3132
@run_task_using("always-optimized", "index-search", schema=run_task_schema)

‎src/taskgraph/transforms/run/run_task.py‎

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,38 @@
2727

2828

2929
#: Schema for run.using run_task
30-
class RunTaskRunSchema(Schema, forbid_unknown_fields=False, kw_only=True):
31-
# Specifies the task type. Must be 'run-task'.
32-
using: Literal["run-task"]
33-
# The command arguments to pass to the `run-task` script, after the checkout
34-
# arguments. If a list, it will be passed directly; otherwise it will be
35-
# included in a single argument to the command specified by `exec-with`.
36-
command: Union[list[taskref_or_string_msgspec], taskref_or_string_msgspec]
37-
# If true (the default), perform a checkout on the worker. Can also be a
38-
# dictionary specifying explicit checkouts.
39-
checkout: Union[bool, dict[str, dict]]
40-
# Base work directory used to set up the task.
41-
workdir: str
42-
# Specifies which caches to use. May take a boolean in which case either all
43-
# (True) or no (False) caches will be used. Alternatively, it can accept a
44-
# list of caches to enable. Defaults to only the checkout cache enabled.
45-
use_caches: Optional[Union[bool, list[CacheType]]] = None
46-
# Path to run command in. If a checkout is present, the path to the checkout
47-
# will be interpolated with the key `checkout`.
48-
cwd: Optional[str] = None
49-
# Specifies what to execute the command with in the event the command is a
50-
# string.
51-
exec_with: Optional[ExecWith] = None
52-
# Command used to invoke the `run-task` script. Can be used if the script
53-
# or Python installation is in a non-standard location on the workers.
54-
run_task_command: Optional[list] = None
55-
# Whether to run as root. Defaults to False.
56-
run_as_root: Optional[bool] = None
57-
58-
59-
run_task_schema = RunTaskRunSchema
30+
run_task_schema = Schema.from_dict(
31+
{
32+
# Specifies the task type. Must be 'run-task'.
33+
"using": Literal["run-task"],
34+
# The command arguments to pass to the `run-task` script, after the checkout
35+
# arguments. If a list, it will be passed directly; otherwise it will be
36+
# included in a single argument to the command specified by `exec-with`.
37+
"command": Union[list[taskref_or_string_msgspec], taskref_or_string_msgspec],
38+
# If true (the default), perform a checkout on the worker. Can also be a
39+
# dictionary specifying explicit checkouts.
40+
"checkout": Union[bool, dict[str, dict]],
41+
# Base work directory used to set up the task.
42+
"workdir": str,
43+
# Specifies which caches to use. May take a boolean in which case either all
44+
# (True) or no (False) caches will be used. Alternatively, it can accept a
45+
# list of caches to enable. Defaults to only the checkout cache enabled.
46+
"use-caches": Optional[Union[bool, list[CacheType]]],
47+
# Path to run command in. If a checkout is present, the path to the checkout
48+
# will be interpolated with the key `checkout`.
49+
"cwd": Optional[str],
50+
# Specifies what to execute the command with in the event the command is a
51+
# string.
52+
"exec-with": Optional[ExecWith],
53+
# Command used to invoke the `run-task` script. Can be used if the script
54+
# or Python installation is in a non-standard location on the workers.
55+
"run-task-command": Optional[list],
56+
# Whether to run as root. Defaults to False.
57+
"run-as-root": Optional[bool],
58+
},
59+
name="RunTaskRunSchema",
60+
forbid_unknown_fields=False,
61+
)
6062

6163

6264
def common_setup(config, task, taskdesc, command):

‎src/taskgraph/transforms/run/toolchain.py‎

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,31 @@
2323

2424

2525
#: Schema for run.using toolchain
26-
class ToolchainRunSchema(Schema, forbid_unknown_fields=False, kw_only=True):
27-
# Specifies the run type. Must be "toolchain-script".
28-
using: Literal["toolchain-script"]
29-
# The script (in taskcluster/scripts/misc) to run.
30-
script: str
31-
# Path to the artifact produced by the toolchain task.
32-
toolchain_artifact: str
33-
# Base work directory used to set up the task.
34-
workdir: str
35-
# Arguments to pass to the script.
36-
arguments: Optional[list[str]] = None
37-
# Paths/patterns pointing to files that influence the outcome of
38-
# a toolchain build.
39-
resources: Optional[list[str]] = None
40-
# An alias that can be used instead of the real toolchain task name in
41-
# fetch stanzas for tasks.
42-
toolchain_alias: Optional[Union[str, list[str]]] = None
43-
# Additional env variables to add to the worker when using this
44-
# toolchain.
45-
toolchain_env: Optional[dict[str, object]] = None
46-
47-
48-
toolchain_run_schema = ToolchainRunSchema
26+
toolchain_run_schema = Schema.from_dict(
27+
{
28+
# Specifies the run type. Must be "toolchain-script".
29+
"using": Literal["toolchain-script"],
30+
# The script (in taskcluster/scripts/misc) to run.
31+
"script": str,
32+
# Path to the artifact produced by the toolchain task.
33+
"toolchain-artifact": str,
34+
# Base work directory used to set up the task.
35+
"workdir": str,
36+
# Arguments to pass to the script.
37+
"arguments": Optional[list[str]],
38+
# Paths/patterns pointing to files that influence the outcome of
39+
# a toolchain build.
40+
"resources": Optional[list[str]],
41+
# An alias that can be used instead of the real toolchain task name in
42+
# fetch stanzas for tasks.
43+
"toolchain-alias": Optional[Union[str, list[str]]],
44+
# Additional env variables to add to the worker when using this
45+
# toolchain.
46+
"toolchain-env": Optional[dict[str, object]],
47+
},
48+
name="ToolchainRunSchema",
49+
forbid_unknown_fields=False,
50+
)
4951

5052

5153
def get_digest_data(config, run, taskdesc):

0 commit comments

Comments
 (0)