Skip to content
Snippets Groups Projects
Commit 184dd60f authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

RF: run function now passes all extra kwargs through to sp.Popen

parent 57cb76ed
No related branches found
No related tags found
No related merge requests found
...@@ -165,17 +165,21 @@ def run(*args, **kwargs): ...@@ -165,17 +165,21 @@ def run(*args, **kwargs):
- cmd: Optional file-like object to which the command - cmd: Optional file-like object to which the command
itself is logged. itself is logged.
All other keyword arguments are passed through to the ``subprocess.Popen``
object (via :func:`_realrun`), unless ``submit=True``, in which case they
are ignored.
:returns: If ``submit`` is provided, the return value of :returns: If ``submit`` is provided, the return value of
:func:`.fslsub` is returned. Otherwise returns a single :func:`.fslsub` is returned. Otherwise returns a single
value or a tuple, based on the based on the ``stdout``, value or a tuple, based on the based on the ``stdout``,
``stderr``, and ``exitcode`` arguments. ``stderr``, and ``exitcode`` arguments.
""" """
returnStdout = kwargs.get('stdout', True) returnStdout = kwargs.pop('stdout', True)
returnStderr = kwargs.get('stderr', False) returnStderr = kwargs.pop('stderr', False)
returnExitcode = kwargs.get('exitcode', False) returnExitcode = kwargs.pop('exitcode', False)
submit = kwargs.get('submit', {}) submit = kwargs.pop('submit', {})
log = kwargs.get('log', {}) log = kwargs.pop('log', {})
tee = log .get('tee', False) tee = log .get('tee', False)
logStdout = log .get('stdout', None) logStdout = log .get('stdout', None)
logStderr = log .get('stderr', None) logStderr = log .get('stderr', None)
...@@ -207,7 +211,7 @@ def run(*args, **kwargs): ...@@ -207,7 +211,7 @@ def run(*args, **kwargs):
# Run directly - delegate to _realrun # Run directly - delegate to _realrun
stdout, stderr, exitcode = _realrun( stdout, stderr, exitcode = _realrun(
tee, logStdout, logStderr, logCmd, *args) tee, logStdout, logStderr, logCmd, *args, **kwargs)
if not returnExitcode and (exitcode != 0): if not returnExitcode and (exitcode != 0):
raise RuntimeError('{} returned non-zero exit code: {}'.format( raise RuntimeError('{} returned non-zero exit code: {}'.format(
...@@ -222,7 +226,6 @@ def run(*args, **kwargs): ...@@ -222,7 +226,6 @@ def run(*args, **kwargs):
else: return tuple(results) else: return tuple(results)
def _dryrun(submit, returnStdout, returnStderr, returnExitcode, *args): def _dryrun(submit, returnStdout, returnStderr, returnExitcode, *args):
"""Used by the :func:`run` function when the :attr:`DRY_RUN` flag is """Used by the :func:`run` function when the :attr:`DRY_RUN` flag is
active. active.
...@@ -243,7 +246,7 @@ def _dryrun(submit, returnStdout, returnStderr, returnExitcode, *args): ...@@ -243,7 +246,7 @@ def _dryrun(submit, returnStdout, returnStderr, returnExitcode, *args):
else: return tuple(results) else: return tuple(results)
def _realrun(tee, logStdout, logStderr, logCmd, *args): def _realrun(tee, logStdout, logStderr, logCmd, *args, **kwargs):
"""Used by :func:`run`. Runs the given command and manages its standard """Used by :func:`run`. Runs the given command and manages its standard
output and error streams. output and error streams.
...@@ -262,12 +265,14 @@ def _realrun(tee, logStdout, logStderr, logCmd, *args): ...@@ -262,12 +265,14 @@ def _realrun(tee, logStdout, logStderr, logCmd, *args):
:arg args: Command to run :arg args: Command to run
:arg kwargs: Passed through to the ``subprocess.Popen`` object.
:returns: A tuple containing: :returns: A tuple containing:
- the command's standard output as a string. - the command's standard output as a string.
- the command's standard error as a string. - the command's standard error as a string.
- the command's exit code. - the command's exit code.
""" """
proc = sp.Popen(args, stdout=sp.PIPE, stderr=sp.PIPE) proc = sp.Popen(args, stdout=sp.PIPE, stderr=sp.PIPE, **kwargs)
with tempdir.tempdir(changeto=False) as td: with tempdir.tempdir(changeto=False) as td:
# We always direct the command's stdout/ # We always direct the command's stdout/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment