paths.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2015 YouCompleteMe contributors.
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. import os
  20. import sys
  21. import vim
  22. import functools
  23. import re
  24. from ycmd import utils
  25. DIR_OF_CURRENT_SCRIPT = os.path.dirname( os.path.abspath( __file__ ) )
  26. WIN_PYTHON_PATH = os.path.join( sys.exec_prefix, 'python.exe' )
  27. PYTHON_BINARY_REGEX = re.compile( r'python(2(\.[67])?)?(.exe)?$' )
  28. def Memoize( obj ):
  29. cache = obj.cache = {}
  30. @functools.wraps( obj )
  31. def memoizer( *args, **kwargs ):
  32. key = str( args ) + str( kwargs )
  33. if key not in cache:
  34. cache[ key ] = obj( *args, **kwargs )
  35. return cache[ key ]
  36. return memoizer
  37. @Memoize
  38. def PathToPythonInterpreter():
  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. if not EndsWithPython( path ):
  69. return False
  70. command = [ path,
  71. '-c',
  72. "import sys;"
  73. "major, minor = sys.version_info[ :2 ];"
  74. "sys.exit( major != 2 or minor < 6)" ]
  75. return utils.SafePopen( command ).wait() == 0
  76. def PathToServerScript():
  77. return os.path.join( DIR_OF_CURRENT_SCRIPT, '..', '..', 'third_party',
  78. 'ycmd', 'ycmd' )
  79. def PathToCheckCoreVersion():
  80. return os.path.join( DIR_OF_CURRENT_SCRIPT, '..', '..', 'third_party',
  81. 'ycmd', 'check_core_version.py' )