mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-24 07:26:29 +00:00
Don't use Queue to gather log messages in log_call
... instead we just log the messages directly
This commit is contained in:
parent
20eabc8aca
commit
f1994fab1e
2 changed files with 21 additions and 22 deletions
|
@ -9,7 +9,6 @@ def log_check_call(command, stdin=None, env=None, shell=False):
|
||||||
def log_call(command, stdin=None, env=None, shell=False):
|
def log_call(command, stdin=None, env=None, shell=False):
|
||||||
import subprocess
|
import subprocess
|
||||||
import logging
|
import logging
|
||||||
import Queue
|
|
||||||
from multiprocessing.dummy import Pool as ThreadPool
|
from multiprocessing.dummy import Pool as ThreadPool
|
||||||
from os.path import realpath
|
from os.path import realpath
|
||||||
|
|
||||||
|
@ -22,11 +21,6 @@ def log_call(command, stdin=None, env=None, shell=False):
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE)
|
stderr=subprocess.PIPE)
|
||||||
|
|
||||||
def stream_readline(args):
|
|
||||||
stream, q = args
|
|
||||||
for line in iter(stream.readline, ''):
|
|
||||||
q.put((stream, line.strip()))
|
|
||||||
|
|
||||||
if stdin is not None:
|
if stdin is not None:
|
||||||
log.debug(' stdin: ' + stdin)
|
log.debug(' stdin: ' + stdin)
|
||||||
process.stdin.write(stdin + "\n")
|
process.stdin.write(stdin + "\n")
|
||||||
|
@ -35,23 +29,28 @@ def log_call(command, stdin=None, env=None, shell=False):
|
||||||
|
|
||||||
stdout = []
|
stdout = []
|
||||||
stderr = []
|
stderr = []
|
||||||
q = Queue.Queue()
|
|
||||||
|
def handle_stdout(line):
|
||||||
|
log.debug(line)
|
||||||
|
stdout.append(line)
|
||||||
|
|
||||||
|
def handle_stderr(line):
|
||||||
|
log.error(line)
|
||||||
|
stderr.append(line)
|
||||||
|
|
||||||
|
handlers = {process.stdout: handle_stdout,
|
||||||
|
process.stderr: handle_stderr}
|
||||||
|
|
||||||
|
def stream_readline(stream):
|
||||||
|
for line in iter(stream.readline, ''):
|
||||||
|
handlers[stream](line.strip())
|
||||||
|
|
||||||
pool = ThreadPool(2)
|
pool = ThreadPool(2)
|
||||||
pool.map(stream_readline, [(process.stdout, q), (process.stderr, q)])
|
pool.map(stream_readline, [process.stdout, process.stderr])
|
||||||
pool.close()
|
pool.close()
|
||||||
while True:
|
pool.join()
|
||||||
try:
|
process.wait()
|
||||||
stream, output = q.get_nowait()
|
return process.returncode, stdout, stderr
|
||||||
if stream is process.stdout:
|
|
||||||
log.debug(output)
|
|
||||||
stdout.append(output)
|
|
||||||
elif stream is process.stderr:
|
|
||||||
log.error(output)
|
|
||||||
stderr.append(output)
|
|
||||||
except Queue.Empty:
|
|
||||||
pool.join()
|
|
||||||
process.wait()
|
|
||||||
return process.returncode, stdout, stderr
|
|
||||||
|
|
||||||
|
|
||||||
def sed_i(file_path, pattern, subst):
|
def sed_i(file_path, pattern, subst):
|
||||||
|
|
|
@ -18,7 +18,7 @@ def setup_logger():
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def test_log_call():
|
def test_log_call_output_order():
|
||||||
logged = setup_logger()
|
logged = setup_logger()
|
||||||
fixture = """
|
fixture = """
|
||||||
2 0.1 one\\\\n
|
2 0.1 one\\\\n
|
||||||
|
|
Loading…
Add table
Reference in a new issue