Source code for spux.utils.shell
import subprocess
import os
[docs]def execute (command, directory = None, verbosity = 1, executable = None):
"""
Execute an application command (including any arguments) in a command line shell.
Parameter `verbosity`:
0 - no output to the console
1 - print only error messages (default)
2 - prints the command being executed and the result
Returns (process returncode, standard output, error output)
"""
PIPE = subprocess.PIPE
if verbosity == 2:
print (' : -> Executing: %s' % command)
process = subprocess.Popen ([command], cwd = directory, shell = True, stdout = PIPE, stderr = PIPE, env = os.environ.copy (), executable = executable)
output, error = process.communicate ()
if verbosity:
if process.returncode == 0:
if verbosity == 2:
print (' : -> Shell command done.')
else:
print (' : -> Shell command returned a non-zero exit code.')
print (' : -> The standard output and the standard error are printed below.')
print (' : -> STDOUT:')
print (output.decode ('ascii'))
print (' : -> STDERR:')
print (error.decode ('ascii'))
return (process.returncode, output, error)