Fix for GitHub #30 and SPACK-70.

This commit is contained in:
Todd Gamblin 2015-04-14 15:11:41 -07:00
parent 8b14a46201
commit 5c04b3c480

View File

@ -28,8 +28,10 @@
import sys import sys
import re import re
import subprocess import subprocess
import inspect
import llnl.util.tty as tty import llnl.util.tty as tty
import spack
import spack.error import spack.error
class Executable(object): class Executable(object):
@ -38,6 +40,9 @@ def __init__(self, name):
self.exe = name.split(' ') self.exe = name.split(' ')
self.returncode = None self.returncode = None
if not self.exe:
raise ProcessError("Cannot construct executable for '%s'" % name)
def add_default_arg(self, arg): def add_default_arg(self, arg):
self.exe.append(arg) self.exe.append(arg)
@ -63,7 +68,9 @@ def __call__(self, *args, **kwargs):
"Consider removing them") "Consider removing them")
cmd = self.exe + list(args) cmd = self.exe + list(args)
tty.debug(" ".join(cmd))
cmd_line = ' '.join(cmd)
tty.debug(cmd_line)
close_error = False close_error = False
try: try:
@ -79,16 +86,22 @@ def __call__(self, *args, **kwargs):
self.returncode = proc.returncode self.returncode = proc.returncode
if fail_on_error and proc.returncode != 0: if fail_on_error and proc.returncode != 0:
raise ProcessError("command '%s' returned error code %d" raise ProcessError("Command exited with status %d:"
% (" ".join(cmd), proc.returncode)) % proc.returncode, cmd_line)
if return_output: if return_output:
return out return out
except OSError, e:
raise ProcessError(
"%s: %s" % (self.exe[0], e.strerror),
"Command: " + cmd_line)
except subprocess.CalledProcessError, e: except subprocess.CalledProcessError, e:
if fail_on_error: if fail_on_error:
raise ProcessError( raise ProcessError(
"command '%s' failed to run." % ( str(e),
" ".join(cmd), proc.returncode), str(e)) "\nExit status %d when invoking command: %s"
% (proc.returncode, cmd_line))
finally: finally:
if close_error: if close_error:
@ -130,5 +143,27 @@ def which(name, **kwargs):
class ProcessError(spack.error.SpackError): class ProcessError(spack.error.SpackError):
def __init__(self, msg, *long_msg): def __init__(self, msg, long_msg=None):
super(ProcessError, self).__init__(msg, *long_msg) # Friendlier exception trace info for failed executables
long_msg = long_msg + "\n" if long_msg else ""
for f in inspect.stack():
frame = f[0]
loc = frame.f_locals
if 'self' in loc:
obj = loc['self']
if isinstance(obj, spack.Package):
long_msg += "---\n"
long_msg += "Context:\n"
long_msg += " %s:%d, in %s:\n" % (
inspect.getfile(frame.f_code),
frame.f_lineno,
frame.f_code.co_name)
lines, start = inspect.getsourcelines(frame)
for i, line in enumerate(lines):
mark = ">> " if start + i == frame.f_lineno else " "
long_msg += " %s%-5d%s\n" % (
mark, start + i, line.rstrip())
break
super(ProcessError, self).__init__(msg, long_msg)