Skip to content

FIX: Improve error handling of CommandLine interfaces #3395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions nipype/interfaces/base/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ class must be instantiated with a command argument
_cmd = None
_version = None
_terminal_output = "stream"
_write_cmdline = False

@classmethod
def set_default_terminal_output(cls, output_type):
Expand All @@ -623,7 +624,9 @@ def set_default_terminal_output(cls, output_type):
else:
raise AttributeError("Invalid terminal output_type: %s" % output_type)

def __init__(self, command=None, terminal_output=None, **inputs):
def __init__(
self, command=None, terminal_output=None, write_cmdline=False, **inputs
):
super(CommandLine, self).__init__(**inputs)
self._environ = None
# Set command. Input argument takes precedence
Expand All @@ -638,6 +641,8 @@ def __init__(self, command=None, terminal_output=None, **inputs):
if terminal_output is not None:
self.terminal_output = terminal_output

self._write_cmdline = write_cmdline

@property
def cmd(self):
"""sets base command, immutable"""
Expand Down Expand Up @@ -669,6 +674,14 @@ def terminal_output(self, value):
)
self._terminal_output = value

@property
def write_cmdline(self):
return self._write_cmdline

@write_cmdline.setter
def write_cmdline(self, value):
self._write_cmdline = value is True

def raise_exception(self, runtime):
raise RuntimeError(
(
Expand Down Expand Up @@ -716,9 +729,16 @@ def _run_interface(self, runtime, correct_return_codes=(0,)):
adds stdout, stderr, merged, cmdline, dependencies, command_path

"""

out_environ = self._get_environ()
# Initialize runtime Bunch

try:
runtime.cmdline = self.cmdline
except Exception as exc:
raise RuntimeError(
"Error raised when interpolating the command line"
) from exc

runtime.stdout = None
runtime.stderr = None
runtime.cmdline = self.cmdline
Expand All @@ -742,7 +762,11 @@ def _run_interface(self, runtime, correct_return_codes=(0,)):
if self._ldd
else "<skipped>"
)
runtime = run_command(runtime, output=self.terminal_output)
runtime = run_command(
runtime,
output=self.terminal_output,
write_cmdline=self.write_cmdline,
)
return runtime

def _format_arg(self, name, trait_spec, value):
Expand Down Expand Up @@ -907,7 +931,14 @@ def _parse_inputs(self, skip=None):

if not isdefined(value):
continue
arg = self._format_arg(name, spec, value)

try:
arg = self._format_arg(name, spec, value)
except Exception as exc:
raise ValueError(
f"Error formatting command line argument '{name}' with value '{value}'"
) from exc

if arg is None:
continue
pos = spec.position
Expand Down
8 changes: 4 additions & 4 deletions nipype/pipeline/engine/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ def __init__(
self.needed_outputs = needed_outputs
self.config = None

if hasattr(self._interface, "write_cmdline"):
self._interface.write_cmdline = True

@property
def interface(self):
"""Return the underlying interface object"""
Expand Down Expand Up @@ -711,16 +714,13 @@ def _run_command(self, execute, copyfiles=True):
f'[Node] Executing "{self.name}" <{self._interface.__module__}'
f".{self._interface.__class__.__name__}>"
)

# Invoke core run method of the interface ignoring exceptions
result = self._interface.run(cwd=outdir, ignore_exception=True)
logger.info(
f'[Node] Finished "{self.name}", elapsed time {result.runtime.duration}s.'
)

if issubclass(self._interface.__class__, CommandLine):
# Write out command line as it happened
Path.write_text(outdir / "command.txt", f"{result.runtime.cmdline}\n")

exc_tb = getattr(result.runtime, "traceback", None)

if not exc_tb:
Expand Down
6 changes: 5 additions & 1 deletion nipype/utils/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import select
import locale
import datetime
from pathlib import Path
from subprocess import Popen, STDOUT, PIPE
from .filemanip import canonicalize_env, read_stream

Expand Down Expand Up @@ -69,7 +70,7 @@ def _read(self, drain):
self._lastidx = len(self._rows)


def run_command(runtime, output=None, timeout=0.01):
def run_command(runtime, output=None, timeout=0.01, write_cmdline=False):
"""Run a command, read stdout and stderr, prefix with timestamp.

The returned runtime contains a merged stdout+stderr log with timestamps
Expand Down Expand Up @@ -100,6 +101,9 @@ def run_command(runtime, output=None, timeout=0.01):
errfile = os.path.join(runtime.cwd, "stderr.nipype")
stderr = open(errfile, "wb")

if write_cmdline:
(Path(runtime.cwd) / "command.txt").write_text(cmdline)

proc = Popen(
cmdline,
stdout=stdout,
Expand Down