Commit 65e7bcd0 authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

add options to log_shell for check_call and check_output

parent f2744d9f
......@@ -232,8 +232,14 @@ def long_debug(message):
long_log(logging.DEBUG, message)
def log_shell(arg_list, **kwargs):
"""Pass arg_list to a subprocess call and log all output."""
def log_shell(arg_list, raise_err=False, return_output=False, **kwargs):
"""Pass arg_list to a subprocess call and log all output.
raise_err: (like subprocess.check_call) if True,
raises a RuntimeError if exit code is non-zero
return_output: (like subprocess.check_output) returns any output
from shell call instead of the exit code
"""
logging.info('Starting child process of ppid={}'.format(os.getppid()))
logging.info('Executing: {}'.format(' '.join(arg_list)))
try:
......@@ -243,21 +249,27 @@ def log_shell(arg_list, **kwargs):
parent_name, line_num = os.path.basename(f[1]), f[2]
break
proc_output = ""
proc = subprocess.Popen(arg_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kwargs)
for line in iter(proc.stdout.readline, b''):
if not line:
break
proc_output += line
logging.getLogger('_sub').info(line.rstrip(), extra={'parentname':parent_name, 'parentline':line_num})
return_code = proc.wait()
if return_code:
logging.warning('{} failed with exit code {}'.format(' '.join(arg_list), return_code))
return return_code
if raise_err:
logging.error('{} failed with exit code {}'.format(' '.join(arg_list), return_code))
raise RuntimeError('{} failed with exit code {}'.format(' '.join(arg_list), return_code))
else:
logging.warning('{} failed with exit code {}'.format(' '.join(arg_list), return_code))
return proc_output if return_output else return_code
except OSError as err:
logging.error('Error while executing %s: %s', ' '.join(arg_list), str(err))
return err
else:
logging.info('Successfully finished: %s', ' '.join(arg_list))
return 0
return proc_output if return_output else return_code
def stack_trace(log_level=logging.ERROR):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment