summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2017-08-14 03:57:46 -0700
committerTodd Gamblin <tgamblin@llnl.gov>2017-08-20 16:51:10 -0700
commite0dd55e09074087f624443cd86439a854dc0cc18 (patch)
tree726efa4d60e60ae67843010157c1fdb04a86034a
parente77c1a20c5964c169d0ac557e13fd071804f68ed (diff)
downloadspack-e0dd55e09074087f624443cd86439a854dc0cc18.tar.gz
spack-e0dd55e09074087f624443cd86439a854dc0cc18.tar.bz2
spack-e0dd55e09074087f624443cd86439a854dc0cc18.tar.xz
spack-e0dd55e09074087f624443cd86439a854dc0cc18.zip
Make SpackCommand a bit more testable
- add fail_on_error argument - record exception and return code when the command fails
-rw-r--r--lib/spack/spack/main.py20
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/spack/spack/main.py b/lib/spack/spack/main.py
index d10047e325..b17cb3cdc5 100644
--- a/lib/spack/spack/main.py
+++ b/lib/spack/spack/main.py
@@ -369,13 +369,15 @@ class SpackCommand(object):
Use this to invoke Spack commands directly from Python and check
their stdout and stderr.
"""
- def __init__(self, command, fail_on_error=True):
+ def __init__(self, command):
"""Create a new SpackCommand that invokes ``command`` when called."""
self.parser = make_argument_parser()
self.parser.add_command(command)
self.command_name = command
self.command = spack.cmd.get_command(command)
- self.fail_on_error = fail_on_error
+
+ self.returncode = None
+ self.error = None
def __call__(self, *argv, **kwargs):
"""Invoke this SpackCommand.
@@ -384,17 +386,20 @@ class SpackCommand(object):
argv (list of str): command line arguments.
Keyword Args:
- color (optional bool): force-disable or force-enable color
+ fail_on_error (optional bool): Don't raise an exception on error
Returns:
(str, str): output and error as a strings
On return, if ``fail_on_error`` is False, return value of comman
- is set in ``returncode`` property. Otherwise, raise an error.
+ is set in ``returncode`` property, and the error is set in the
+ ``error`` property. Otherwise, raise an error.
"""
args, unknown = self.parser.parse_known_args(
[self.command_name] + list(argv))
+ fail_on_error = kwargs.get('fail_on_error', True)
+
out, err = sys.stdout, sys.stderr
ofd, ofn = tempfile.mkstemp()
efd, efn = tempfile.mkstemp()
@@ -408,6 +413,11 @@ class SpackCommand(object):
except SystemExit as e:
self.returncode = e.code
+ except:
+ self.error = sys.exc_info()[1]
+ if fail_on_error:
+ raise
+
finally:
sys.stdout.flush()
sys.stdout.close()
@@ -420,7 +430,7 @@ class SpackCommand(object):
os.unlink(ofn)
os.unlink(efn)
- if self.fail_on_error and self.returncode != 0:
+ if fail_on_error and self.returncode not in (None, 0):
raise SpackCommandError(
"Command exited with code %d: %s(%s)" % (
self.returncode, self.command_name,