-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcollect_command.py
More file actions
221 lines (180 loc) · 7.42 KB
/
collect_command.py
File metadata and controls
221 lines (180 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""Contains the implementation of ``pytask collect``."""
from __future__ import annotations
import sys
from collections import defaultdict
from typing import TYPE_CHECKING
from typing import Any
import click
from rich.text import Text
from rich.tree import Tree
from _pytask.click import ColoredCommand
from _pytask.console import FILE_ICON
from _pytask.console import PYTHON_ICON
from _pytask.console import TASK_ICON
from _pytask.console import console
from _pytask.console import create_url_style_for_path
from _pytask.console import format_node_name
from _pytask.console import format_task_name
from _pytask.dag import create_dag
from _pytask.exceptions import CollectionError
from _pytask.exceptions import ConfigurationError
from _pytask.exceptions import ResolvingDependenciesError
from _pytask.mark import select_by_keyword
from _pytask.mark import select_by_mark
from _pytask.node_protocols import PPathNode
from _pytask.node_protocols import PTask
from _pytask.node_protocols import PTaskWithPath
from _pytask.outcomes import ExitCode
from _pytask.path import find_common_ancestor
from _pytask.path import relative_to
from _pytask.pluginmanager import hookimpl
from _pytask.pluginmanager import storage
from _pytask.session import Session
from _pytask.tree_util import tree_leaves
if TYPE_CHECKING:
from pathlib import Path
from typing import NoReturn
@hookimpl(tryfirst=True)
def pytask_extend_command_line_interface(cli: click.Group) -> None:
"""Extend the command line interface."""
cli.add_command(collect)
@click.command(cls=ColoredCommand)
@click.option(
"--nodes",
is_flag=True,
default=False,
help="Show a task's dependencies and products.",
)
def collect(**raw_config: Any | None) -> NoReturn:
"""Collect tasks and report information about them."""
pm = storage.get()
raw_config["command"] = "collect"
try:
config = pm.hook.pytask_configure(pm=pm, raw_config=raw_config)
session = Session.from_config(config)
except (ConfigurationError, Exception): # noqa: BLE001 # pragma: no cover
session = Session(exit_code=ExitCode.CONFIGURATION_FAILED)
console.print_exception()
else:
try:
session.hook.pytask_log_session_header(session=session)
session.hook.pytask_collect(session=session)
session.dag = create_dag(session=session)
tasks = _select_tasks_by_expressions_and_marker(session)
task_with_path = [t for t in tasks if isinstance(t, PTaskWithPath)]
common_ancestor = _find_common_ancestor_of_all_nodes(
task_with_path, session.config["paths"], session.config["nodes"]
)
dictionary = _organize_tasks(task_with_path)
if dictionary:
_print_collected_tasks(
dictionary,
session.config["nodes"],
session.config["editor_url_scheme"],
common_ancestor,
)
console.print()
console.rule(style="neutral")
except CollectionError: # pragma: no cover
session.exit_code = ExitCode.COLLECTION_FAILED
except ResolvingDependenciesError: # pragma: no cover
session.exit_code = ExitCode.DAG_FAILED
except Exception: # noqa: BLE001 # pragma: no cover
session.exit_code = ExitCode.FAILED
console.print_exception()
console.rule(style="failed")
session.hook.pytask_unconfigure(session=session)
sys.exit(session.exit_code)
def _select_tasks_by_expressions_and_marker(session: Session) -> list[PTask]:
"""Select tasks by expressions and marker."""
all_tasks = {task.signature for task in session.tasks}
remaining_by_mark = select_by_mark(session, session.dag) or all_tasks
remaining_by_keyword = select_by_keyword(session, session.dag) or all_tasks
remaining = remaining_by_mark & remaining_by_keyword
return [task for task in session.tasks if task.signature in remaining]
def _find_common_ancestor_of_all_nodes(
tasks: list[PTaskWithPath], paths: list[Path], show_nodes: bool
) -> Path:
"""Find common ancestor from all nodes and passed paths."""
all_paths = []
for task in tasks:
all_paths.append(task.path)
if show_nodes:
all_paths.extend(
x.path for x in tree_leaves(task.depends_on) if isinstance(x, PPathNode)
)
all_paths.extend(
x.path for x in tree_leaves(task.produces) if isinstance(x, PPathNode)
)
return find_common_ancestor(*all_paths, *paths)
def _organize_tasks(tasks: list[PTaskWithPath]) -> dict[Path, list[PTaskWithPath]]:
"""Organize tasks in a dictionary.
The dictionary has file names as keys and then a dictionary with task names and
below a dictionary with dependencies and targets.
"""
dictionary: dict[Path, list[PTaskWithPath]] = defaultdict(list)
for task in tasks:
dictionary[task.path].append(task)
sorted_dict = {}
for k in sorted(dictionary):
sorted_dict[k] = sorted(dictionary[k], key=lambda x: x.name)
return sorted_dict
def _print_collected_tasks(
dictionary: dict[Path, list[PTaskWithPath]],
show_nodes: bool,
editor_url_scheme: str,
common_ancestor: Path,
) -> None:
"""Print the information on collected tasks.
Parameters
----------
dictionary
A dictionary with path on the first level, tasks on the second, dependencies and
products on the third.
show_nodes
Indicator for whether dependencies and products should be displayed.
editor_url_scheme
The scheme to create an url.
common_ancestor
The path common to all tasks and nodes.
"""
# Have a new line between the number of collected tasks and this info.
console.print()
tree = Tree("Collected tasks:", highlight=True)
for module, tasks in dictionary.items():
reduced_module = relative_to(module, common_ancestor)
url_style = create_url_style_for_path(module, editor_url_scheme)
module_branch = tree.add(
Text.assemble(
PYTHON_ICON, "<Module ", Text(str(reduced_module), style=url_style), ">"
)
)
for task in tasks:
reduced_task_name = format_task_name(
task, editor_url_scheme=editor_url_scheme
)
task_branch = module_branch.add(
Text.assemble(TASK_ICON, "<Function ", reduced_task_name, ">"),
)
if show_nodes:
deps = list(tree_leaves(task.depends_on))
for node in sorted(
deps,
key=(
lambda x: x.path.as_posix()
if isinstance(x, PPathNode)
else x.name
),
):
text = format_node_name(node, (common_ancestor,))
task_branch.add(Text.assemble(FILE_ICON, "<Dependency ", text, ">"))
products = list(tree_leaves(task.produces))
for node in sorted(
products,
key=lambda x: x.path.as_posix()
if isinstance(x, PPathNode)
else x.name,
):
text = format_node_name(node, (common_ancestor,))
task_branch.add(Text.assemble(FILE_ICON, "<Product ", text, ">"))
console.print(tree)