Browse Source

Move PathToPythonInterpreter function to YCM

Create a module for paths.
micbou 9 years ago
parent
commit
87854f0245
3 changed files with 85 additions and 14 deletions
  1. 5 6
      autoload/youcompleteme.vim
  2. 77 0
      python/ycm/paths.py
  3. 3 8
      python/ycm/youcompleteme.py

+ 5 - 6
autoload/youcompleteme.vim

@@ -130,20 +130,19 @@ from ycm import base
 base.LoadJsonDefaultsIntoVim()
 from ycmd import user_options_store
 user_options_store.SetAll( base.BuildServerConf() )
-from ycm import vimsupport
+from ycm import paths, vimsupport
 
-popen_args = [ utils.PathToPythonInterpreter(),
-               os.path.join( script_folder,
-                             '../third_party/ycmd/check_core_version.py') ]
+popen_args = [ paths.PathToPythonInterpreter(),
+               paths.PathToCheckCoreVersion() ]
 
 if utils.SafePopen( popen_args ).wait() == 2:
   vimsupport.PostVimMessage(
     'YouCompleteMe unavailable: YCM support libs too old, PLEASE RECOMPILE' )
-  vim.command( 'return 0')
+  vim.command( 'return 0' )
 
 from ycm.youcompleteme import YouCompleteMe
 ycm_state = YouCompleteMe( user_options_store.GetAll() )
-vim.command( 'return 1')
+vim.command( 'return 1' )
 EOF
 endfunction
 

+ 77 - 0
python/ycm/paths.py

@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2015 YouCompleteMe contributors.
+#
+# This file is part of YouCompleteMe.
+#
+# YouCompleteMe is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# YouCompleteMe is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with YouCompleteMe.  If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import vim
+import functools
+from ycmd import utils
+
+DIR_OF_CURRENT_SCRIPT = os.path.dirname( os.path.abspath( __file__ ) )
+
+WIN_PYTHON27_PATH = 'C:\python27\python.exe'
+WIN_PYTHON26_PATH = 'C:\python26\python.exe'
+
+
+def Memoize( obj ):
+  cache = obj.cache = {}
+
+  @functools.wraps( obj )
+  def memoizer( *args, **kwargs ):
+    key = str( args ) + str( kwargs )
+    if key not in cache:
+      cache[ key ] = obj( *args, **kwargs )
+    return cache[ key ]
+  return memoizer
+
+
+@Memoize
+def PathToPythonInterpreter():
+  user_path_to_python = vim.eval( 'g:ycm_path_to_python_interpreter' )
+
+  if user_path_to_python:
+    return user_path_to_python
+
+  # We check for 'python2' before 'python' because some OS's (I'm looking at
+  # you Arch Linux) have made the... interesting decision to point
+  # /usr/bin/python to python3.
+  python_names = [ 'python2', 'python' ]
+
+  path_to_python = utils.PathToFirstExistingExecutable( python_names )
+  if path_to_python:
+    return path_to_python
+
+  # On Windows, Python may not be on the PATH at all, so we check some common
+  # install locations.
+  if utils.OnWindows():
+    if os.path.exists( WIN_PYTHON27_PATH ):
+      return WIN_PYTHON27_PATH
+    elif os.path.exists( WIN_PYTHON26_PATH ):
+      return WIN_PYTHON26_PATH
+
+  raise RuntimeError( 'Python 2.7/2.6 not installed!' )
+
+
+def PathToServerScript():
+  return os.path.join( DIR_OF_CURRENT_SCRIPT, '..', '..', 'third_party',
+                       'ycmd', 'ycmd' )
+
+
+def PathToCheckCoreVersion():
+  return os.path.join( DIR_OF_CURRENT_SCRIPT, '..', '..', 'third_party',
+                       'ycmd', 'check_core_version.py' )

+ 3 - 8
python/ycm/youcompleteme.py

@@ -25,7 +25,7 @@ import re
 import signal
 import base64
 from subprocess import PIPE
-from ycm import vimsupport
+from ycm import paths, vimsupport
 from ycmd import utils
 from ycmd.request_wrap import RequestWrap
 from ycm.diagnostic_interface import DiagnosticInterface
@@ -113,8 +113,8 @@ class YouCompleteMe( object ):
       json.dump( options_dict, options_file )
       options_file.flush()
 
-      args = [ utils.PathToPythonInterpreter(),
-               _PathToServerScript(),
+      args = [ paths.PathToPythonInterpreter(),
+               paths.PathToServerScript(),
                '--port={0}'.format( server_port ),
                '--options_file={0}'.format( options_file.name ),
                '--log={0}'.format( self._user_options[ 'server_log_level' ] ),
@@ -556,11 +556,6 @@ class YouCompleteMe( object ):
         extra_conf_vim_data )
 
 
-def _PathToServerScript():
-  dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
-  return os.path.join( dir_of_current_script, '../../third_party/ycmd/ycmd' )
-
-
 def _AddUltiSnipsDataIfNeeded( extra_data ):
   if not USE_ULTISNIPS_DATA:
     return