paths.py 3.4 KB

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