From bbfd53d7185a88cb6b48e6c9baa1786d5691ce30 Mon Sep 17 00:00:00 2001 From: nvbn Date: Sat, 6 Feb 2016 16:42:42 +0300 Subject: [PATCH] #N/A: Use `shutil.which` when possible --- thefuck/utils.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/thefuck/utils.py b/thefuck/utils.py index a5e43158..047dcc88 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -46,22 +46,26 @@ memoize.disabled = False @memoize def which(program): """Returns `program` path or `None`.""" + try: + from shutil import which - def is_exe(fpath): - return os.path.isfile(fpath) and os.access(fpath, os.X_OK) + return which(program) + except ImportError: + def is_exe(fpath): + return os.path.isfile(fpath) and os.access(fpath, os.X_OK) - fpath, fname = os.path.split(program) - if fpath: - if is_exe(program): - return program - else: - for path in os.environ["PATH"].split(os.pathsep): - path = path.strip('"') - exe_file = os.path.join(path, program) - if is_exe(exe_file): - return exe_file + fpath, fname = os.path.split(program) + if fpath: + if is_exe(program): + return program + else: + for path in os.environ["PATH"].split(os.pathsep): + path = path.strip('"') + exe_file = os.path.join(path, program) + if is_exe(exe_file): + return exe_file - return None + return None def default_settings(params):