paths.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. # Not installing aliases from python-future; it's unreliable and slow.
  22. from builtins import * # noqa
  23. import os
  24. import sys
  25. import vim
  26. import functools
  27. import re
  28. # Can't import these from setup.py because it makes nosetests go crazy.
  29. DIR_OF_CURRENT_SCRIPT = os.path.dirname( os.path.abspath( __file__ ) )
  30. DIR_OF_YCMD = os.path.join( DIR_OF_CURRENT_SCRIPT, '..', '..', 'third_party',
  31. 'ycmd' )
  32. WIN_PYTHON_PATH = os.path.join( sys.exec_prefix, 'python.exe' )
  33. PYTHON_BINARY_REGEX = re.compile(
  34. r'python((2(\.[67])?)|(3(\.[3-9])?))?(.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_server_python_interpreter' )
  48. if python_interpreter:
  49. if IsPythonVersionCorrect( python_interpreter ):
  50. return python_interpreter
  51. raise RuntimeError( "Path in 'g:ycm_server_python_interpreter' option "
  52. "does not point to a valid Python 2.6+ or 3.3+." )
  53. python_interpreter = _PathToPythonUsedDuringBuild()
  54. if IsPythonVersionCorrect( python_interpreter ):
  55. return python_interpreter
  56. # On UNIX platforms, we use sys.executable as the Python interpreter path.
  57. # We cannot use sys.executable on Windows because for unknown reasons, it
  58. # returns the Vim executable. Instead, we use sys.exec_prefix to deduce the
  59. # interpreter path.
  60. python_interpreter = ( WIN_PYTHON_PATH if utils.OnWindows() else
  61. sys.executable )
  62. if IsPythonVersionCorrect( python_interpreter ):
  63. return python_interpreter
  64. # As a last resort, we search python in the PATH. We prefer Python 2 over 3
  65. # for the sake of backwards compatibility with ycm_extra_conf.py files out
  66. # there; few people wrote theirs to work on py3.
  67. # So we check 'python2' before 'python' because on some distributions (Arch
  68. # Linux for example), python refers to python3.
  69. python_interpreter = utils.PathToFirstExistingExecutable( [ 'python2',
  70. 'python',
  71. 'python3' ] )
  72. if IsPythonVersionCorrect( python_interpreter ):
  73. return python_interpreter
  74. raise RuntimeError( "Cannot find Python 2.6+ or 3.3+. You can set its path "
  75. "using the 'g:ycm_server_python_interpreter' "
  76. "option." )
  77. def _PathToPythonUsedDuringBuild():
  78. from ycmd import utils
  79. try:
  80. filepath = os.path.join( DIR_OF_YCMD, 'PYTHON_USED_DURING_BUILDING' )
  81. return utils.ReadFile( filepath ).strip()
  82. # We need to check for IOError for Python2 and OSError for Python3
  83. except ( IOError, OSError ):
  84. return None
  85. def EndsWithPython( path ):
  86. """Check if given path ends with a python 2.6+ or 3.3+ name."""
  87. return path and PYTHON_BINARY_REGEX.search( path ) is not None
  88. def IsPythonVersionCorrect( path ):
  89. """Check if given path is the Python interpreter version 2.6+ or 3.3+."""
  90. from ycmd import utils
  91. if not EndsWithPython( path ):
  92. return False
  93. command = [ path,
  94. # Disable site customize. Faster, and less likely to encounter
  95. # issues with disconnected mounts (nfs, fuse, etc.)
  96. '-S',
  97. '-c',
  98. "import sys;"
  99. "major, minor = sys.version_info[ :2 ];"
  100. "good_python = ( major == 2 and minor >= 6 ) "
  101. "or ( major == 3 and minor >= 3 ) or major > 3;"
  102. # If this looks weird, remember that:
  103. # int( True ) == 1
  104. # int( False ) == 0
  105. "sys.exit( not good_python )" ]
  106. return utils.SafePopen( command ).wait() == 0
  107. def PathToServerScript():
  108. return os.path.join( DIR_OF_YCMD, 'ycmd' )