paths.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. import os
  18. import sys
  19. import vim
  20. import re
  21. # Can't import these from setup.py because it makes nosetests go crazy.
  22. DIR_OF_CURRENT_SCRIPT = os.path.dirname( os.path.abspath( __file__ ) )
  23. DIR_OF_YCMD = os.path.join( DIR_OF_CURRENT_SCRIPT, '..', '..', 'third_party',
  24. 'ycmd' )
  25. WIN_PYTHON_PATH = os.path.join( sys.exec_prefix, 'python.exe' )
  26. PYTHON_BINARY_REGEX = re.compile(
  27. r'python(3(\.[6-9])?)?(.exe)?$', re.IGNORECASE )
  28. # Not caching the result of this function; users shouldn't have to restart Vim
  29. # after running the install script or setting the
  30. # `g:ycm_server_python_interpreter` option.
  31. def PathToPythonInterpreter():
  32. # Not calling the Python interpreter to check its version as it significantly
  33. # impacts startup time.
  34. from ycmd import utils
  35. python_interpreter = vim.eval( 'g:ycm_server_python_interpreter' )
  36. if python_interpreter:
  37. python_interpreter = utils.FindExecutable( python_interpreter )
  38. if python_interpreter:
  39. return python_interpreter
  40. raise RuntimeError( "Path in 'g:ycm_server_python_interpreter' option "
  41. "does not point to a valid Python 3.6+." )
  42. python_interpreter = _PathToPythonUsedDuringBuild()
  43. if python_interpreter and utils.GetExecutable( python_interpreter ):
  44. return python_interpreter
  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 _EndsWithPython( python_interpreter ):
  52. return python_interpreter
  53. python_interpreter = utils.PathToFirstExistingExecutable( [ 'python3',
  54. 'python' ] )
  55. if python_interpreter:
  56. return python_interpreter
  57. raise RuntimeError( "Cannot find Python 3.6+. "
  58. "Set the 'g:ycm_server_python_interpreter' option "
  59. "to a Python interpreter path." )
  60. def _PathToPythonUsedDuringBuild():
  61. from ycmd import utils
  62. try:
  63. filepath = os.path.join( DIR_OF_YCMD, 'PYTHON_USED_DURING_BUILDING' )
  64. return utils.ReadFile( filepath ).strip()
  65. except OSError:
  66. return None
  67. def _EndsWithPython( path ):
  68. """Check if given path ends with a python 3.6+ name."""
  69. return path and PYTHON_BINARY_REGEX.search( path ) is not None
  70. def PathToServerScript():
  71. return os.path.join( DIR_OF_YCMD, 'ycmd' )