Skip to content
Snippets Groups Projects
Commit cee7bfa9 authored by Todd Gamblin's avatar Todd Gamblin
Browse files

Simplify output redirection in spack.util.executable

- By default inherit parent's input/output descriptor
- Only use pipes if we need to return output.
- Allows subprocesses (like emacsclient) to detect terminal correctly
parent ad32f64e
No related branches found
No related tags found
No related merge requests found
......@@ -51,7 +51,7 @@ def _verify_executables(*paths):
def get_compiler_version(compiler_path, version_arg, regex='(.*)'):
if not compiler_path in _version_cache:
compiler = Executable(compiler_path)
output = compiler(version_arg, return_output=True, error=None)
output = compiler(version_arg, return_output=True, error=os.devnull)
match = re.search(regex, output)
_version_cache[compiler_path] = match.group(1) if match else 'unknown'
......
......@@ -60,16 +60,16 @@ def __call__(self, *args, **kwargs):
fail_on_error = kwargs.get("fail_on_error", True)
ignore_errors = kwargs.get("ignore_errors", ())
output = kwargs.get("output", sys.stdout)
error = kwargs.get("error", sys.stderr)
# Default values of None says to keep parent's file descriptors.
output = kwargs.get("output", None)
error = kwargs.get("error", None)
input = kwargs.get("input", None)
def streamify(arg, mode):
if isinstance(arg, basestring):
return open(arg, mode), True
elif arg is None and mode != 'r':
return open(os.devnull, mode), True
return arg, False
else:
return arg, False
output, ostream = streamify(output, 'w')
error, estream = streamify(error, 'w')
input, istream = streamify(input, 'r')
......@@ -92,19 +92,14 @@ def streamify(arg, mode):
tty.debug(cmd_line)
try:
if return_output:
output = subprocess.PIPE
proc = subprocess.Popen(
cmd,
stdin=input,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
cmd, stdin=input, stderr=error, stdout=output)
out, err = proc.communicate()
self.returncode = proc.returncode
if not return_output:
output.write(out)
error.write(err)
rc = proc.returncode
rc = self.returncode = proc.returncode
if fail_on_error and rc != 0 and (rc not in ignore_errors):
raise ProcessError("Command exited with status %d:"
% proc.returncode, cmd_line)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment