2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-09 16:36:25 +00:00

chore: use shutil.which instead of find_executable

This commit is contained in:
Gavin D'souza 2019-12-20 14:45:56 +05:30
parent 07d23d6911
commit 9e3d5c8587

View File

@ -20,44 +20,6 @@ def log(message, level=0):
print(start + message + end) print(start + message + end)
def find_executable(executable, path=None):
"""Tries to find 'executable' in the directories listed in 'path'.
A string listing directories separated by 'os.pathsep'; defaults to
os.environ['PATH']. Returns the complete filename or None if not found.
source: https://github.com/python/cpython/blob/master/Lib/distutils/spawn.py
"""
_, ext = os.path.splitext(executable)
if (sys.platform == 'win32') and (ext != '.exe'):
executable = executable + '.exe'
if os.path.isfile(executable):
return executable
if path is None:
path = os.environ.get('PATH', None)
if path is None:
try:
path = os.confstr("CS_PATH")
except (AttributeError, ValueError):
# os.confstr() or CS_PATH is not available
path = os.defpath
# bpo-35755: Don't use os.defpath if the PATH environment variable is
# set to an empty string
# PATH='' doesn't match, whereas PATH=':' looks in the current directory
if not path:
return None
paths = path.split(os.pathsep)
for p in paths:
f = os.path.join(p, executable)
if os.path.isfile(f):
# the file exists, we have a shot at spawn working
return f
return None
def check_environment(): def check_environment():
needed_environ_vars = ['LANG', 'LC_ALL'] needed_environ_vars = ['LANG', 'LC_ALL']
message = '' message = ''
@ -77,7 +39,7 @@ def run_os_command(command_map):
success = True success = True
for executable, commands in command_map.items(): for executable, commands in command_map.items():
if find_executable(executable): if shutil.which(executable):
if isinstance(commands, str): if isinstance(commands, str):
commands = [commands] commands = [commands]
@ -97,7 +59,7 @@ def is_sudo_user():
def install_package(package): def install_package(package):
if find_executable(package): if shutil.which(package):
log("{} already installed!".format(package), level=1) log("{} already installed!".format(package), level=1)
else: else:
log("Installing {}...".format(package)) log("Installing {}...".format(package))
@ -138,7 +100,7 @@ def install_bench(args):
] ]
}) })
if not find_executable("git"): if not shutil.which("git"):
success = run_os_command({ success = run_os_command({
'brew': 'brew install git' 'brew': 'brew install git'
}) })
@ -148,7 +110,7 @@ def install_bench(args):
return return
# secure pip installation # secure pip installation
if find_executable('pip'): if shutil.which('pip'):
run_os_command({ run_os_command({
'pip': 'sudo -H pip install --upgrade setuptools cryptography pip' 'pip': 'sudo -H pip install --upgrade setuptools cryptography pip'
}) })
@ -284,7 +246,7 @@ def get_distribution_info():
def check_brew_installed(): def check_brew_installed():
if 'Darwin' in os.uname(): if 'Darwin' in os.uname():
if not find_executable('brew'): if not shutil.which('brew'):
raise Exception(''' raise Exception('''
Please install brew package manager before proceeding with bench setup. Please run following Please install brew package manager before proceeding with bench setup. Please run following
to install brew package manager on your machine, to install brew package manager on your machine,