diff --git a/fsl/utils/fslsub.py b/fsl/utils/fslsub.py index 18228b7ba130a239b61d7847da98c7a77dd0b0f3..fd7f195b1c91973afc436f47ff846bd0a9bbcfd7 100644 --- a/fsl/utils/fslsub.py +++ b/fsl/utils/fslsub.py @@ -33,17 +33,20 @@ Example usage, building a short pipeline:: submit info + output wait + func_to_cmd """ -import logging from six import string_types, BytesIO import subprocess as sp +import os.path as op import time import pickle import sys import tempfile +import logging log = logging.getLogger(__name__) @@ -141,7 +144,7 @@ def info(job_id): try: result = sp.call(['qstat', '-j', job_id]).decode('utf-8') except FileNotFoundError: - log.debug("qstat not found; assumes not not cluster") + log.debug("qstat not found; assuming not on cluster") return {} if 'Following jobs do not exist:' in result: return {} @@ -152,6 +155,38 @@ def info(job_id): return res +def output(job_id, command, cwd='.', name=None): + """Returns the output of the given job. + + :arg job_id: String with job id + :arg command: Command that was run + :arg cwd: Directory from which command was submitted - defaults to + the current directory. + :arg name: Job name if it was specified. + :returns: A tuple containing the standard output and standard error. + """ + + if name is None: + name = op.basename(command) + + stdout = op.join(cwd, '{}.o{}'.format(name, job_id)) + stderr = op.join(cwd, '{}.e{}'.format(name, job_id)) + + if op.exists(stdout): + with open(stdout, 'rt') as f: + stdout = f.read() + else: + stdout = None + + if op.exists(stderr): + with open(stderr, 'rt') as f: + stderr = f.read() + else: + stderr = None + + return stdout, stderr + + def wait(job_ids): """Wait for one or more jobs to finish @@ -212,4 +247,5 @@ def func_to_cmd(func, args, kwargs, tmp_dir=None, clean=False): with open(filename, 'w') as python_file: python_file.write(python_cmd) + # TODO use current interpreter (e.g. fslpython)? return "python " + filename + ('; rm ' + filename if clean else '') diff --git a/fsl/utils/run.py b/fsl/utils/run.py index 0f0fa0a080b8350d1a70e8e801eac01f5a93f8f7..bcf43106af7095fa97e15229eac1f05d243a4f50 100644 --- a/fsl/utils/run.py +++ b/fsl/utils/run.py @@ -112,9 +112,9 @@ def run(*args, **kwargs): ``err``), and return code (if ``ret``). """ - err = kwargs.get('err', False) - ret = kwargs.get('ret', False) - submit = kwargs.get('ret', None) + err = kwargs.get('err', False) + ret = kwargs.get('ret', False) + submit = kwargs.get('submit', None) args = _prepareArgs(args) if not bool(submit): @@ -185,5 +185,5 @@ def runfsl(*args, **kwargs): def wait(job_ids): - """Calls :func:`.fslsub.wait` for the given ``job_ids``. """ + """Proxy for :func:`.fslsub.wait`. """ return fslsub.wait(job_ids)