-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathtask.py
More file actions
358 lines (303 loc) · 12.2 KB
/
task.py
File metadata and controls
358 lines (303 loc) · 12.2 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import json
from pathlib import Path
import click
from rich.console import Console
from rst2ansi import rst2ansi
from cumulusci.cli.extra_yaml import resolve_extra_yaml
from cumulusci.core.config import TaskConfig
from cumulusci.core.exceptions import CumulusCIUsageError
from cumulusci.utils import doc_task
from .runtime import pass_runtime
from .ui import CliTable
from .utils import group_items
@click.group("task", help="Commands for finding and running tasks for a project")
def task():
pass
# Commands for group: task
@task.command(name="list", help="List available tasks for the current context")
@click.option("--plain", is_flag=True, help="Print the table using plain ascii.")
@click.option("--json", "print_json", is_flag=True, help="Print a json string")
@pass_runtime(require_project=False)
def task_list(runtime, plain, print_json):
tasks = runtime.get_available_tasks()
plain = plain or runtime.universal_config.cli__plain_output
console = Console()
if print_json:
console.print_json(json.dumps(tasks))
return None
task_groups = group_items(tasks)
for group, tasks in task_groups.items():
data = [["Task", "Description"]]
data.extend(sorted(tasks))
table = CliTable(
data,
group,
)
console.print(table)
console.print(
"Use [bold]cci task info <task_name>[/] to get more information about a task."
)
@task.command(name="doc", help="Exports RST format documentation for all tasks")
@click.option(
"--project", "project", is_flag=True, help="Include project-specific tasks only"
)
@click.option(
"--write",
"write",
is_flag=True,
help="If true, write output to a file (./docs/project_tasks.rst or ./docs/cumulusci_tasks.rst)",
)
@pass_runtime(require_project=False)
def task_doc(runtime, project=False, write=False):
if project and runtime.project_config is None:
raise click.UsageError(
"The --project option can only be used inside a project."
)
if project:
full_tasks = runtime.project_config.tasks
selected_tasks = runtime.project_config.config_project.get("tasks", {})
file_name = "project_tasks.rst"
project_name = runtime.project_config.project__name
title = f"{project_name} Tasks Reference"
else:
full_tasks = selected_tasks = runtime.universal_config.tasks
file_name = "cumulusci_tasks.rst"
title = "Tasks Reference"
result = ["=" * len(title), title, "=" * len(title), ""]
for name, task_config_dict in full_tasks.items():
if name not in selected_tasks:
continue
task_config = TaskConfig(task_config_dict)
doc = doc_task(name, task_config)
result += [doc, ""]
result = "\n".join(result)
if write:
Path("docs").mkdir(exist_ok=True)
(Path("docs") / file_name).write_text(result, encoding="utf-8")
else:
click.echo(result)
@task.command(name="info", help="Displays information for a task")
@click.argument("task_name")
@click.option(
"--extra-yaml",
"extra_yaml",
multiple=True,
type=click.Path(),
help=(
"Path to an additional YAML file to merge into the project config "
"for this command only. Can be specified multiple times; later files "
"override earlier ones. Also honors CUMULUSCI_EXTRA_YAML env var "
"(comma-separated paths) as a fallback."
),
)
@pass_runtime(require_project=False, require_keychain=True)
def task_info(runtime, task_name, extra_yaml):
runtime.reload_project_config(additional_yaml=resolve_extra_yaml(extra_yaml))
task_config = (
runtime.project_config.get_task(task_name)
if runtime.project_config is not None
else runtime.universal_config.get_task(task_name)
)
doc = doc_task(task_name, task_config).encode()
click.echo(rst2ansi(doc))
def _peek_extra_yaml(args):
"""Extract --extra-yaml values from args before Click parses them.
Matches both ``--extra-yaml PATH`` and ``--extra-yaml=PATH`` forms.
Returns a tuple suitable for resolve_extra_yaml().
"""
paths = []
i = 0
while i < len(args):
arg = args[i]
if arg == "--extra-yaml":
if i + 1 >= len(args):
raise CumulusCIUsageError("--extra-yaml requires a path argument")
paths.append(args[i + 1])
i += 2
elif arg.startswith("--extra-yaml="):
paths.append(arg.split("=", 1)[1])
i += 1
else:
i += 1
return tuple(paths)
class RunTaskCommand(click.MultiCommand):
# options that are not task specific
global_options = {
"no-prompt": {
"help": "Disables all prompts. Set for non-interactive mode such as calling from scripts or CI sytems",
"is_flag": True,
},
"debug": {
"help": "Drops into the Python debugger on an exception",
"is_flag": True,
},
"debug-before": {
"help": "Drops into the Python debugger right before the task starts",
"is_flag": True,
},
"debug-after": {
"help": "Drops into the Python debugger at task completion.",
"is_flag": True,
},
"extra-yaml": {
"help": (
"Path to an additional YAML file to merge into the project "
"config for this command only. Can be specified multiple times; "
"later files override earlier ones. Also honors "
"CUMULUSCI_EXTRA_YAML env var (comma-separated paths) as a "
"fallback."
),
"is_flag": False,
"multiple": True,
},
}
def list_commands(self, ctx):
runtime = ctx.obj
tasks = runtime.get_available_tasks()
return sorted([t["name"] for t in tasks])
def resolve_command(self, ctx, args):
# Peek at --extra-yaml / CUMULUSCI_EXTRA_YAML before Click resolves
# the task. Click's MultiCommand protocol calls resolve_command ->
# get_command; by the time get_command runs, ctx.args is empty. We
# read from the raw `args` list here so --extra-yaml values are
# available before get_task() runs.
runtime = ctx.obj
if runtime is not None and runtime.project_config is not None:
extra_yaml_paths = _peek_extra_yaml(args)
runtime.reload_project_config(
additional_yaml=resolve_extra_yaml(extra_yaml_paths)
)
return super().resolve_command(ctx, args)
def get_command(self, ctx, task_name):
runtime = ctx.obj
if runtime.project_config is None:
raise runtime.project_config_error
runtime._load_keychain()
task_config = runtime.project_config.get_task(task_name)
if "options" not in task_config.config:
task_config.config["options"] = {}
task_class = task_config.get_class()
task_options = task_class.task_options
params = self._get_default_command_options(task_class.salesforce_task)
params.extend(self._get_click_options_for_task(task_options))
def run_task(*args, **kwargs):
"""Callback function that executes when the command fires."""
org, org_config = runtime.get_org(
kwargs.pop("org", None), fail_if_missing=False
)
# Merge old-style and new-style command line options
old_options = kwargs.pop("o", ())
new_options = {
k: v for k, v in kwargs.items() if k not in self.global_options
}
options = self._collect_task_options(
new_options, old_options, task_name, task_options
)
# Merge options from the command line into options from the task config.
task_config.config["options"].update(options)
try:
task = task_class(
task_config.project_config, task_config, org_config=org_config
)
if kwargs.get("debug_before", None):
import pdb
pdb.set_trace()
task()
if kwargs.get("debug_after", None):
import pdb
pdb.set_trace()
finally:
runtime.alert(f"Task complete: {task_name}")
cmd = click.Command(task_name, params=params, callback=run_task)
cmd.help = task_config.description
return cmd
def format_help(self, ctx, formatter):
"""Custom help for `cci task run`"""
runtime = ctx.obj
tasks = runtime.get_available_tasks()
task_groups = group_items(tasks)
console = Console()
for group, tasks in task_groups.items():
data = [["Task", "Description"]]
data.extend(sorted(tasks))
table = CliTable(
data,
group,
)
console.print(table)
console.print("Usage: cci task run <task_name> [TASK_OPTIONS...]\n")
console.print("See above for a complete list of available tasks.")
console.print(
"Use [bold]cci task info <task_name>[/] to get more information about a task and its options."
)
def _collect_task_options(self, new_options, old_options, task_name, task_options):
"""Merge new style --options with old style -o options.
Raises:
CumulusCIUsageError: if there is an old option which duplicates a new one,
or the option doesn't exist for the given task.
"""
# filter out options with no values
options = {
normalize_option_name(k): v for k, v in new_options.items() if v is not None
}
for k, v in old_options:
k = normalize_option_name(k)
if options.get(k):
raise CumulusCIUsageError(
f"Please make sure to specify options only once. Found duplicate option `{k}`."
)
if k not in task_options:
raise CumulusCIUsageError(
f"No option `{k}` found in task {task_name}.\nTo view available task options run: `cci task info {task_name}`"
)
options[k] = v
return options
def _get_click_options_for_task(self, task_options):
"""
Given a dict of options in a task, constructs and returns the
corresponding list of click.Option instances
"""
click_options = [click.Option(["-o"], nargs=2, multiple=True, hidden=True)]
for name, properties in task_options.items():
# NOTE: When task options aren't explicitly given via the command line
# click complains that there are no values for options. We set required=False
# to mitigate this error. Task option validation should be performed at the
# task level via task._validate_options() or Pydantic models.
decls = set(
(
f"--{name}",
f"--{name.replace('_', '-')}",
)
)
click_options.append(
click.Option(
param_decls=tuple(decls),
required=False, # don't enforce option values in Click
help=properties.get("description", ""),
)
)
return click_options
def _get_default_command_options(self, is_salesforce_task):
click_options = []
for opt_name, config in self.global_options.items():
click_options.append(
click.Option(
param_decls=(f"--{opt_name}",),
is_flag=config["is_flag"],
multiple=config.get("multiple", False),
help=config["help"],
)
)
if is_salesforce_task:
click_options.append(
click.Option(
param_decls=("--org",),
help="Specify the target org. By default, runs against the current default org.",
)
)
return click_options
@task.command(cls=RunTaskCommand, name="run", help="Runs a task")
def task_run():
pass # pragma: no cover
def normalize_option_name(k):
return k.replace("-", "_")