Enhance 'which' decorator reliability for peculiar environments

This commit is contained in:
Bo Maryniuk 2015-11-12 10:34:36 +01:00
parent 30ea94439c
commit 1c59eedec2

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,9 @@ 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
default_path = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin'
search_path = list(set(os.pathsep.join([os.environ.get('PATH', ''), default_path]).split(os.pathsep)))
for path in search_path:
full_path = os.path.join(path, exe)
if _is_executable_file_or_link(full_path):
@ -554,16 +552,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