fsl.utils.run
This module provides some functions for running shell commands.
Note
The functions in this module are only known to work in Unix-like environments.
Call a command and return its output. |
|
Call a FSL command and return its output. |
|
Run the given python function as a shell command. |
|
Save the given python function to an executable file. |
|
Context manager which causes all calls to |
|
Waits until all specified cluster jobs have finished. |
|
Returns the output of the given cluster-submitted job. |
- fsl.utils.run.DRY_RUN = False
If
True
, therun()
function will only log commands, but will not execute them.
- fsl.utils.run.DRY_RUN_COMMANDS = None
Contains the commands that got logged during a dry run.
Commands will be logged if
DRY_RUN
is true, which can be set usingdryrun()
.
- exception fsl.utils.run.FSLNotPresent[source]
Bases:
Exception
Error raised by the
runfsl()
function when$FSLDIR
cannot be found.- __module__ = 'fsl.utils.run'
- __weakref__
list of weak references to the object (if defined)
- fsl.utils.run.dryrun(*_)[source]
Context manager which causes all calls to
run()
to be logged but not executed. See theDRY_RUN
flag.The returned standard output will be equal to
' '.join(args)
.After this function returns, each command that was executed while the dryrun is active, along with any submission parameters, will be accessible within a list which is stored as
DRY_RUN_COMMANDS
.
- fsl.utils.run.prepareArgs(args)[source]
Used by the
run()
function. Ensures that the given arguments is a list of strings.
- fsl.utils.run._forwardStream(in_, *outs)[source]
Creates and starts a daemon thread which forwards the given input stream to one or more output streams. Used by the
run()
function to redirect a command’s standard output/error streams to more than one destination.It is necessary to read the process stdout/ stderr on separate threads to avoid deadlocks.
- Parameters:
in – Input stream
outs – Output stream(s)
- Returns:
The thread that has been started.
- fsl.utils.run.run(*args, **kwargs)[source]
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.
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 the
exitcode
option is set toTrue
.- Parameters:
stdout – Must be passed as a keyword argument. Defaults to
True
. IfTrue
, standard output is captured and returned. Ignored ifsubmit
is specified.stderr – Must be passed as a keyword argument. Defaults to
False
. IfTrue
, standard error is captured and returned. Ignored ifsubmit
is specified.exitcode – Must be passed as a keyword argument. Defaults to
False
. IfTrue
, and the command’s return code is non-0, an exception is not raised. Ignored ifsubmit
is specified.submit – Must be passed as a keyword argument. Defaults to
None
. IfTrue
, the command is submitted as a cluster job via thefsl.wrappers.fsl_sub
function. May also be a dictionary containing arguments to that function.cmdonly – Defaults to
False
. IfTrue
, the command is not executed, but rather is returned directly, as a list of arguments.log –
Must be passed as a keyword argument. Defaults to
{'tee' : True}
. An optionaldict
which may be used to redirect the command’s standard output and error. Ignored ifsubmit
is specified. The following keys are recognised:- tee: If
True
(the default), the command’s standard output/error streams are forwarded to the output streams of this process, in addition to being captured and returned.
- tee: If
- stdout: Optional callable or file-like object to which
the command’s standard output stream can be forwarded.
- stderr: Optional callable or file-like object to which
the command’s standard error stream can be forwarded.
- cmd: Optional callable or file-like object to which
the command itself is logged.
silent – Suppress standard output/error. Equivalent to passing
log={'tee' : False}
. Ignored if log is also passed.
All other keyword arguments are passed through to the
subprocess.Popen
object (via_realrun()
), unlesssubmit=True
, in which case they are passed through to thefsl_sub()
function.- Returns:
If
submit
is provided, the ID of the submitted job is returned as a string. Otherwise returns a single value or a tuple, based on the based on thestdout
,stderr
, andexitcode
arguments.
- fsl.utils.run._realrun(tee, logStdout, logStderr, logCmd, *args, **kwargs)[source]
Used by
run()
. Runs the given command and manages its standard output and error streams.- Parameters:
tee – If
True
, the command’s standard output and error streams are forwarded to this process’ standard output/ error.logStdout – Optional callable or file-like object to which the command’s standard output stream can be forwarded.
logStderr – Optional callable or file-like object to which the command’s standard error stream can be forwarded.
logCmd – Optional callable or file-like to which the command itself is logged.
args – Command to run
kwargs – Passed through to the
subprocess.Popen
object.
- Returns:
A tuple containing: - the command’s standard output as a string. - the command’s standard error as a string. - the command’s exit code.
- fsl.utils.run.runfsl(*args, **kwargs)[source]
Call a FSL command and return its output.
This function searches for the command in the following locations (ordered by priority):
FSL_PREFIX
$FSLDEVDIR/bin
$FSLDIR/bin
If found, the full path to the command is then passed to
run()
.
- fsl.utils.run.runfunc(func, args=None, kwargs=None, tmp_dir=None, clean='never', verbose=False, **run_kwargs)[source]
Run the given python function as a shell command. See
func_to_cmd()
for details on the arguments.The remaining
run_kwargs
arguments are passed through to therun()
function.
- fsl.utils.run.func_to_cmd(func, args=None, kwargs=None, tmp_dir=None, clean='never', verbose=False)[source]
Save the given python function to an executable file. Return a string containing a command that can be used to run the function.
- ..warning:: If submitting a function defined in the
__main__
script, the script will be run again to retrieve this function. Make sure there is a
if __name__ == '__main__'
guard to prevent the full script from being re-run.
- Parameters:
func – function to be run
args – positional arguments
kwargs – keyword arguments
tmp_dir – directory where to store the temporary file (default: the system temporary directory)
clean –
Whether the script should be removed after running. There are three options:
"never"
: (default) Script is kept"on_success"
: only remove if script successfullyfinished (i.e., no error is raised)
"always"
: always remove the script, even if itraises an error
verbose – If set to True, the script will print its own filename before running
- ..warning:: If submitting a function defined in the
- fsl.utils.run.wslcmd(cmdpath, *args)[source]
Convert a command + arguments into an equivalent set of arguments that will run the command under Windows Subsystem for Linux
- Parameters:
cmdpath – Fully qualified path to the command. This is essentially a WSL path not a Windows one since FSLDIR is specified as a WSL path, however it may have backslashes as path separators due to previous use of
os.path.join
args – Sequence of command arguments (the first of which is the unqualified command name)
- Returns:
If
cmdpath
exists and is executable in WSL, return a sequence of command arguments which when executed will run the command in WSL. Windows paths in the argument list will be converted to WSL paths. Ifcmdpath
was not executable in WSL, returns None
- fsl.utils.run.hold(job_ids, hold_filename=None, timeout=10)[source]
Waits until all specified cluster jobs have finished.
- Parameters:
job_ids – Possibly nested sequence of job ids. The job ids themselves should be strings.
hold_filename – Filename to use as a hold file. The containing directory should exist, but the file itself should not. Defaults to a ./.<random characters>.hold in the current directory.
timeout – Number of seconds to sleep between status checks.
- fsl.utils.run.job_output(job_id, logdir='.', command=None, name=None)[source]
Returns the output of the given cluster-submitted job.
On SGE cluster systems, the standard output and error streams of a submitted job are saved to files named
<job_id>.o
and<job_id>.e
. This function simply reads those files and returns their content.- Parameters:
job_id – String containing job ID.
logdir – Directory containing the log - defaults to the current directory.
command – Command that was run. Not currently used.
name – Job name if it was specified. Not currently used.
- Returns:
A tuple containing the standard output and standard error.