auto_process_ngs.command
Provides a single utility class Command which can be used to build command lines to execute applications.
- class auto_process_ngs.command.Command(command, *args)
Class for creating and executing command lines
The Command class is intended to help with building and executing command lines.
For example to create a command line to run the Linux ‘ls’ command:
>>> ls = Command('ls')
To add some arguments:
>>> ls.add_args('-l','-tr')
To see the resulting command line:
>>> str(ls)
To run using the subprocess module do:
>>> ls.run_subprocess()
- add_args(*args)
Append arguments to the command
- property args
Return the arguments as a list
- property command
Return the command
- property command_line
Return the full command line as a list
- property has_exe
Check if the command executable exists
- make_wrapper_script(shell=None, filen=None, fp=None, prologue=None, epilogue=None, quote_spaces=False)
Wrap the command in a script
Returns a string which can be injected into a file and run as a script.
- Parameters:
shell (str) – optional, if set then will be written to the wrapper script shebang (#!)
filen (str) – optional, if set then wrapper script will be written to a file with this path
fp (File) – optional, if set then must be a File-like object opened for writing, to which the wrapper script will be written
prologue (str) – optional, if set then will be written into the script before the command
epilogue (str) – optional, if set then will be written into the script after the command
quote_spaces (str) – if True then arguments containing whitespace will be wrapped in quotes
- Returns:
the wrapper script contents.
- Return type:
String
- run_subprocess(log=None, err=None, working_dir=None)
Run the command using subprocess.Popen
This runs the command using the subprocess.popen() function and wais for it to finish.
- Parameters:
log – optional, name of file to write stdout to (defaults to sys.stdout)
err – optional, name of file to write stderr to (defaults to same location as log, if that was specified, or else to sys.stderr)
working_dir – optional, working directory to use (defaults to current directory
- Returns:
Return code from subprocess.Popen() call.
- subprocess_check_output(include_err=True, working_dir=None)
Run the command and capture the output
This runs the command using
subprocess.check_output
and returns the output (along with the return code from the command).- Parameters:
include_err – optional, if True then stderr is included in the output (default); otherwise stderr is discarded.
working_dir – optional, working directory to use (defaults to current directory
- Returns:
Tuple of (returncode,output).