4
4
import sys
5
5
from pathlib import Path
6
6
from typing import Any
7
+ from typing import Callable
8
+ from typing import Iterable
9
+ from typing import Literal
7
10
from typing import TYPE_CHECKING
8
11
9
12
import click
13
+ from _pytask .capture import CaptureMethod
10
14
from _pytask .click import ColoredCommand
11
15
from _pytask .config import hookimpl
12
16
from _pytask .config_utils import _find_project_root_and_config
26
30
27
31
28
32
if TYPE_CHECKING :
33
+ from _pytask .node_protocols import PTask
29
34
from typing import NoReturn
30
35
31
36
32
37
@hookimpl (tryfirst = True )
33
38
def pytask_extend_command_line_interface (cli : click .Group ) -> None :
34
39
"""Extend the command line interface."""
35
- cli .add_command (build )
36
-
37
-
38
- def main (raw_config : dict [str , Any ]) -> Session : # noqa: C901, PLR0912, PLR0915
40
+ cli .add_command (build_command )
41
+
42
+
43
+ def build ( # noqa: C901, PLR0912, PLR0913, PLR0915
44
+ * ,
45
+ capture : Literal ["fd" , "no" , "sys" , "tee-sys" ] | CaptureMethod = CaptureMethod .NO ,
46
+ check_casing_of_paths : bool = True ,
47
+ config : Path | None = None ,
48
+ database_url : str = "" ,
49
+ debug_pytask : bool = False ,
50
+ disable_warnings : bool = False ,
51
+ dry_run : bool = False ,
52
+ editor_url_scheme : Literal ["no_link" , "file" , "vscode" , "pycharm" ] # noqa: PYI051
53
+ | str = "file" ,
54
+ expression : str = "" ,
55
+ force : bool = False ,
56
+ ignore : Iterable [str ] = (),
57
+ marker_expression : str = "" ,
58
+ max_failures : float = float ("inf" ),
59
+ n_entries_in_table : int = 15 ,
60
+ paths : str | Path | Iterable [str | Path ] = (),
61
+ pdb : bool = False ,
62
+ pdb_cls : str = "" ,
63
+ s : bool = False ,
64
+ show_capture : bool = True ,
65
+ show_errors_immediately : bool = False ,
66
+ show_locals : bool = False ,
67
+ show_traceback : bool = True ,
68
+ sort_table : bool = True ,
69
+ stop_after_first_failure : bool = False ,
70
+ strict_markers : bool = False ,
71
+ tasks : Callable [..., Any ] | PTask | Iterable [Callable [..., Any ] | PTask ] = (),
72
+ task_files : str | Iterable [str ] = "task_*.py" ,
73
+ trace : bool = False ,
74
+ verbose : int = 1 ,
75
+ ** kwargs : Any ,
76
+ ) -> Session :
39
77
"""Run pytask.
40
78
41
79
This is the main command to run pytask which usually receives kwargs from the
@@ -44,13 +82,73 @@ def main(raw_config: dict[str, Any]) -> Session: # noqa: C901, PLR0912, PLR0915
44
82
45
83
Parameters
46
84
----------
47
- raw_config : dict[str, Any]
48
- A dictionary with options passed to pytask. In general, this dictionary holds
49
- the information passed via the command line interface.
85
+ capture
86
+ The capture method for stdout and stderr.
87
+ check_casing_of_paths
88
+ Whether errors should be raised when file names have different casings.
89
+ config
90
+ A path to the configuration file.
91
+ database_url
92
+ An URL to the database that tracks the status of tasks.
93
+ debug_pytask
94
+ Whether debug information should be shown.
95
+ disable_warnings
96
+ Whether warnings should be disabled and not displayed.
97
+ dry_run
98
+ Whether a dry-run should be performed that shows which tasks need to be rerun.
99
+ editor_url_scheme
100
+ An url scheme that allows to click on task names, node names and filenames and
101
+ jump right into you preferred edior to the right line.
102
+ expression
103
+ Same as ``-k`` on the command line. Select tasks via expressions on task ids.
104
+ force
105
+ Run tasks even though they would be skipped since nothing has changed.
106
+ ignore
107
+ A pattern to ignore files or directories. Refer to ``pathlib.Path.match``
108
+ for more info.
109
+ marker_expression
110
+ Same as ``-m`` on the command line. Select tasks via marker expressions.
111
+ max_failures
112
+ Stop after some failures.
113
+ n_entries_in_table
114
+ How many entries to display in the table during the execution. Tasks which are
115
+ running are always displayed.
116
+ paths
117
+ A path or collection of paths where pytask looks for the configuration and
118
+ tasks.
119
+ pdb
120
+ Start the interactive debugger on errors.
121
+ pdb_cls
122
+ Start a custom debugger on errors. For example:
123
+ ``--pdbcls=IPython.terminal.debugger:TerminalPdb``
124
+ s
125
+ Shortcut for ``pytask.build(capture"no")``.
126
+ show_capture
127
+ Choose which captured output should be shown for failed tasks.
128
+ show_errors_immediately
129
+ Show errors with tracebacks as soon as the task fails.
130
+ show_locals
131
+ Show local variables in tracebacks.
132
+ show_traceback
133
+ Choose whether tracebacks should be displayed or not.
134
+ sort_table
135
+ Sort the table of tasks at the end of the execution.
136
+ stop_after_first_failure
137
+ Stop after the first failure.
138
+ strict_markers
139
+ Raise errors for unknown markers.
140
+ tasks
141
+ A task or a collection of tasks that is passed to ``pytask.build(tasks=...)``.
142
+ task_files
143
+ A pattern to describe modules that contain tasks.
144
+ trace
145
+ Enter debugger in the beginning of each task.
146
+ verbose
147
+ Make pytask verbose (>= 0) or quiet (= 0).
50
148
51
149
Returns
52
150
-------
53
- session : _pytask.session .Session
151
+ session : pytask .Session
54
152
The session captures all the information of the current run.
55
153
56
154
"""
@@ -61,6 +159,39 @@ def main(raw_config: dict[str, Any]) -> Session: # noqa: C901, PLR0912, PLR0915
61
159
pm .register (cli )
62
160
pm .hook .pytask_add_hooks (pm = pm )
63
161
162
+ raw_config = {
163
+ "capture" : capture ,
164
+ "check_casing_of_paths" : check_casing_of_paths ,
165
+ "config" : config ,
166
+ "database_url" : database_url ,
167
+ "debug_pytask" : debug_pytask ,
168
+ "disable_warnings" : disable_warnings ,
169
+ "dry_run" : dry_run ,
170
+ "editor_url_scheme" : editor_url_scheme ,
171
+ "expression" : expression ,
172
+ "force" : force ,
173
+ "ignore" : ignore ,
174
+ "marker_expression" : marker_expression ,
175
+ "max_failures" : max_failures ,
176
+ "n_entries_in_table" : n_entries_in_table ,
177
+ "paths" : paths ,
178
+ "pdb" : pdb ,
179
+ "pdb_cls" : pdb_cls ,
180
+ "s" : s ,
181
+ "show_capture" : show_capture ,
182
+ "show_errors_immediately" : show_errors_immediately ,
183
+ "show_locals" : show_locals ,
184
+ "show_traceback" : show_traceback ,
185
+ "sort_table" : sort_table ,
186
+ "stop_after_first_failure" : stop_after_first_failure ,
187
+ "strict_markers" : strict_markers ,
188
+ "tasks" : tasks ,
189
+ "task_files" : task_files ,
190
+ "trace" : trace ,
191
+ "verbose" : verbose ,
192
+ ** kwargs ,
193
+ }
194
+
64
195
# If someone called the programmatic interface, we need to do some parsing.
65
196
if "command" not in raw_config :
66
197
raw_config ["command" ] = "build"
@@ -97,9 +228,9 @@ def main(raw_config: dict[str, Any]) -> Session: # noqa: C901, PLR0912, PLR0915
97
228
98
229
raw_config = {** raw_config , ** config_from_file }
99
230
100
- config = pm .hook .pytask_configure (pm = pm , raw_config = raw_config )
231
+ config_ = pm .hook .pytask_configure (pm = pm , raw_config = raw_config )
101
232
102
- session = Session .from_config (config )
233
+ session = Session .from_config (config_ )
103
234
104
235
except (ConfigurationError , Exception ):
105
236
exc_info = sys .exc_info ()
@@ -137,7 +268,7 @@ def main(raw_config: dict[str, Any]) -> Session: # noqa: C901, PLR0912, PLR0915
137
268
return session
138
269
139
270
140
- @click .command (cls = ColoredCommand )
271
+ @click .command (cls = ColoredCommand , name = "build" )
141
272
@click .option (
142
273
"--debug-pytask" ,
143
274
is_flag = True ,
@@ -161,13 +292,13 @@ def main(raw_config: dict[str, Any]) -> Session: # noqa: C901, PLR0912, PLR0915
161
292
"--show-errors-immediately" ,
162
293
is_flag = True ,
163
294
default = False ,
164
- help = "Print errors with tracebacks as soon as the task fails." ,
295
+ help = "Show errors with tracebacks as soon as the task fails." ,
165
296
)
166
297
@click .option (
167
298
"--show-traceback/--show-no-traceback" ,
168
299
type = bool ,
169
300
default = True ,
170
- help = ( "Choose whether tracebacks should be displayed or not." ) ,
301
+ help = "Choose whether tracebacks should be displayed or not." ,
171
302
)
172
303
@click .option (
173
304
"--dry-run" , type = bool , is_flag = True , default = False , help = "Perform a dry-run."
@@ -179,13 +310,13 @@ def main(raw_config: dict[str, Any]) -> Session: # noqa: C901, PLR0912, PLR0915
179
310
default = False ,
180
311
help = "Execute a task even if it succeeded successfully before." ,
181
312
)
182
- def build (** raw_config : Any ) -> NoReturn :
313
+ def build_command (** raw_config : Any ) -> NoReturn :
183
314
"""Collect tasks, execute them and report the results.
184
315
185
316
The default command. pytask collects tasks from the given paths or the
186
317
current working directory, executes them and reports the results.
187
318
188
319
"""
189
320
raw_config ["command" ] = "build"
190
- session = main ( raw_config )
321
+ session = build ( ** raw_config )
191
322
sys .exit (session .exit_code )
0 commit comments