From d31866270135b3360efddfb6e6d0a36096c57f42 Mon Sep 17 00:00:00 2001 From: Manuel Traut Date: Wed, 13 Dec 2017 08:02:24 +0100 Subject: print stdout/stderr during runtime If pyjustest is used with a test command, that prints an input request to stdout and afterwards waits for the input, it waits forever. The reason for this is, that stdout and stderr is printed only after the test command was executed completely. Instead of printing stdout and stderr after execution of the test-command print it as it occurs. This also provides meaningful timestamps if run in a timestamp wrapped environment like jenkins. Signed-off-by: Manuel Traut Reviewed-by: Anna-Maria Gleixner --- pyjutest | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/pyjutest b/pyjutest index 96766cb..ee04fdd 100755 --- a/pyjutest +++ b/pyjutest @@ -14,6 +14,7 @@ from subprocess import Popen, PIPE, STDOUT from optparse import OptionParser import os +import select import sys import time @@ -48,12 +49,39 @@ else: start = time.time() p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) -out,err = p.communicate() +fds = [ p.stdout, p.stderr ] + +out = "" +err = "" + +while True: + readable, writable, exceptional = select.select(fds, [], []) + done = 0 + + if p.stderr in readable: + l = p.stderr.readline().decode('utf-8') + if len(l): + print(l[:-1], file=sys.stderr) + sys.stderr.flush() + err += l + else: + done = 1 + + if p.stdout in readable: + l = p.stdout.readline().decode('utf-8') + if len(l): + print(l[:-1]) + sys.stdout.flush() + out += l + else: + if done: + break + +p.wait() duration = time.time() - start -tc = TestCase(opt.uname, opt.uclass, duration, out[:-1].decode('utf-8'), - err[:-1].decode('utf-8')) +tc = TestCase(opt.uname, opt.uclass, duration, out, err) if p.returncode: tc.add_failure_info(err, "return: %d" % p.returncode) @@ -65,10 +93,3 @@ with open('pyjutest.dat', 'wb') as dat: with open('pyjutest.xml', 'w') as xml: TestSuite.to_file(xml, tss, prettyprint=True) - -print("%s STDOUT:" % opt.uname) -for l in out.decode().split("\n"): - print(l) -print("%s STDERR:" % opt.uname) -for l in err.decode().split("\n"): - print(l, file=sys.stderr) -- cgit v1.2.3