Skip to content

Commit 848a80c

Browse files
committed
Don't create temporary files
1 parent 7eea60d commit 848a80c

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

pipeline/compilers/__init__.py

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import os
2-
import shutil
32
import subprocess
4-
from tempfile import NamedTemporaryFile
53

64
from django.contrib.staticfiles import finders
75
from django.contrib.staticfiles.storage import staticfiles_storage
@@ -114,37 +112,25 @@ def execute_command(self, command, cwd=None, stdout_captured=None):
114112
# The first element in argument_list is the program that will be executed; if it is '', then
115113
# a PermissionError will be raised. Thus empty arguments are filtered out from argument_list
116114
argument_list = list(filter(None, argument_list))
117-
stdout = None
118115
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()
134121

135122
# User wants to see everything that happened.
136123
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)
140126
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

Comments
 (0)