aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiana Picus <diana.picus@linaro.org>2017-12-22 11:49:03 +0100
committerDiana Picus <diana.picus@linaro.org>2018-01-12 10:58:07 +0000
commit41f49b14abd6e4c805e76186a7c6e6d6e82c31da (patch)
tree6a4c15259fcf57cb942ad07e29fc8c28ca952c2d
parent9f75686d11ff62768d6b1f961b19dce174fa3ef3 (diff)
downloadlinaro-scripts-41f49b14abd6e4c805e76186a7c6e6d6e82c31da.tar.gz
Collect live output from subprocesses
When launching a command, we now collect its output during execution and print it as soon as it becomes available. This is necessary in order to be able to see progress in long-running commands such as CMake or Ninja. This requires some changes to the way in which we use the Subprocess module, since we need to use the Popen constructor. This does not throw an exception if the launched process returns an error - we now need to manually check the return code and throw an exception ourselves. Change-Id: I973d8bdec0bd77449672830476cb2eef11c6633a
-rw-r--r--modules/utils.py19
1 files changed, 12 insertions, 7 deletions
diff --git a/modules/utils.py b/modules/utils.py
index dac77c5..4e779d8 100644
--- a/modules/utils.py
+++ b/modules/utils.py
@@ -1,11 +1,13 @@
-from subprocess import CalledProcessError
-from subprocess import check_output
+from subprocess import PIPE
+from subprocess import Popen
from subprocess import STDOUT
+from subprocess import SubprocessError
import re
from linaropy.git.worktree import Worktree
+
class CommandPrinter(object):
"""Command consumer that just prints the commands that it receives."""
@@ -22,12 +24,15 @@ class CommandRunner(object):
stderr. If an exception is thrown while running the command, it will be
rethrown as a RuntimeError.
"""
- # FIXME: This prints the results after the command has finished running.
- # For long-running commands (e.g. an LLVM build) we'll want live
- # output.
try:
- print(str(check_output(command, stderr=STDOUT, cwd=directory), 'utf-8'))
- except CalledProcessError as exc:
+ with Popen(command, stdout=PIPE, stderr=STDOUT, cwd=directory) as process:
+ for line in process.stdout:
+ print(str(line, 'utf-8'), end='')
+ if process.returncode != 0:
+ raise RuntimeError(
+ "Command failed with return code: {}\n".format(
+ process.returncode))
+ except SubprocessError as exc:
raise RuntimeError(
"Error while running command\n{}".format(str(exc.output, 'utf-8'))) from exc