Merge pull request #28812 from isbm/isbm-which-decorator-enhancement

Enhance 'which' decorator reliability
This commit is contained in:
Mike Place 2015-11-17 08:32:10 -07:00
commit 73719928f9

View file

@ -520,11 +520,7 @@ def which(exe=None):
# executable in cwd or fullpath
return exe
# default path based on busybox's default
default_path = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin'
search_path = os.environ.get('PATH', default_path)
path_ext = os.environ.get('PATHEXT', '.EXE')
ext_list = path_ext.split(';')
ext_list = os.environ.get('PATHEXT', '.EXE').split(';')
@real_memoize
def _exe_has_ext():
@ -541,7 +537,13 @@ def which(exe=None):
continue
return False
search_path = search_path.split(os.pathsep)
# Enhance POSIX path for the reliability at some environments, when $PATH is changing
# This also keeps order, where 'first came, first win' for cases to find optional alternatives
search_path = os.environ.get('PATH') and os.environ['PATH'].split(os.pathsep) or list()
for default_path in ['/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin']:
if default_path not in search_path:
search_path.append(default_path)
os.environ['PATH'] = os.pathsep.join(search_path)
for path in search_path:
full_path = os.path.join(path, exe)
if _is_executable_file_or_link(full_path):
@ -554,16 +556,10 @@ def which(exe=None):
# safely rely on that behavior
if _is_executable_file_or_link(full_path + ext):
return full_path + ext
log.trace(
'{0!r} could not be found in the following search '
'path: {1!r}'.format(
exe, search_path
)
)
log.trace('{0!r} could not be found in the following search path: {1!r}'.format(exe, search_path))
else:
log.error(
'No executable was passed to be searched by salt.utils.which()'
)
log.error('No executable was passed to be searched by salt.utils.which()')
return None