paths.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (C) 2015 YouCompleteMe contributors.
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. import os
  18. import sys
  19. import vim
  20. import functools
  21. import re
  22. DIR_OF_CURRENT_SCRIPT = os.path.dirname( os.path.abspath( __file__ ) )
  23. DIR_OF_YCMD = os.path.join( DIR_OF_CURRENT_SCRIPT, '..', '..', 'third_party',
  24. 'ycmd' )
  25. WIN_PYTHON_PATH = os.path.join( sys.exec_prefix, 'python.exe' )
  26. PYTHON_BINARY_REGEX = re.compile( r'python(2(\.[67])?)?(.exe)?$' )
  27. def Memoize( obj ):
  28. cache = obj.cache = {}
  29. @functools.wraps( obj )
  30. def memoizer( *args, **kwargs ):
  31. key = str( args ) + str( kwargs )
  32. if key not in cache:
  33. cache[ key ] = obj( *args, **kwargs )
  34. return cache[ key ]
  35. return memoizer
  36. @Memoize
  37. def PathToPythonInterpreter():
  38. from ycmd import utils
  39. python_interpreter = vim.eval( 'g:ycm_path_to_python_interpreter' )
  40. if python_interpreter:
  41. if IsPythonVersionCorrect( python_interpreter ):
  42. return python_interpreter
  43. raise RuntimeError( "Path in 'g:ycm_path_to_python_interpreter' option "
  44. "does not point to a valid Python 2.6 or 2.7." )
  45. # On UNIX platforms, we use sys.executable as the Python interpreter path.
  46. # We cannot use sys.executable on Windows because for unknown reasons, it
  47. # returns the Vim executable. Instead, we use sys.exec_prefix to deduce the
  48. # interpreter path.
  49. python_interpreter = ( WIN_PYTHON_PATH if utils.OnWindows() else
  50. sys.executable )
  51. if IsPythonVersionCorrect( python_interpreter ):
  52. return python_interpreter
  53. # As a last resort, we search python in the PATH. We check 'python2' before
  54. # 'python' because on some distributions (Arch Linux for example), python
  55. # refers to python3.
  56. python_interpreter = utils.PathToFirstExistingExecutable( [ 'python2',
  57. 'python' ] )
  58. if IsPythonVersionCorrect( python_interpreter ):
  59. return python_interpreter
  60. raise RuntimeError( "Cannot find Python 2.6 or 2.7. You can set its path "
  61. "using the 'g:ycm_path_to_python_interpreter' "
  62. "option." )
  63. def EndsWithPython( path ):
  64. """Check if given path ends with a python 2.6 or 2.7 name."""
  65. return PYTHON_BINARY_REGEX.search( path ) is not None
  66. def IsPythonVersionCorrect( path ):
  67. """Check if given path is the Python interpreter version 2.6 or 2.7."""
  68. from ycmd import utils
  69. if not EndsWithPython( path ):
  70. return False
  71. command = [ path,
  72. '-c',
  73. "import sys;"
  74. "major, minor = sys.version_info[ :2 ];"
  75. "sys.exit( major != 2 or minor < 6)" ]
  76. return utils.SafePopen( command ).wait() == 0
  77. def PathToServerScript():
  78. return os.path.join( DIR_OF_YCMD, 'ycmd' )
  79. def PathToCheckCoreVersion():
  80. return os.path.join( DIR_OF_YCMD, 'check_core_version.py' )