|
5 | 5 | import json |
6 | 6 | import sys |
7 | 7 | from contextlib import suppress |
8 | | -from pathlib import Path |
9 | 8 | from typing import TYPE_CHECKING |
10 | 9 | from typing import Any |
11 | 10 | from typing import Literal |
|
16 | 15 | from _pytask.capture_utils import CaptureMethod |
17 | 16 | from _pytask.capture_utils import ShowCapture |
18 | 17 | from _pytask.click import ColoredCommand |
19 | | -from _pytask.config_utils import find_project_root_and_config |
20 | | -from _pytask.config_utils import read_config |
| 18 | +from _pytask.config_utils import normalize_programmatic_config |
21 | 19 | from _pytask.console import console |
22 | 20 | from _pytask.dag import create_dag |
23 | 21 | from _pytask.exceptions import CollectionError |
|
30 | 28 | from _pytask.pluginmanager import hookimpl |
31 | 29 | from _pytask.pluginmanager import storage |
32 | 30 | from _pytask.session import Session |
33 | | -from _pytask.shared import parse_paths |
34 | | -from _pytask.shared import to_list |
35 | 31 | from _pytask.traceback import Traceback |
36 | 32 |
|
37 | 33 | if TYPE_CHECKING: |
38 | 34 | from collections.abc import Callable |
39 | 35 | from collections.abc import Iterable |
| 36 | + from pathlib import Path |
40 | 37 | from typing import NoReturn |
41 | 38 |
|
42 | 39 | from _pytask.node_protocols import PTask |
@@ -66,7 +63,7 @@ def pytask_unconfigure(session: Session) -> None: |
66 | 63 | path.write_text(json.dumps(HashPathCache._cache)) |
67 | 64 |
|
68 | 65 |
|
69 | | -def build( # noqa: C901, PLR0912, PLR0913, PLR0915 |
| 66 | +def build( # noqa: PLR0913 |
70 | 67 | *, |
71 | 68 | capture: Literal["fd", "no", "sys", "tee-sys"] | CaptureMethod = CaptureMethod.FD, |
72 | 69 | check_casing_of_paths: bool = True, |
@@ -225,47 +222,14 @@ def build( # noqa: C901, PLR0912, PLR0913, PLR0915 |
225 | 222 |
|
226 | 223 | # If someone called the programmatic interface, we need to do some parsing. |
227 | 224 | if "command" not in raw_config: |
228 | | - raw_config["command"] = "build" |
229 | 225 | # Add defaults from cli. |
230 | 226 | from _pytask.cli import DEFAULTS_FROM_CLI # noqa: PLC0415 |
231 | 227 |
|
232 | | - raw_config = {**DEFAULTS_FROM_CLI, **raw_config} |
233 | | - |
234 | | - paths_value = raw_config["paths"] |
235 | | - # Convert tuple to list since parse_paths expects Path | list[Path] |
236 | | - if isinstance(paths_value, tuple): |
237 | | - paths_value = list(paths_value) |
238 | | - if not isinstance(paths_value, (Path, list)): |
239 | | - msg = f"paths must be Path or list, got {type(paths_value)}" |
240 | | - raise TypeError(msg) # noqa: TRY301 |
241 | | - # Cast is justified - we validated at runtime |
242 | | - raw_config["paths"] = parse_paths(cast("Path | list[Path]", paths_value)) |
243 | | - |
244 | | - if raw_config["config"] is not None: |
245 | | - config_value = raw_config["config"] |
246 | | - if not isinstance(config_value, (str, Path)): |
247 | | - msg = f"config must be str or Path, got {type(config_value)}" |
248 | | - raise TypeError(msg) # noqa: TRY301 |
249 | | - raw_config["config"] = Path(config_value).resolve() |
250 | | - raw_config["root"] = raw_config["config"].parent |
251 | | - else: |
252 | | - ( |
253 | | - raw_config["root"], |
254 | | - raw_config["config"], |
255 | | - ) = find_project_root_and_config(raw_config["paths"]) |
256 | | - |
257 | | - if raw_config["config"] is not None: |
258 | | - config_from_file = read_config(raw_config["config"]) |
259 | | - |
260 | | - if "paths" in config_from_file: |
261 | | - paths = config_from_file["paths"] |
262 | | - paths = [ |
263 | | - raw_config["config"].parent.joinpath(path).resolve() |
264 | | - for path in to_list(paths) |
265 | | - ] |
266 | | - config_from_file["paths"] = paths |
267 | | - |
268 | | - raw_config = {**raw_config, **config_from_file} |
| 228 | + raw_config = normalize_programmatic_config( |
| 229 | + raw_config, |
| 230 | + command="build", |
| 231 | + defaults_from_cli=cast("dict[str, Any]", DEFAULTS_FROM_CLI), |
| 232 | + ) |
269 | 233 |
|
270 | 234 | config_ = pm.hook.pytask_configure(pm=pm, raw_config=raw_config) |
271 | 235 |
|
|
0 commit comments