|
1 | 1 | import os |
2 | | -import shutil |
3 | 2 | import subprocess |
4 | | -from tempfile import NamedTemporaryFile |
5 | 3 |
|
6 | 4 | from django.contrib.staticfiles import finders |
7 | 5 | from django.contrib.staticfiles.storage import staticfiles_storage |
@@ -114,37 +112,25 @@ def execute_command(self, command, cwd=None, stdout_captured=None): |
114 | 112 | # The first element in argument_list is the program that will be executed; if it is '', then |
115 | 113 | # a PermissionError will be raised. Thus empty arguments are filtered out from argument_list |
116 | 114 | argument_list = list(filter(None, argument_list)) |
117 | | - stdout = None |
118 | 115 | try: |
119 | | - # We always catch stdout in a file, but we may not have a use for it. |
120 | | - temp_file_container = cwd or os.path.dirname(stdout_captured or "") or os.getcwd() |
121 | | - with NamedTemporaryFile('wb', delete=False, dir=temp_file_container) as stdout: |
122 | | - compiling = subprocess.Popen(argument_list, cwd=cwd, |
123 | | - stdout=stdout, |
124 | | - stderr=subprocess.PIPE) |
125 | | - _, stderr = compiling.communicate() |
126 | | - set_std_streams_blocking() |
127 | | - |
128 | | - if compiling.returncode != 0: |
129 | | - stdout_captured = None # Don't save erroneous result. |
130 | | - raise CompilerError( |
131 | | - f"{argument_list!r} exit code {compiling.returncode}\n{stderr}", |
132 | | - command=argument_list, |
133 | | - error_output=stderr) |
| 116 | + compiling = subprocess.run( |
| 117 | + argument_list, cwd=cwd, check=True, |
| 118 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE |
| 119 | + ) |
| 120 | + set_std_streams_blocking() |
134 | 121 |
|
135 | 122 | # User wants to see everything that happened. |
136 | 123 | if self.verbose: |
137 | | - with open(stdout.name, 'rb') as out: |
138 | | - print(out.read()) |
139 | | - print(stderr) |
| 124 | + print(compiling.stdout) |
| 125 | + print(compiling.stderr) |
140 | 126 | except OSError as e: |
141 | | - stdout_captured = None # Don't save erroneous result. |
142 | | - raise CompilerError(e, command=argument_list, |
143 | | - error_output=str(e)) |
144 | | - finally: |
145 | | - # Decide what to do with captured stdout. |
146 | | - if stdout: |
147 | | - if stdout_captured: |
148 | | - shutil.move(stdout.name, os.path.join(cwd or os.curdir, stdout_captured)) |
149 | | - else: |
150 | | - os.remove(stdout.name) |
| 127 | + raise CompilerError(e, command=argument_list, error_output=str(e)) |
| 128 | + except subprocess.CalledProcessError as e: |
| 129 | + raise CompilerError( |
| 130 | + f"{argument_list!r} exit code {e.returncode}\n{e.stderr}", |
| 131 | + command=e.cmd, error_output=e.stderr |
| 132 | + ) |
| 133 | + else: |
| 134 | + if stdout_captured: |
| 135 | + with open(os.path.join(cwd or os.curdir, stdout_captured), 'wb') as f: |
| 136 | + f.write(compiling.stdout) |
0 commit comments