2015-03-20 15:55:39 -07:00
|
|
|
" The Python provider helper
|
|
|
|
if exists('s:loaded_pythonx_provider')
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
|
|
|
let s:loaded_pythonx_provider = 1
|
|
|
|
|
2015-05-24 10:05:09 -07:00
|
|
|
function! provider#pythonx#Detect(major_ver) abort
|
|
|
|
let host_var = (a:major_ver == 2) ?
|
2015-03-20 15:55:39 -07:00
|
|
|
\ 'g:python_host_prog' : 'g:python3_host_prog'
|
2015-05-24 10:05:09 -07:00
|
|
|
let skip_var = (a:major_ver == 2) ?
|
2015-03-20 15:55:39 -07:00
|
|
|
\ 'g:python_host_skip_check' : 'g:python3_host_skip_check'
|
|
|
|
let skip = exists(skip_var) ? {skip_var} : 0
|
|
|
|
if exists(host_var)
|
2015-05-14 10:26:49 -07:00
|
|
|
" Disable auto detection.
|
2015-06-04 09:43:12 -07:00
|
|
|
let [result, err] = s:check_interpreter({host_var}, a:major_ver, skip)
|
2015-05-24 10:05:09 -07:00
|
|
|
if result
|
2015-05-14 10:26:49 -07:00
|
|
|
return [{host_var}, err]
|
|
|
|
endif
|
2015-05-24 10:05:09 -07:00
|
|
|
return ['', 'provider/pythonx: Could not load Python ' . a:major_ver
|
|
|
|
\ . ' from ' . host_var . ': ' . err]
|
2015-03-20 15:55:39 -07:00
|
|
|
endif
|
|
|
|
|
2015-05-24 10:05:09 -07:00
|
|
|
let prog_suffixes = (a:major_ver == 2) ?
|
2015-05-02 12:14:41 -07:00
|
|
|
\ ['2', '2.7', '2.6', '']
|
|
|
|
\ : ['3', '3.5', '3.4', '3.3', '']
|
2015-03-20 15:55:39 -07:00
|
|
|
|
2015-05-24 10:05:09 -07:00
|
|
|
let errors = []
|
|
|
|
for prog in map(prog_suffixes, "'python' . v:val")
|
2015-06-04 09:43:12 -07:00
|
|
|
let [result, err] = s:check_interpreter(prog, a:major_ver, skip)
|
2015-05-24 10:05:09 -07:00
|
|
|
if result
|
2015-06-04 09:43:12 -07:00
|
|
|
return [prog, err]
|
2015-03-20 15:55:39 -07:00
|
|
|
endif
|
2015-05-24 10:05:09 -07:00
|
|
|
|
|
|
|
" Accumulate errors in case we don't find
|
|
|
|
" any suitable Python interpreter.
|
|
|
|
call add(errors, err)
|
2015-03-20 15:55:39 -07:00
|
|
|
endfor
|
|
|
|
|
2015-05-24 10:05:09 -07:00
|
|
|
" No suitable Python interpreter found.
|
|
|
|
return ['', 'provider/pythonx: Could not load Python ' . a:major_ver
|
|
|
|
\ . ":\n" . join(errors, "\n")]
|
2015-03-20 15:55:39 -07:00
|
|
|
endfunction
|
|
|
|
|
2015-05-24 10:05:09 -07:00
|
|
|
function! s:check_interpreter(prog, major_ver, skip) abort
|
2015-06-05 02:16:52 -07:00
|
|
|
let prog_path = exepath(a:prog)
|
|
|
|
if prog_path == ''
|
|
|
|
return [0, a:prog . ' not found in search path or not executable.']
|
2015-03-20 15:55:39 -07:00
|
|
|
endif
|
|
|
|
|
|
|
|
if a:skip
|
2015-06-04 09:43:12 -07:00
|
|
|
return [1, '']
|
2015-03-20 15:55:39 -07:00
|
|
|
endif
|
|
|
|
|
2015-05-02 11:56:09 -07:00
|
|
|
" Try to load neovim module, and output Python version.
|
2015-08-18 07:43:17 -07:00
|
|
|
let prog_ver = system([ a:prog , '-c' ,
|
2015-09-11 02:14:29 -07:00
|
|
|
\ 'import sys; sys.path.remove(""); sys.stdout.write(str(sys.version_info[0]) + '.
|
2015-08-18 07:43:17 -07:00
|
|
|
\ '"." + str(sys.version_info[1])); '.
|
|
|
|
\ (a:major_ver == 2
|
|
|
|
\ ? 'import pkgutil; exit(pkgutil.get_loader("neovim") is None)'
|
|
|
|
\ : 'import importlib; exit(importlib.find_loader("neovim") is None)')
|
|
|
|
\ ])
|
2015-05-02 11:56:09 -07:00
|
|
|
if v:shell_error
|
2015-06-05 02:16:52 -07:00
|
|
|
return [0, prog_path . ' does have not have the neovim module installed. '
|
2015-06-04 09:43:12 -07:00
|
|
|
\ . 'See ":help nvim-python".']
|
|
|
|
endif
|
|
|
|
|
|
|
|
let min_version = (a:major_ver == 2) ? '2.6' : '3.3'
|
|
|
|
if prog_ver =~ '^' . a:major_ver && prog_ver >= min_version
|
|
|
|
return [1, '']
|
2015-05-02 11:56:09 -07:00
|
|
|
endif
|
2015-03-20 15:55:39 -07:00
|
|
|
|
2015-06-05 02:16:52 -07:00
|
|
|
return [0, prog_path . ' is Python ' . prog_ver . ' and cannot provide Python '
|
2015-06-04 09:43:12 -07:00
|
|
|
\ . a:major_ver . '.']
|
|
|
|
endfunction
|