1
0

paths.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2015-2017 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 re
  27. # Can't import these from setup.py because it makes nosetests go crazy.
  28. DIR_OF_CURRENT_SCRIPT = os.path.dirname( os.path.abspath( __file__ ) )
  29. DIR_OF_YCMD = os.path.join( DIR_OF_CURRENT_SCRIPT, '..', '..', 'third_party',
  30. 'ycmd' )
  31. WIN_PYTHON_PATH = os.path.join( sys.exec_prefix, 'python.exe' )
  32. PYTHON_BINARY_REGEX = re.compile(
  33. r'python((2(\.7)?)|(3(\.[5-9])?))?(.exe)?$', re.IGNORECASE )
  34. # Not caching the result of this function; users shouldn't have to restart Vim
  35. # after running the install script or setting the
  36. # `g:ycm_server_python_interpreter` option.
  37. def PathToPythonInterpreter():
  38. # Not calling the Python interpreter to check its version as it significantly
  39. # impacts startup time.
  40. from ycmd import utils
  41. python_interpreter = vim.eval( 'g:ycm_server_python_interpreter' )
  42. if python_interpreter:
  43. python_interpreter = utils.FindExecutable( python_interpreter )
  44. if python_interpreter:
  45. return python_interpreter
  46. raise RuntimeError( "Path in 'g:ycm_server_python_interpreter' option "
  47. "does not point to a valid Python 2.7 or 3.5+." )
  48. python_interpreter = _PathToPythonUsedDuringBuild()
  49. if python_interpreter and utils.GetExecutable( python_interpreter ):
  50. return python_interpreter
  51. # On UNIX platforms, we use sys.executable as the Python interpreter path.
  52. # We cannot use sys.executable on Windows because for unknown reasons, it
  53. # returns the Vim executable. Instead, we use sys.exec_prefix to deduce the
  54. # interpreter path.
  55. python_interpreter = ( WIN_PYTHON_PATH if utils.OnWindows() else
  56. sys.executable )
  57. if _EndsWithPython( python_interpreter ):
  58. return python_interpreter
  59. # As a last resort, we search python in the PATH. We prefer Python 2 over 3
  60. # for the sake of backwards compatibility with ycm_extra_conf.py files out
  61. # there; few people wrote theirs to work on py3.
  62. # So we check 'python2' before 'python' because on some distributions (Arch
  63. # Linux for example), python refers to python3.
  64. python_interpreter = utils.PathToFirstExistingExecutable( [ 'python2',
  65. 'python',
  66. 'python3' ] )
  67. if python_interpreter:
  68. return python_interpreter
  69. raise RuntimeError( "Cannot find Python 2.7 or 3.5+. "
  70. "Set the 'g:ycm_server_python_interpreter' option "
  71. "to a Python interpreter path." )
  72. def _PathToPythonUsedDuringBuild():
  73. from ycmd import utils
  74. try:
  75. filepath = os.path.join( DIR_OF_YCMD, 'PYTHON_USED_DURING_BUILDING' )
  76. return utils.ReadFile( filepath ).strip()
  77. # We need to check for IOError for Python2 and OSError for Python3
  78. except ( IOError, OSError ):
  79. return None
  80. def _EndsWithPython( path ):
  81. """Check if given path ends with a python 2.7 or 3.5+ name."""
  82. return path and PYTHON_BINARY_REGEX.search( path ) is not None
  83. def PathToServerScript():
  84. return os.path.join( DIR_OF_YCMD, 'ycmd' )