provide a clearer error message in case Popen'ed executable is not executable

subprocess.Popen() will give a proper error message if the first
argument is an absolute path, but if it's a bare command name, it will
complain with a "Permission Denied" error.

This change provides a clear error every time.

If speed is proven to have an impact on this code, we could add
the "Logs.verbose > 0" precondition or add memoization.
This commit is contained in:
Jérôme Carretero 2013-08-02 12:39:21 -04:00
parent a40dbe073b
commit f3f9e9f99d
2 changed files with 30 additions and 2 deletions

View File

@ -327,6 +327,9 @@ class Context(ctx):
if 'stderr' not in kw:
kw['stderr'] = subprocess.PIPE
if not kw['shell'] and Utils.check_exe(cmd[0]) is None:
raise Errors.WafError("Program %s not found!" % cmd[0])
try:
if kw['stdout'] or kw['stderr']:
p = subprocess.Popen(cmd, **kw)
@ -388,6 +391,9 @@ class Context(ctx):
else:
to_ret = STDOUT
if not kw['shell'] and Utils.check_exe(cmd[0]) is None:
raise Errors.WafError("Program %s not found!" % cmd[0])
kw['stdout'] = kw['stderr'] = subprocess.PIPE
if quiet is None:
self.to_log(cmd)

View File

@ -440,8 +440,8 @@ def check_dir(path):
"""
Ensure that a directory exists (similar to ``mkdir -p``).
:type dir: string
:param dir: Path to directory
:type path: string
:param path: Path to directory
"""
if not os.path.isdir(path):
try:
@ -450,6 +450,28 @@ def check_dir(path):
if not os.path.isdir(path):
raise Errors.WafError('Cannot create the folder %r' % path, ex=e)
def check_exe(name):
"""
Ensure that a program exists
:type name: string
:param name: name or path to program
:return: path of the program or None
"""
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(name)
if fpath and is_exe(name):
return fpath
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, name)
if is_exe(exe_file):
return exe_file
return None
def def_attrs(cls, **kw):
"""
Set default attributes on a class instance