Coverage for python/lsst/ctrl/mpexec/cli/cmd/commands.py: 79%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of ctrl_mpexec.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22import click
23import lsst.pipe.base.cli.opt as pipeBaseOpts
24from lsst.daf.butler.cli.opt import config_file_option, config_option, options_file_option
25from lsst.daf.butler.cli.utils import MWCtxObj, catch_and_exit, option_section, unwrap
27from .. import opt as ctrlMpExecOpts
28from .. import script
29from ..utils import _ACTION_CONFIG, _ACTION_CONFIG_FILE, PipetaskCommand, makePipelineActions
31epilog = unwrap(
32 """Notes:
34--task, --delete, --config, --config-file, and --instrument action options can
35appear multiple times; all values are used, in order left to right.
37FILE reads command-line options from the specified file. Data may be
38distributed among multiple lines (e.g. one option per line). Data after # is
39treated as a comment and ignored. Blank lines and lines starting with # are
40ignored.)
41"""
42)
45def _collectActions(ctx, **kwargs):
46 """Extract pipeline building options, replace them with PipelineActions,
47 return updated `kwargs`.
49 Notes
50 -----
51 The pipeline actions (task, delete, config, config_file, and instrument)
52 must be handled in the order they appear on the command line, but the CLI
53 specification gives them all different option names. So, instead of using
54 the individual action options as they appear in kwargs (because
55 invocation order can't be known), we capture the CLI arguments by
56 overriding `click.Command.parse_args` and save them in the Context's
57 `obj` parameter. We use `makePipelineActions` to create a list of
58 pipeline actions from the CLI arguments and pass that list to the script
59 function using the `pipeline_actions` kwarg name, and remove the action
60 options from kwargs.
61 """
62 for pipelineAction in (
63 ctrlMpExecOpts.task_option.name(),
64 ctrlMpExecOpts.delete_option.name(),
65 config_option.name(),
66 config_file_option.name(),
67 pipeBaseOpts.instrument_option.name(),
68 ):
69 kwargs.pop(pipelineAction)
71 actions = makePipelineActions(MWCtxObj.getFrom(ctx).args)
72 mock_configs = []
73 pipeline_actions = []
74 for action in actions:
75 if action.label and action.label.endswith("-mock"): 75 ↛ 76line 75 didn't jump to line 76, because the condition on line 75 was never true
76 if action.action not in (_ACTION_CONFIG.action, _ACTION_CONFIG_FILE.action):
77 raise ValueError(f"Unexpected option for mock task config overrides: {action}")
78 mock_configs.append(action)
79 else:
80 pipeline_actions.append(action)
82 kwargs["mock_configs"] = mock_configs
83 kwargs["pipeline_actions"] = pipeline_actions
84 return kwargs
87@click.command(cls=PipetaskCommand, epilog=epilog, short_help="Build pipeline definition.")
88@click.pass_context
89@ctrlMpExecOpts.show_option()
90@ctrlMpExecOpts.pipeline_build_options()
91@option_section(sectionText="")
92@options_file_option()
93@catch_and_exit
94def build(ctx, **kwargs):
95 """Build and optionally save pipeline definition.
97 This does not require input data to be specified.
98 """
99 kwargs = _collectActions(ctx, **kwargs)
100 script.build(**kwargs)
103@click.command(cls=PipetaskCommand, epilog=epilog)
104@click.pass_context
105@ctrlMpExecOpts.show_option()
106@ctrlMpExecOpts.pipeline_build_options()
107@ctrlMpExecOpts.qgraph_options()
108@ctrlMpExecOpts.butler_options()
109@option_section(sectionText="")
110@options_file_option()
111@catch_and_exit
112def qgraph(ctx, **kwargs):
113 """Build and optionally save quantum graph."""
114 kwargs = _collectActions(ctx, **kwargs)
115 pipeline = script.build(**kwargs)
116 script.qgraph(pipelineObj=pipeline, **kwargs)
119@click.command(cls=PipetaskCommand, epilog=epilog)
120@ctrlMpExecOpts.run_options()
121@catch_and_exit
122def run(ctx, **kwargs):
123 """Build and execute pipeline and quantum graph."""
124 kwargs = _collectActions(ctx, **kwargs)
125 pipeline = script.build(**kwargs)
126 qgraph = script.qgraph(pipelineObj=pipeline, **kwargs)
127 script.run(qgraphObj=qgraph, **kwargs)