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

Added new fslsub.output function. Bugfix to run.run.

parent 14083ed6
No related branches found
No related tags found
No related merge requests found
...@@ -33,17 +33,20 @@ Example usage, building a short pipeline:: ...@@ -33,17 +33,20 @@ Example usage, building a short pipeline::
submit submit
info info
output
wait wait
func_to_cmd
""" """
import logging
from six import string_types, BytesIO from six import string_types, BytesIO
import subprocess as sp import subprocess as sp
import os.path as op
import time import time
import pickle import pickle
import sys import sys
import tempfile import tempfile
import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -141,7 +144,7 @@ def info(job_id): ...@@ -141,7 +144,7 @@ def info(job_id):
try: try:
result = sp.call(['qstat', '-j', job_id]).decode('utf-8') result = sp.call(['qstat', '-j', job_id]).decode('utf-8')
except FileNotFoundError: except FileNotFoundError:
log.debug("qstat not found; assumes not not cluster") log.debug("qstat not found; assuming not on cluster")
return {} return {}
if 'Following jobs do not exist:' in result: if 'Following jobs do not exist:' in result:
return {} return {}
...@@ -152,6 +155,38 @@ def info(job_id): ...@@ -152,6 +155,38 @@ def info(job_id):
return res 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): def wait(job_ids):
"""Wait for one or more jobs to finish """Wait for one or more jobs to finish
...@@ -212,4 +247,5 @@ def func_to_cmd(func, args, kwargs, tmp_dir=None, clean=False): ...@@ -212,4 +247,5 @@ def func_to_cmd(func, args, kwargs, tmp_dir=None, clean=False):
with open(filename, 'w') as python_file: with open(filename, 'w') as python_file:
python_file.write(python_cmd) python_file.write(python_cmd)
# TODO use current interpreter (e.g. fslpython)?
return "python " + filename + ('; rm ' + filename if clean else '') return "python " + filename + ('; rm ' + filename if clean else '')
...@@ -112,9 +112,9 @@ def run(*args, **kwargs): ...@@ -112,9 +112,9 @@ def run(*args, **kwargs):
``err``), and return code (if ``ret``). ``err``), and return code (if ``ret``).
""" """
err = kwargs.get('err', False) err = kwargs.get('err', False)
ret = kwargs.get('ret', False) ret = kwargs.get('ret', False)
submit = kwargs.get('ret', None) submit = kwargs.get('submit', None)
args = _prepareArgs(args) args = _prepareArgs(args)
if not bool(submit): if not bool(submit):
...@@ -185,5 +185,5 @@ def runfsl(*args, **kwargs): ...@@ -185,5 +185,5 @@ def runfsl(*args, **kwargs):
def wait(job_ids): def wait(job_ids):
"""Calls :func:`.fslsub.wait` for the given ``job_ids``. """ """Proxy for :func:`.fslsub.wait`. """
return fslsub.wait(job_ids) return fslsub.wait(job_ids)
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