Skip to content

Commit

Permalink
Merge branch 'tickets/DM-27835' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
n8pease committed Dec 9, 2020
2 parents 9a9aca6 + b9b1f49 commit 834ab33
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 43 deletions.
35 changes: 6 additions & 29 deletions python/lsst/ctrl/mpexec/cli/cmd/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
from lsst.daf.butler.cli.opt import (config_file_option,
config_option,
options_file_option)
from lsst.daf.butler.cli.utils import (cli_handle_exception,
Mocker,
MWCtxObj,
from lsst.daf.butler.cli.utils import (MWCtxObj,
option_section,
unwrap)
import lsst.obs.base.cli.opt as obsBaseOpts
Expand Down Expand Up @@ -63,7 +61,7 @@ def _doBuild(ctx, **kwargs):
obsBaseOpts.instrument_option.name()):
kwargs.pop(pipelineAction)
kwargs['pipeline_actions'] = makePipelineActions(MWCtxObj.getFrom(ctx).args)
return cli_handle_exception(script.build, **kwargs)
return script.build(**kwargs)


@click.command(cls=PipetaskCommand, epilog=epilog, short_help="Build pipeline definition.")
Expand Down Expand Up @@ -92,35 +90,14 @@ def qgraph(ctx, **kwargs):
"""Build and optionally save quantum graph.
"""
pipeline = _doBuild(ctx, **kwargs)
cli_handle_exception(script.qgraph, pipelineObj=pipeline, **kwargs)
script.qgraph(pipelineObj=pipeline, **kwargs)


@click.command(cls=PipetaskCommand, epilog=epilog)
@click.pass_context
@ctrlMpExecOpts.debug_option()
@ctrlMpExecOpts.show_option()
@ctrlMpExecOpts.pipeline_build_options()
@ctrlMpExecOpts.qgraph_options()
@ctrlMpExecOpts.butler_options()
@ctrlMpExecOpts.execution_options()
@ctrlMpExecOpts.meta_info_options()
@option_section(sectionText="")
@options_file_option()
# --call-mocker is for use with test code, it is not intended for CLI or other
# non-testing use. It allows this command function to be executed
# programatically and have it call Mocker with its kwargs, which can the be
# gotten from Mocker later. At some point, ctrl_mpexec should stop passing
# around a SimpleNamespace of arguments, which would make this workaround
# unnecessary.
@click.option("--call-mocker",
is_flag=True,
hidden=True) # do not show this option in the help menu.
@ctrlMpExecOpts.run_options()
def run(ctx, **kwargs):
"""Build and execute pipeline and quantum graph.
"""
if kwargs["call_mocker"]:
Mocker(**kwargs)
return
pipeline = _doBuild(ctx, **kwargs)
qgraph = cli_handle_exception(script.qgraph, pipelineObj=pipeline, **kwargs)
cli_handle_exception(script.run, qgraphObj=qgraph, **kwargs)
qgraph = script.qgraph(pipelineObj=pipeline, **kwargs)
script.run(qgraphObj=qgraph, **kwargs)
22 changes: 21 additions & 1 deletion python/lsst/ctrl/mpexec/cli/opt/optionGroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@


__all__ = ("butler_options", "execution_options", "meta_info_options", "pipeline_build_options",
"qgraph_options")
"qgraph_options", "run_options")


import click

from lsst.daf.butler.cli.utils import option_section, unwrap
import lsst.obs.base.cli.opt as obsBaseOpts
import lsst.daf.butler.cli.opt as dafButlerOpts
Expand Down Expand Up @@ -125,3 +127,21 @@ def __init__(self):
ctrlMpExecOpts.init_only_option(),
ctrlMpExecOpts.register_dataset_types_option(),
ctrlMpExecOpts.no_versions_option()]


class run_options(OptionGroup): # noqa: N801
"""Decorator to add the run options to the run command."""

def __init__(self):
self.decorators = [
click.pass_context,
ctrlMpExecOpts.debug_option(),
ctrlMpExecOpts.show_option(),
pipeline_build_options(),
qgraph_options(),
butler_options(),
execution_options(),
meta_info_options(),
option_section(sectionText=""),
dafButlerOpts.options_file_option(),
]
40 changes: 27 additions & 13 deletions tests/test_cmdLineFwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@
import unittest

from lsst.ctrl.mpexec.cmdLineFwk import CmdLineFwk
from lsst.ctrl.mpexec.cli.pipetask import cli as pipetaskCli
from lsst.ctrl.mpexec.cli.utils import (_ACTION_ADD_TASK, _ACTION_CONFIG,
_ACTION_CONFIG_FILE, _ACTION_ADD_INSTRUMENT)
from lsst.ctrl.mpexec.cli.opt import run_options
from lsst.ctrl.mpexec.cli.utils import (
_ACTION_ADD_TASK,
_ACTION_CONFIG,
_ACTION_CONFIG_FILE,
_ACTION_ADD_INSTRUMENT,
PipetaskCommand,
)
from lsst.daf.butler import Config, Quantum, Registry
from lsst.daf.butler.cli.utils import Mocker
from lsst.daf.butler.registry import RegistryConfig
from lsst.obs.base import Instrument
import lsst.pex.config as pexConfig
Expand Down Expand Up @@ -142,7 +146,7 @@ def _makeArgs(registryConfig=None, **kwargs):
"""Return parsed command line arguments.
By default butler_config is set to `Config` populated with some defaults,
it can be overriden completely by keyword argument.
it can be overridden completely by keyword argument.
Parameters
----------
Expand All @@ -153,16 +157,26 @@ def _makeArgs(registryConfig=None, **kwargs):
**kwargs
Overrides for other arguments.
"""
# Execute the "run" command with the --call-mocker flag set so we can get
# all the default arguments that were passed to the command function out of
# the Mocker call.
# At some point, ctrl_mpexec should stop passing around a SimpleNamespace
# of arguments, which would make this workaround unnecessary.
# Use a mock to get the default value of arguments to 'run'.

mock = unittest.mock.Mock()

@click.command(cls=PipetaskCommand)
@run_options()
def fake_run(ctx, **kwargs):
"""Fake "pipetask run" command for gathering input arguments.
The arguments & options should always match the arguments & options in
the "real" command function `lsst.ctrl.mpexec.cli.cmd.run`.
"""
mock(**kwargs)

runner = click.testing.CliRunner()
result = runner.invoke(pipetaskCli, ["run", "--call-mocker"])
result = runner.invoke(fake_run)
if result.exit_code != 0:
raise RuntimeError(f"Failure getting default args from 'pipetask run': {result}")
_, args = Mocker.mock.call_args
raise RuntimeError(f"Failure getting default args from 'fake_run': {result}")
mock.assert_called_once()
args = mock.call_args[1]
args["enableLsstDebug"] = args.pop("debug")
if "pipeline_actions" not in args:
args["pipeline_actions"] = []
Expand Down

0 comments on commit 834ab33

Please sign in to comment.