From b9f52eba67f7940ba69ace10135d790b5b8cc7b0 2009-04-24 22:38:50 From: Brian Granger Date: 2009-04-24 22:38:50 Subject: [PATCH] Moving find_exe -> platutils.find_cmd and making is cross platform. The find_cmd function used to be in ipcluster.py to find executables on win32. We need this on all platforms and now we have it! --- diff --git a/IPython/iplib.py b/IPython/iplib.py index 67cef06..c6515ef 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -152,7 +152,11 @@ def user_setup(ipythondir,rc_suffix,mode='install',interactive=True): printf = lambda s : None # Install mode should be re-entrant: if the install dir already exists, - # bail out cleanly + # bail out cleanly. + # XXX. This is too hasty to return. We need to check to make sure that + # all the expected config files and directories are actually there. We + # currently have a failure mode if someone deletes a needed config file + # but still has the ipythondir. if mode == 'install' and os.path.isdir(ipythondir): return diff --git a/IPython/kernel/scripts/ipcluster.py b/IPython/kernel/scripts/ipcluster.py index dcebf04..34b2d61 100755 --- a/IPython/kernel/scripts/ipcluster.py +++ b/IPython/kernel/scripts/ipcluster.py @@ -54,22 +54,10 @@ from IPython.kernel.fcutil import have_crypto from IPython.kernel.twistedutil import gatherBoth, wait_for_file from IPython.kernel.util import printer - #----------------------------------------------------------------------------- # General process handling code #----------------------------------------------------------------------------- -def find_exe(cmd): - try: - import win32api - except ImportError: - raise ImportError('you need to have pywin32 installed for this to work') - else: - try: - (path, offest) = win32api.SearchPath(os.environ['PATH'],cmd + '.exe') - except: - (path, offset) = win32api.SearchPath(os.environ['PATH'],cmd + '.bat') - return path class ProcessStateError(Exception): pass diff --git a/IPython/platutils.py b/IPython/platutils.py index 6d1d94c..197d489 100644 --- a/IPython/platutils.py +++ b/IPython/platutils.py @@ -61,6 +61,33 @@ def set_term_title(title): _platutils.set_term_title(title) +class FindCmdError(Exception): + pass + +def find_cmd(cmd): + """Find full path to executable cmd in a cross platform manner. + + This function tries to determine the full path to a command line program + using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the + time it will use the version that is first on the users `PATH`. If + cmd is `python` return `sys.executable`. + + Parameters + ---------- + cmd : str + The command line program to look for. + """ + if cmd == 'python': + return sys.executable + try: + path = _platutils.find_cmd(cmd) + except: + raise FindCmdError('command could not be found: %s' % cmd) + # which returns empty if not found + if path == '': + raise FindCmdError('command could not be found: %s' % cmd) + return path + #----------------------------------------------------------------------------- # Deprecated functions #----------------------------------------------------------------------------- diff --git a/IPython/platutils_dummy.py b/IPython/platutils_dummy.py index 1066ac1..35aa5a0 100644 --- a/IPython/platutils_dummy.py +++ b/IPython/platutils_dummy.py @@ -23,3 +23,7 @@ ignore_termtitle = True def set_term_title(*args,**kw): """Dummy no-op.""" pass + +def find_cmd(cmd): + """Find the full path to a command using which.""" + return os.popen('which %s' % cmd).read().strip() diff --git a/IPython/platutils_posix.py b/IPython/platutils_posix.py index e4d162b..d0700a2 100644 --- a/IPython/platutils_posix.py +++ b/IPython/platutils_posix.py @@ -30,3 +30,7 @@ if os.environ.get('TERM','') == 'xterm': set_term_title = _set_term_title_xterm else: set_term_title = _dummy_op + +def find_cmd(cmd): + """Find the full path to a command using which.""" + return os.popen('which %s' % cmd).read().strip() diff --git a/IPython/platutils_win32.py b/IPython/platutils_win32.py index 36f9b31..10fe9b9 100644 --- a/IPython/platutils_win32.py +++ b/IPython/platutils_win32.py @@ -41,3 +41,16 @@ except ImportError: if ret: # non-zero return code signals error, don't try again ignore_termtitle = True + +def find_cmd(cmd): + """Find the full path to a .bat or .exe using the win32api module.""" + try: + import win32api + except ImportError: + raise ImportError('you need to have pywin32 installed for this to work') + else: + try: + (path, offest) = win32api.SearchPath(os.environ['PATH'],cmd + '.exe') + except: + (path, offset) = win32api.SearchPath(os.environ['PATH'],cmd + '.bat') + return path