blob: 9e6f29a25d45f6f7c64a60bba9e00afdd263895d [file] [log] [blame]
Diana Picus052b7d32017-11-24 16:19:41 +01001from subprocess import CalledProcessError
2from subprocess import check_output
3from subprocess import STDOUT
4
5
6class CommandPrinter(object):
7 """Command consumer that just prints the commands that it receives."""
8
9 def consume(self, command, directory):
10 print("{}$ {}".format(directory, ' '.join(command)))
11
12
13class CommandRunner(object):
14 """Command consumer that runs the commands that it receives."""
15
16 def consume(self, command, directory):
17 """
18 Run the given command in the given directory and print the stdout and
19 stderr. If an exception is thrown while running the command, it will be
20 rethrown as a RuntimeError.
21 """
22 # FIXME: This prints the results after the command has finished running.
23 # For long-running commands (e.g. an LLVM build) we'll want live
24 # output.
25 try:
26 print(str(check_output(command, stderr=STDOUT, cwd=directory), 'utf-8'))
27 except CalledProcessError as exc:
28 raise RuntimeError(
29 "Error while running command\n{}".format(str(exc.output, 'utf-8'))) from exc