2015-10-08 05:36:34 -04:00
|
|
|
# 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/>.
|
|
|
|
|
2016-02-27 19:12:24 -05:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from future import standard_library
|
|
|
|
standard_library.install_aliases()
|
|
|
|
from builtins import * # noqa
|
|
|
|
|
2015-10-08 05:36:34 -04:00
|
|
|
import os
|
2016-01-11 07:33:43 -05:00
|
|
|
import sys
|
2015-10-08 05:36:34 -04:00
|
|
|
import vim
|
|
|
|
import functools
|
2016-01-12 15:24:23 -05:00
|
|
|
import re
|
2015-10-08 05:36:34 -04:00
|
|
|
|
2016-02-28 17:39:55 -05:00
|
|
|
# Can't import these from setup.py because it makes nosetests go crazy.
|
2015-10-08 05:36:34 -04:00
|
|
|
DIR_OF_CURRENT_SCRIPT = os.path.dirname( os.path.abspath( __file__ ) )
|
2015-10-12 15:19:37 -04:00
|
|
|
DIR_OF_YCMD = os.path.join( DIR_OF_CURRENT_SCRIPT, '..', '..', 'third_party',
|
|
|
|
'ycmd' )
|
2016-01-11 07:33:43 -05:00
|
|
|
WIN_PYTHON_PATH = os.path.join( sys.exec_prefix, 'python.exe' )
|
2016-02-28 20:44:48 -05:00
|
|
|
PYTHON_BINARY_REGEX = re.compile(
|
|
|
|
r'python((2(\.[67])?)|(3(\.[3456789])?))?(.exe)?$' )
|
2015-10-08 05:36:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
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():
|
2015-10-12 15:19:37 -04:00
|
|
|
from ycmd import utils
|
|
|
|
|
2016-01-11 07:33:43 -05:00
|
|
|
python_interpreter = vim.eval( 'g:ycm_path_to_python_interpreter' )
|
2015-10-08 05:36:34 -04:00
|
|
|
|
2016-01-11 07:33:43 -05:00
|
|
|
if python_interpreter:
|
2016-01-23 10:58:24 -05:00
|
|
|
if IsPythonVersionCorrect( python_interpreter ):
|
|
|
|
return python_interpreter
|
2015-10-08 05:36:34 -04:00
|
|
|
|
2016-01-23 10:58:24 -05:00
|
|
|
raise RuntimeError( "Path in 'g:ycm_path_to_python_interpreter' option "
|
2016-02-28 20:44:48 -05:00
|
|
|
"does not point to a valid Python 2.6+ or 3.3+." )
|
2015-10-08 05:36:34 -04:00
|
|
|
|
2016-01-11 07:33:43 -05:00
|
|
|
# On UNIX platforms, we use sys.executable as the Python interpreter path.
|
|
|
|
# We cannot use sys.executable on Windows because for unknown reasons, it
|
|
|
|
# returns the Vim executable. Instead, we use sys.exec_prefix to deduce the
|
|
|
|
# interpreter path.
|
|
|
|
python_interpreter = ( WIN_PYTHON_PATH if utils.OnWindows() else
|
|
|
|
sys.executable )
|
2015-10-08 05:36:34 -04:00
|
|
|
|
2016-01-23 10:58:24 -05:00
|
|
|
if IsPythonVersionCorrect( python_interpreter ):
|
|
|
|
return python_interpreter
|
|
|
|
|
|
|
|
# As a last resort, we search python in the PATH. We check 'python2' before
|
|
|
|
# 'python' because on some distributions (Arch Linux for example), python
|
|
|
|
# refers to python3.
|
|
|
|
python_interpreter = utils.PathToFirstExistingExecutable( [ 'python2',
|
2016-02-28 20:44:48 -05:00
|
|
|
'python',
|
|
|
|
'python3' ] )
|
2016-01-23 10:58:24 -05:00
|
|
|
|
|
|
|
if IsPythonVersionCorrect( python_interpreter ):
|
|
|
|
return python_interpreter
|
2015-10-08 05:36:34 -04:00
|
|
|
|
2016-02-28 20:44:48 -05:00
|
|
|
raise RuntimeError( "Cannot find Python 2.6+ or 3.3+. You can set its path "
|
2016-01-23 10:58:24 -05:00
|
|
|
"using the 'g:ycm_path_to_python_interpreter' "
|
|
|
|
"option." )
|
2016-01-11 07:33:43 -05:00
|
|
|
|
|
|
|
|
2016-01-12 15:24:23 -05:00
|
|
|
def EndsWithPython( path ):
|
2016-02-28 20:44:48 -05:00
|
|
|
"""Check if given path ends with a python 2.6+ or 3.3+ name."""
|
2016-01-12 15:24:23 -05:00
|
|
|
return PYTHON_BINARY_REGEX.search( path ) is not None
|
|
|
|
|
|
|
|
|
|
|
|
def IsPythonVersionCorrect( path ):
|
2016-02-28 20:44:48 -05:00
|
|
|
"""Check if given path is the Python interpreter version 2.6+ or 3.3+."""
|
2015-10-12 15:19:37 -04:00
|
|
|
from ycmd import utils
|
|
|
|
|
2016-01-12 15:24:23 -05:00
|
|
|
if not EndsWithPython( path ):
|
|
|
|
return False
|
|
|
|
|
|
|
|
command = [ path,
|
2016-01-11 07:33:43 -05:00
|
|
|
'-c',
|
|
|
|
"import sys;"
|
|
|
|
"major, minor = sys.version_info[ :2 ];"
|
2016-02-28 20:44:48 -05:00
|
|
|
"good_python = ( major == 2 and minor >= 6 ) "
|
|
|
|
"or ( major == 3 and minor >= 3 ) or major > 3;"
|
|
|
|
# If this looks weird, remember that:
|
|
|
|
# int( True ) == 1
|
|
|
|
# int( False ) == 0
|
|
|
|
"sys.exit( not good_python )" ]
|
2016-01-11 07:33:43 -05:00
|
|
|
|
|
|
|
return utils.SafePopen( command ).wait() == 0
|
2015-10-08 05:36:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
def PathToServerScript():
|
2015-10-12 15:19:37 -04:00
|
|
|
return os.path.join( DIR_OF_YCMD, 'ycmd' )
|
2015-10-08 05:36:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
def PathToCheckCoreVersion():
|
2015-10-12 15:19:37 -04:00
|
|
|
return os.path.join( DIR_OF_YCMD, 'check_core_version.py' )
|