Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame^] | 1 | from subprocess import CalledProcessError |
| 2 | from subprocess import check_output |
| 3 | from subprocess import STDOUT |
| 4 | |
| 5 | |
| 6 | class 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 | |
| 13 | class 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 |