Merge #9257 'health/python: warn if pynvim upgrade failed'

Reference: https://github.com/neovim/neovim/wiki/Following-HEAD#20181118
This commit is contained in:
Marco Hinz
2018-11-20 13:15:04 +01:00
committed by GitHub
2 changed files with 47 additions and 31 deletions

View File

@@ -400,6 +400,8 @@ function! s:check_python(version) abort
endfor endfor
endif endif
let pip = 'pip' . (a:version == 2 ? '' : '3')
if !empty(python_bin) if !empty(python_bin)
let [pyversion, current, latest, status] = s:version_info(python_bin) let [pyversion, current, latest, status] = s:version_info(python_bin)
if a:version != str2nr(pyversion) if a:version != str2nr(pyversion)
@@ -410,28 +412,35 @@ function! s:check_python(version) abort
call health#report_warn('Python 3.3+ is recommended.') call health#report_warn('Python 3.3+ is recommended.')
endif endif
call health#report_info('Python'.a:version.' version: ' . pyversion) call health#report_info('Python version: ' . pyversion)
if s:is_bad_response(status) if s:is_bad_response(status)
call health#report_info(printf('%s-neovim version: %s (%s)', pyname, current, status)) call health#report_info(printf('pynvim version: %s (%s)', current, status))
else else
call health#report_info(printf('%s-neovim version: %s', pyname, current)) call health#report_info(printf('pynvim version: %s', current))
let [module_found, _msg] = provider#pythonx#CheckForModule(python_bin,
\ 'neovim', a:version)
if !module_found
call health#report_error('Importing "neovim" failed.',
\ "Reinstall \"pynvim\" and optionally \"neovim\" packages.\n" .
\ pip ." uninstall pynvim neovim\n" .
\ pip ." install pynvim\n" .
\ pip ." install neovim # only if needed by third-party software")
endif
endif endif
if s:is_bad_response(current) if s:is_bad_response(current)
call health#report_error( call health#report_error(
\ "Neovim Python client is not installed.\nError: ".current, \ "pynvim is not installed.\nError: ".current,
\ ['Run in shell: pip' . a:version . ' install pynvim']) \ ['Run in shell: '. pip .' install pynvim'])
endif endif
if s:is_bad_response(latest) if s:is_bad_response(latest)
call health#report_warn('Could not contact PyPI to get latest version.') call health#report_warn('Could not contact PyPI to get latest version.')
call health#report_error('HTTP request failed: '.latest) call health#report_error('HTTP request failed: '.latest)
elseif s:is_bad_response(status) elseif s:is_bad_response(status)
call health#report_warn(printf('Latest %s-neovim is NOT installed: %s', call health#report_warn(printf('Latest pynvim is NOT installed: %s', latest))
\ pyname, latest))
elseif !s:is_bad_response(current) elseif !s:is_bad_response(current)
call health#report_ok(printf('Latest %s-neovim is installed: %s', call health#report_ok(printf('Latest pynvim is installed.'))
\ pyname, latest))
endif endif
endif endif

View File

@@ -40,7 +40,7 @@ function! provider#pythonx#Detect(major_ver) abort
let errors = [] let errors = []
for prog in progs for prog in progs
let [result, err] = s:check_interpreter(prog, a:major_ver) let [result, err] = provider#pythonx#CheckForModule(prog, 'pynvim', a:major_ver)
if result if result
return [prog, err] return [prog, err]
endif endif
@@ -54,46 +54,53 @@ function! provider#pythonx#Detect(major_ver) abort
\ . ":\n" . join(errors, "\n")] \ . ":\n" . join(errors, "\n")]
endfunction endfunction
function! s:check_interpreter(prog, major_ver) abort " Returns array: [prog_exitcode, prog_version]
function! s:import_module(prog, module) abort
let prog_version = system([a:prog, '-c' , printf(
\ 'import sys; ' .
\ 'sys.path.remove(""); ' .
\ 'sys.stdout.write(str(sys.version_info[0]) + "." + str(sys.version_info[1])); ' .
\ 'import pkgutil; ' .
\ 'exit(2*int(pkgutil.get_loader("%s") is None))',
\ a:module)])
return [v:shell_error, prog_version]
endfunction
" Returns array: [was_success, error_message]
function! provider#pythonx#CheckForModule(prog, module, major_version) abort
let prog_path = exepath(a:prog) let prog_path = exepath(a:prog)
if prog_path ==# '' if prog_path ==# ''
return [0, a:prog . ' not found in search path or not executable.'] return [0, a:prog . ' not found in search path or not executable.']
endif endif
let min_version = (a:major_ver == 2) ? '2.6' : '3.3' let min_version = (a:major_version == 2) ? '2.6' : '3.3'
" Try to load pynvim module, and output Python version. " Try to load pynvim module, and output Python version.
" Return codes: " Exit codes:
" 0 pynvim module can be loaded. " 0 pynvim module can be loaded.
" 2 pynvim module cannot be loaded. " 2 pynvim module cannot be loaded.
" Otherwise something else went wrong (e.g. 1 or 127). " Otherwise something else went wrong (e.g. 1 or 127).
let prog_ver = system([ a:prog , '-c' , let [prog_exitcode, prog_version] = s:import_module(a:prog, 'pynvim')
\ 'import sys; ' .
\ 'sys.path.remove(""); ' .
\ 'sys.stdout.write(str(sys.version_info[0]) + "." + str(sys.version_info[1])); ' .
\ 'import pkgutil; ' .
\ 'exit(2*int(pkgutil.get_loader("pynvim") is None))'
\ ])
if v:shell_error == 2 || v:shell_error == 0 if prog_exitcode == 2 || prog_exitcode == 0
" Check version only for expected return codes. " Check version only for expected return codes.
if prog_ver !~ '^' . a:major_ver if prog_version !~ '^' . a:major_version
return [0, prog_path . ' is Python ' . prog_ver . ' and cannot provide Python ' return [0, prog_path . ' is Python ' . prog_version . ' and cannot provide Python '
\ . a:major_ver . '.'] \ . a:major_version . '.']
elseif prog_ver =~ '^' . a:major_ver && prog_ver < min_version elseif prog_version =~ '^' . a:major_version && prog_version < min_version
return [0, prog_path . ' is Python ' . prog_ver . ' and cannot provide Python >= ' return [0, prog_path . ' is Python ' . prog_version . ' and cannot provide Python >= '
\ . min_version . '.'] \ . min_version . '.']
endif endif
endif endif
if v:shell_error == 2 if prog_exitcode == 2
return [0, prog_path.' does not have the "pynvim" module. :help provider-python'] return [0, prog_path.' does not have the "pynvim" module. :help provider-python']
elseif v:shell_error == 127 elseif prog_exitcode == 127
" This can happen with pyenv's shims. " This can happen with pyenv's shims.
return [0, prog_path . ' does not exist: ' . prog_ver] return [0, prog_path . ' does not exist: ' . prog_version]
elseif v:shell_error elseif prog_exitcode
return [0, 'Checking ' . prog_path . ' caused an unknown error. ' return [0, 'Checking ' . prog_path . ' caused an unknown error. '
\ . '(' . v:shell_error . ', output: ' . prog_ver . ')' \ . '(' . prog_exitcode . ', output: ' . prog_version . ')'
\ . ' Report this at https://github.com/neovim/neovim'] \ . ' Report this at https://github.com/neovim/neovim']
endif endif