|
13 | 13 | """Placeholder docstring"""
|
14 | 14 | from __future__ import absolute_import
|
15 | 15 |
|
| 16 | +import io |
16 | 17 | import os
|
17 | 18 | import subprocess
|
18 | 19 | import sys
|
|
24 | 25 |
|
25 | 26 |
|
26 | 27 | def create(cmd, error_class, cwd=None, capture_error=False, **kwargs):
|
27 |
| - """Placeholder docstring""" |
| 28 | + """Create subprocess.Popen object for the given command. |
| 29 | +
|
| 30 | + Args: |
| 31 | + cmd (list): The command to be run. |
| 32 | + error_class (cls): The class to use when raising an exception. |
| 33 | + cwd (str): The location from which to run the command (default: None). |
| 34 | + If None, this defaults to the ``code_dir`` of the environment. |
| 35 | + capture_error (bool): whether or not to direct stderr to a stream |
| 36 | + that can later be read (default: False). |
| 37 | + **kwargs: Extra arguments that are passed to the subprocess.Popen constructor. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + subprocess.Popen: the process for the given command |
| 41 | +
|
| 42 | + Raises: |
| 43 | + error_class: if there is an exception raised when creating the process |
| 44 | + """ |
28 | 45 | try:
|
| 46 | + # Capture both so that we can control the order of when stdout and stderr are streamed |
| 47 | + stdout = subprocess.PIPE if capture_error else None |
29 | 48 | stderr = subprocess.PIPE if capture_error else None
|
| 49 | + |
30 | 50 | return subprocess.Popen(
|
31 |
| - cmd, env=os.environ, cwd=cwd or _env.code_dir, stderr=stderr, **kwargs |
| 51 | + cmd, env=os.environ, cwd=cwd or _env.code_dir, stdout=stdout, stderr=stderr, **kwargs |
32 | 52 | )
|
33 | 53 | except Exception as e: # pylint: disable=broad-except
|
34 | 54 | six.reraise(error_class, error_class(e), sys.exc_info()[2])
|
35 | 55 |
|
36 | 56 |
|
37 | 57 | def check_error(cmd, error_class, capture_error=False, **kwargs):
|
38 | 58 | # type: (List[str], type, bool, Mapping[str, object]) -> subprocess.Popen
|
39 |
| - """Placeholder docstring""" |
| 59 | + """Run a commmand, raising an exception if there is an error. |
| 60 | +
|
| 61 | + Args: |
| 62 | + cmd (list): The command to be run. |
| 63 | + error_class (cls): The class to use when raising an exception. |
| 64 | + capture_error (bool): whether or not to include stderr in |
| 65 | + the exception message (default: False). In either case, |
| 66 | + stderr is streamed to the process's output. |
| 67 | + **kwargs: Extra arguments that are passed to the subprocess.Popen constructor. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + subprocess.Popen: the process for the given command |
| 71 | +
|
| 72 | + Raises: |
| 73 | + error_class: if there is an exception raised when creating the process |
| 74 | + """ |
40 | 75 | process = create(cmd, error_class, capture_error=capture_error, **kwargs)
|
41 | 76 |
|
42 | 77 | if capture_error:
|
43 |
| - _, stderr = process.communicate() |
44 |
| - return_code = process.poll() |
| 78 | + # Create a copy of stderr so that it can be read after being streamed |
| 79 | + with io.BytesIO() as stderr_copy: |
| 80 | + return_code = process.poll() |
| 81 | + while return_code is None: |
| 82 | + stdout = process.stdout.readline() |
| 83 | + sys.stdout.write(stdout.decode("utf-8")) |
| 84 | + stderr = process.stderr.readline() |
| 85 | + sys.stdout.write(stderr.decode("utf-8")) |
| 86 | + |
| 87 | + stderr_copy.write(stderr) |
| 88 | + return_code = process.poll() |
| 89 | + |
| 90 | + # Read the rest of stdout/stdin because readline() reads only one line at a time |
| 91 | + stdout = process.stdout.read() |
| 92 | + sys.stdout.write(stdout.decode("utf-8")) |
| 93 | + stderr = process.stderr.read() |
| 94 | + sys.stdout.write(stderr.decode("utf-8")) |
| 95 | + |
| 96 | + stderr_copy.write(stderr) |
| 97 | + full_stderr = stderr_copy.getvalue() |
45 | 98 | else:
|
46 |
| - stderr = None |
| 99 | + full_stderr = None |
47 | 100 | return_code = process.wait()
|
48 | 101 |
|
49 | 102 | if return_code:
|
50 |
| - raise error_class(return_code=return_code, cmd=" ".join(cmd), output=stderr) |
| 103 | + raise error_class(return_code=return_code, cmd=" ".join(cmd), output=full_stderr) |
51 | 104 | return process
|
52 | 105 |
|
53 | 106 |
|
|
0 commit comments