|
1 | 1 | """
|
2 | 2 | Wrapper around calling make through a subprocess.
|
3 | 3 | """
|
4 |
| - |
| 4 | +import io |
5 | 5 | import logging
|
| 6 | +import subprocess |
| 7 | +import shutil |
| 8 | +import threading |
6 | 9 |
|
7 | 10 | LOG = logging.getLogger(__name__)
|
8 | 11 |
|
@@ -82,14 +85,26 @@ def run(self, args, env=None, cwd=None):
|
82 | 85 |
|
83 | 86 | p = self.osutils.popen(invoke_make, stdout=self.osutils.pipe, stderr=self.osutils.pipe, cwd=cwd, env=env)
|
84 | 87 |
|
85 |
| - out, err = p.communicate() |
86 |
| - |
87 |
| - # Typically this type of information is logged to DEBUG, however, since the Make builder |
88 |
| - # can be different per customer's use case, it is helpful to always log the output so |
89 |
| - # developers can diagnose any issues. |
90 |
| - LOG.info(out.decode("utf8").strip()) |
91 |
| - |
92 |
| - if p.returncode != 0: |
93 |
| - raise MakeExecutionError(message=err.decode("utf8").strip()) |
94 |
| - |
95 |
| - return out.decode("utf8").strip() |
| 88 | + # Create a stdout variable that will contain the final stitched stdout result |
| 89 | + stdout = "" |
| 90 | + # Create a buffer and use a thread to gather the stderr stream into the buffer |
| 91 | + stderr_buf = io.BytesIO() |
| 92 | + stderr_thread = threading.Thread(target=shutil.copyfileobj, args=(p.stderr, stderr_buf), daemon=True) |
| 93 | + stderr_thread.start() |
| 94 | + |
| 95 | + # Log every stdout line by iterating |
| 96 | + for line in p.stdout: |
| 97 | + decoded_line = line.decode("utf-8").strip() |
| 98 | + LOG.info(decoded_line) |
| 99 | + # Gather total stdout |
| 100 | + stdout += decoded_line |
| 101 | + |
| 102 | + # Wait for the process to exit and stderr thread to end. |
| 103 | + return_code = p.wait() |
| 104 | + stderr_thread.join() |
| 105 | + |
| 106 | + if return_code != 0: |
| 107 | + # Raise an Error with the appropriate value from the stderr buffer. |
| 108 | + raise MakeExecutionError(message=stderr_buf.getvalue().decode("utf8").strip()) |
| 109 | + |
| 110 | + return stdout |
0 commit comments