paths.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. from __future__ import unicode_literals
  18. from __future__ import print_function
  19. from __future__ import division
  20. from __future__ import absolute_import
  21. from future import standard_library
  22. standard_library.install_aliases()
  23. from builtins import * # noqa
  24. import os
  25. import sys
  26. import vim
  27. import functools
  28. import re
  29. # Can't import these from setup.py because it makes nosetests go crazy.
  30. DIR_OF_CURRENT_SCRIPT = os.path.dirname( os.path.abspath( __file__ ) )
  31. DIR_OF_YCMD = os.path.join( DIR_OF_CURRENT_SCRIPT, '..', '..', 'third_party',
  32. 'ycmd' )
  33. WIN_PYTHON_PATH = os.path.join( sys.exec_prefix, 'python.exe' )
  34. PYTHON_BINARY_REGEX = re.compile( r'python(2(\.[67])?)?(.exe)?$' )
  35. def Memoize( obj ):
  36. cache = obj.cache = {}
  37. @functools.wraps( obj )
  38. def memoizer( *args, **kwargs ):
  39. key = str( args ) + str( kwargs )
  40. if key not in cache:
  41. cache[ key ] = obj( *args, **kwargs )
  42. return cache[ key ]
  43. return memoizer
  44. @Memoize
  45. def PathToPythonInterpreter():
  46. from ycmd import utils
  47. python_interpreter = vim.eval( 'g:ycm_path_to_python_interpreter' )
  48. if python_interpreter:
  49. if IsPythonVersionCorrect( python_interpreter ):
  50. return python_interpreter
  51. raise RuntimeError( "Path in 'g:ycm_path_to_python_interpreter' option "
  52. "does not point to a valid Python 2.6 or 2.7." )
  53. # On UNIX platforms, we use sys.executable as the Python interpreter path.
  54. # We cannot use sys.executable on Windows because for unknown reasons, it
  55. # returns the Vim executable. Instead, we use sys.exec_prefix to deduce the
  56. # interpreter path.
  57. python_interpreter = ( WIN_PYTHON_PATH if utils.OnWindows() else
  58. sys.executable )
  59. if IsPythonVersionCorrect( python_interpreter ):
  60. return python_interpreter
  61. # As a last resort, we search python in the PATH. We check 'python2' before
  62. # 'python' because on some distributions (Arch Linux for example), python
  63. # refers to python3.
  64. python_interpreter = utils.PathToFirstExistingExecutable( [ 'python2',
  65. 'python' ] )
  66. if IsPythonVersionCorrect( python_interpreter ):
  67. return python_interpreter
  68. raise RuntimeError( "Cannot find Python 2.6 or 2.7. You can set its path "
  69. "using the 'g:ycm_path_to_python_interpreter' "
  70. "option." )
  71. def EndsWithPython( path ):
  72. """Check if given path ends with a python 2.6 or 2.7 name."""
  73. return PYTHON_BINARY_REGEX.search( path ) is not None
  74. def IsPythonVersionCorrect( path ):
  75. """Check if given path is the Python interpreter version 2.6 or 2.7."""
  76. from ycmd import utils
  77. if not EndsWithPython( path ):
  78. return False
  79. command = [ path,
  80. '-c',
  81. "import sys;"
  82. "major, minor = sys.version_info[ :2 ];"
  83. "sys.exit( major != 2 or minor < 6)" ]
  84. return utils.SafePopen( command ).wait() == 0
  85. def PathToServerScript():
  86. return os.path.join( DIR_OF_YCMD, 'ycmd' )
  87. def PathToCheckCoreVersion():
  88. return os.path.join( DIR_OF_YCMD, 'check_core_version.py' )