|
13 | 13 | """Placeholder docstring"""
|
14 | 14 | from __future__ import absolute_import
|
15 | 15 |
|
16 |
| -import io |
17 | 16 | import os
|
18 | 17 | import subprocess
|
19 | 18 | import sys
|
|
25 | 24 |
|
26 | 25 |
|
27 | 26 | def create(cmd, error_class, cwd=None, capture_error=False, **kwargs):
|
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 |
| - """ |
| 27 | + """Placeholder docstring""" |
45 | 28 | 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 |
48 | 29 | stderr = subprocess.PIPE if capture_error else None
|
49 |
| - |
50 | 30 | return subprocess.Popen(
|
51 |
| - cmd, env=os.environ, cwd=cwd or _env.code_dir, stdout=stdout, stderr=stderr, **kwargs |
| 31 | + cmd, env=os.environ, cwd=cwd or _env.code_dir, stderr=stderr, **kwargs |
52 | 32 | )
|
53 | 33 | except Exception as e: # pylint: disable=broad-except
|
54 | 34 | six.reraise(error_class, error_class(e), sys.exc_info()[2])
|
55 | 35 |
|
56 | 36 |
|
57 | 37 | def check_error(cmd, error_class, capture_error=False, **kwargs):
|
58 | 38 | # type: (List[str], type, bool, Mapping[str, object]) -> subprocess.Popen
|
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 |
| - """ |
| 39 | + """Placeholder docstring""" |
75 | 40 | process = create(cmd, error_class, capture_error=capture_error, **kwargs)
|
76 | 41 |
|
77 | 42 | if capture_error:
|
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() |
| 43 | + _, stderr = process.communicate() |
| 44 | + return_code = process.poll() |
98 | 45 | else:
|
99 |
| - full_stderr = None |
| 46 | + stderr = None |
100 | 47 | return_code = process.wait()
|
101 | 48 |
|
102 | 49 | if return_code:
|
103 |
| - raise error_class(return_code=return_code, cmd=" ".join(cmd), output=full_stderr) |
| 50 | + raise error_class(return_code=return_code, cmd=" ".join(cmd), output=stderr) |
104 | 51 | return process
|
105 | 52 |
|
106 | 53 |
|
|
0 commit comments