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
This commit is contained in:
Todd Gamblin 2016-01-18 00:01:30 -08:00
parent ad32f64ef6
commit cee7bfa9f0
2 changed files with 11 additions and 16 deletions

View File

@ -51,7 +51,7 @@ def _verify_executables(*paths):
def get_compiler_version(compiler_path, version_arg, regex='(.*)'): def get_compiler_version(compiler_path, version_arg, regex='(.*)'):
if not compiler_path in _version_cache: if not compiler_path in _version_cache:
compiler = Executable(compiler_path) 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) match = re.search(regex, output)
_version_cache[compiler_path] = match.group(1) if match else 'unknown' _version_cache[compiler_path] = match.group(1) if match else 'unknown'

View File

@ -60,15 +60,15 @@ def __call__(self, *args, **kwargs):
fail_on_error = kwargs.get("fail_on_error", True) fail_on_error = kwargs.get("fail_on_error", True)
ignore_errors = kwargs.get("ignore_errors", ()) ignore_errors = kwargs.get("ignore_errors", ())
output = kwargs.get("output", sys.stdout) # Default values of None says to keep parent's file descriptors.
error = kwargs.get("error", sys.stderr) output = kwargs.get("output", None)
error = kwargs.get("error", None)
input = kwargs.get("input", None) input = kwargs.get("input", None)
def streamify(arg, mode): def streamify(arg, mode):
if isinstance(arg, basestring): if isinstance(arg, basestring):
return open(arg, mode), True return open(arg, mode), True
elif arg is None and mode != 'r': else:
return open(os.devnull, mode), True
return arg, False return arg, False
output, ostream = streamify(output, 'w') output, ostream = streamify(output, 'w')
error, estream = streamify(error, 'w') error, estream = streamify(error, 'w')
@ -92,19 +92,14 @@ def streamify(arg, mode):
tty.debug(cmd_line) tty.debug(cmd_line)
try: try:
if return_output:
output = subprocess.PIPE
proc = subprocess.Popen( proc = subprocess.Popen(
cmd, cmd, stdin=input, stderr=error, stdout=output)
stdin=input,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
out, err = proc.communicate() out, err = proc.communicate()
self.returncode = proc.returncode
if not return_output: rc = self.returncode = proc.returncode
output.write(out)
error.write(err)
rc = proc.returncode
if fail_on_error and rc != 0 and (rc not in ignore_errors): if fail_on_error and rc != 0 and (rc not in ignore_errors):
raise ProcessError("Command exited with status %d:" raise ProcessError("Command exited with status %d:"
% proc.returncode, cmd_line) % proc.returncode, cmd_line)