Logging fixes

This commit is contained in:
Anders Ingemann 2013-07-07 21:29:15 +02:00
parent 0fc535cb29
commit f0e3d45e21

View file

@ -1,12 +1,10 @@
def log_check_call(command): def log_check_call(command):
status = log_call(command) status, stdout, stderr = log_call(command)
if status != 0: if status != 0:
from subprocess import CalledProcessError from subprocess import CalledProcessError
msg = ('Command \'{command}\' returned non-zero exit status ' raise CalledProcessError(status, ' '.join(command), '\n'.join(stderr))
'{status}'.format(command=command, status=status))
raise CalledProcessError(msg)
def log_call(command): def log_call(command):
@ -14,10 +12,13 @@ def log_call(command):
import select import select
import logging import logging
command_log = command[0].replace('/', '.') from os.path import realpath
command_log = realpath(command[0]).replace('/', '.')
log = logging.getLogger(__name__ + command_log) log = logging.getLogger(__name__ + command_log)
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = []
stderr = []
while True: while True:
reads = [process.stdout.fileno(), process.stderr.fileno()] reads = [process.stdout.fileno(), process.stderr.fileno()]
ret = select.select(reads, [], []) ret = select.select(reads, [], [])
@ -26,12 +27,14 @@ def log_call(command):
line = process.stdout.readline() line = process.stdout.readline()
if line != '': if line != '':
log.debug(line.strip()) log.debug(line.strip())
stdout.append(line.strip())
if fd == process.stderr.fileno(): if fd == process.stderr.fileno():
line = process.stderr.readline() line = process.stderr.readline()
if line != '': if line != '':
log.error(line.strip()) log.error(line.strip())
stderr.append(line.strip())
if process.poll() is not None: if process.poll() is not None:
return process.returncode return process.returncode, stdout, stderr
def sed_i(file_path, pattern, subst): def sed_i(file_path, pattern, subst):