From 1e2f421faa62455033ea6c3db941221723da3599 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Thu, 23 Jul 2015 17:01:33 -0700 Subject: Fix Python 2.6 compatibility issue. --- lib/spack/spack/cmd/find.py | 2 +- lib/spack/spack/util/executable.py | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/cmd/find.py b/lib/spack/spack/cmd/find.py index a0232d2f12..3c993990b1 100644 --- a/lib/spack/spack/cmd/find.py +++ b/lib/spack/spack/cmd/find.py @@ -92,7 +92,7 @@ def display_specs(specs, **kwargs): # Print one spec per line along with prefix path width = max(len(s) for s in abbreviated) width += 2 - format = " %-{}s%s".format(width) + format = " %%-%ds%%s" % width for abbrv, spec in zip(abbreviated, specs): if hashes: diff --git a/lib/spack/spack/util/executable.py b/lib/spack/spack/util/executable.py index 67e8cddd98..d1dfb62ffb 100644 --- a/lib/spack/spack/util/executable.py +++ b/lib/spack/spack/util/executable.py @@ -34,6 +34,7 @@ import llnl.util.tty as tty import spack import spack.error + class Executable(object): """Class representing a program that can be run on the command line.""" def __init__(self, name): @@ -58,7 +59,20 @@ class Executable(object): return_output = kwargs.get("return_output", False) 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) + 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 + output, ostream = streamify(output, 'w') + error, estream = streamify(error, 'w') + input, istream = streamify(input, 'r') # if they just want to ignore one error code, make it a tuple. if isinstance(ignore_errors, int): @@ -77,16 +91,12 @@ class Executable(object): cmd_line = ' '.join(cmd) tty.debug(cmd_line) - close_error = False try: - if error is None: - error = open(os.devnull, 'w') - close_error = True - proc = subprocess.Popen( cmd, + stdin=input, stderr=error, - stdout=subprocess.PIPE if return_output else sys.stdout) + stdout=subprocess.PIPE if return_output else output) out, err = proc.communicate() self.returncode = proc.returncode @@ -110,8 +120,9 @@ class Executable(object): % (proc.returncode, cmd_line)) finally: - if close_error: - error.close() + if ostream: output.close() + if estream: error.close() + if istream: input.close() def __eq__(self, other): -- cgit v1.2.3-60-g2f50