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

Untested integration of new fslsub module with wrappers and run modules.

parent c260c36a
No related branches found
No related tags found
No related merge requests found
...@@ -11,18 +11,21 @@ ...@@ -11,18 +11,21 @@
run run
runfsl runfsl
fslsub wait
dryrun
""" """
import logging import logging
import contextlib import contextlib
import collections
import subprocess as sp import subprocess as sp
import os.path as op import os.path as op
import six import six
from fsl.utils.platform import platform as fslplatform from fsl.utils.platform import platform as fslplatform
import fsl.utils.fslsub as fslsub
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -81,26 +84,52 @@ def run(*args, **kwargs): ...@@ -81,26 +84,52 @@ def run(*args, **kwargs):
"""Call a command and return its output. You can pass the command and """Call a command and return its output. You can pass the command and
arguments as a single string, or as a regular or unpacked sequence. arguments as a single string, or as a regular or unpacked sequence.
The command can be run on a cluster by using the ``submit`` keyword
argument.
An exception is raised if the command returns a non-zero exit code, unless An exception is raised if the command returns a non-zero exit code, unless
the ``returnCode`` option is set to ``True``. the ``ret`` option is set to ``True``.
:arg submit: Must be passed as a keyword argument. Defaults to ``None``.
Accepted values are ``True`` or a
If ``True``, the command is submitted as a cluster job via
the :func:`.fslsub.submit` function. May also be a
dictionary containing arguments to that function.
:arg err: Must be passed as a keyword argument. Defaults to
``False``. If ``True``, standard error is captured and
returned. Ignored if ``submit`` is specified.
:arg ret: Must be passed as a keyword argument. Defaults to ``False``.
If ``True``, and the command's return code is non-0, an
exception is not raised. Ignored if ``submit`` is specified.
:returns: If ``submit`` is provided, the cluster job ID is returned.
Otherwise if ``err is False and ret is False`` (the default)
a string containing the command's standard output. is
returned. Or, if ``err is True`` and/or ``ret is True``, a
tuple containing the standard output, standard error (if
``err``), and return code (if ``ret``).
"""
:arg err: Must be passed as a keyword argument. Defaults to err = kwargs.get('err', False)
``False``. If ``True``, standard error is captured and ret = kwargs.get('ret', False)
returned. submit = kwargs.get('ret', None)
args = _prepareArgs(args)
:arg ret: Must be passed as a keyword argument. Defaults to if not bool(submit):
``False``. If ``True``, and the command's return code submit = None
is non-0, an exception is not raised.
:returns: A string containing the command's standard output. Or, if submit is not None:
if ``err is True`` and/or ``returnCode is True``, a tuple err = False
containing the standard output, standard error (if ret = False
``err``), and return code (if ``returnCode``).
""" if submit is True:
submit = dict()
err = kwargs.get('err', False) if submit is not None and not isinstance(submit, collections.Mapping):
ret = kwargs.get('ret', False) raise ValueError('submit must be a mapping containing '
args = _prepareArgs(args) 'options for fsl.utils.fslsub.submit')
if DRY_RUN: if DRY_RUN:
log.debug('dryrun: {}'.format(' '.join(args))) log.debug('dryrun: {}'.format(' '.join(args)))
...@@ -108,8 +137,15 @@ def run(*args, **kwargs): ...@@ -108,8 +137,15 @@ def run(*args, **kwargs):
log.debug('run: {}'.format(' '.join(args))) log.debug('run: {}'.format(' '.join(args)))
if DRY_RUN: if DRY_RUN:
stdout = ' '.join(args)
stderr = '' stderr = ''
if submit is not None:
stdout = ' '.join(args)
else:
stdout = '[submit] ' + ' '.join(args)
elif submit is not None:
return fslsub.submit(' '.join(args), **submit)
else: else:
proc = sp.Popen(args, stdout=sp.PIPE, stderr=sp.PIPE) proc = sp.Popen(args, stdout=sp.PIPE, stderr=sp.PIPE)
stdout, stderr = proc.communicate() stdout, stderr = proc.communicate()
...@@ -148,6 +184,6 @@ def runfsl(*args, **kwargs): ...@@ -148,6 +184,6 @@ def runfsl(*args, **kwargs):
return run(*args, **kwargs) return run(*args, **kwargs)
def fslsub(*args): def wait(job_ids):
"""Not implemented yet. """ """Calls :func:`.fslsub.wait` for the given ``job_ids``. """
raise NotImplementedError('') return fslsub.wait(job_ids)
...@@ -141,8 +141,9 @@ def cmdwrapper(func): ...@@ -141,8 +141,9 @@ def cmdwrapper(func):
:func:`fsl.utils.run.run` function in a standardised manner. :func:`fsl.utils.run.run` function in a standardised manner.
""" """
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
submit = kwargs.pop('submit', None)
cmd = func(*args, **kwargs) cmd = func(*args, **kwargs)
return run.run(cmd, err=True) return run.run(cmd, err=True, submit=submit)
return _update_wrapper(wrapper, func) return _update_wrapper(wrapper, func)
...@@ -152,8 +153,9 @@ def fslwrapper(func): ...@@ -152,8 +153,9 @@ def fslwrapper(func):
:func:`fsl.utils.run.runfsl` function in a standardised manner. :func:`fsl.utils.run.runfsl` function in a standardised manner.
""" """
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
submit = kwargs.pop('submit', None)
cmd = func(*args, **kwargs) cmd = func(*args, **kwargs)
return run.runfsl(cmd, err=True) return run.runfsl(cmd, err=True, submit=submit)
return _update_wrapper(wrapper, func) return _update_wrapper(wrapper, func)
......
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