2017-05-05 22:03:19 +00:00
|
|
|
" Author: w0rp <devw0rp@gmail.com>
|
|
|
|
" Description: Functions for integrating with Python linters.
|
2017-05-06 18:11:43 +00:00
|
|
|
|
2017-10-01 17:47:54 +00:00
|
|
|
let s:sep = has('win32') ? '\' : '/'
|
2017-07-05 12:07:55 +00:00
|
|
|
" bin is used for Unix virtualenv directories, and Scripts is for Windows.
|
|
|
|
let s:bin_dir = has('unix') ? 'bin' : 'Scripts'
|
2017-05-20 12:34:53 +00:00
|
|
|
let g:ale_virtualenv_dir_names = get(g:, 'ale_virtualenv_dir_names', [
|
|
|
|
\ '.env',
|
|
|
|
\ 'env',
|
|
|
|
\ 've-py3',
|
2017-06-07 20:45:48 +00:00
|
|
|
\ 've',
|
2017-05-20 12:34:53 +00:00
|
|
|
\ 'virtualenv',
|
|
|
|
\])
|
|
|
|
|
2017-07-11 20:57:37 +00:00
|
|
|
function! ale#python#FindProjectRootIni(buffer) abort
|
|
|
|
for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
|
|
|
|
if filereadable(l:path . '/MANIFEST.in')
|
|
|
|
\|| filereadable(l:path . '/setup.cfg')
|
|
|
|
\|| filereadable(l:path . '/pytest.ini')
|
|
|
|
\|| filereadable(l:path . '/tox.ini')
|
2017-11-08 17:58:56 +00:00
|
|
|
\|| filereadable(l:path . '/pycodestyle.cfg')
|
|
|
|
\|| filereadable(l:path . '/flake8.cfg')
|
2017-07-11 20:57:37 +00:00
|
|
|
return l:path
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2017-05-06 18:11:43 +00:00
|
|
|
" Given a buffer number, find the project root directory for Python.
|
|
|
|
" The root directory is defined as the first directory found while searching
|
|
|
|
" upwards through paths, including the current directory, until a path
|
2017-07-12 08:00:42 +00:00
|
|
|
" containing an init file (one from MANIFEST.in, setup.cfg, pytest.ini,
|
|
|
|
" tox.ini) is found. If it is not possible to find the project root directorty
|
|
|
|
" via init file, then it will be defined as the first directory found
|
|
|
|
" searching upwards through paths, including the current directory, until no
|
|
|
|
" __init__.py files is found.
|
2017-05-06 18:11:43 +00:00
|
|
|
function! ale#python#FindProjectRoot(buffer) abort
|
2017-07-11 20:57:37 +00:00
|
|
|
let l:ini_root = ale#python#FindProjectRootIni(a:buffer)
|
|
|
|
|
|
|
|
if !empty(l:ini_root)
|
|
|
|
return l:ini_root
|
|
|
|
endif
|
|
|
|
|
2017-05-06 18:11:43 +00:00
|
|
|
for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
|
|
|
|
if !filereadable(l:path . '/__init__.py')
|
|
|
|
return l:path
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Given a buffer number, find a virtualenv path for Python.
|
|
|
|
function! ale#python#FindVirtualenv(buffer) abort
|
|
|
|
for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
|
2017-07-05 14:51:31 +00:00
|
|
|
" Skip empty path components returned in MSYS.
|
|
|
|
if empty(l:path)
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
2017-05-20 12:34:53 +00:00
|
|
|
for l:dirname in ale#Var(a:buffer, 'virtualenv_dir_names')
|
2017-10-01 17:47:54 +00:00
|
|
|
let l:venv_dir = ale#path#Simplify(
|
|
|
|
\ join([l:path, l:dirname], s:sep)
|
|
|
|
\)
|
|
|
|
let l:script_filename = ale#path#Simplify(
|
|
|
|
\ join([l:venv_dir, s:bin_dir, 'activate'], s:sep)
|
|
|
|
\)
|
|
|
|
|
|
|
|
if filereadable(l:script_filename)
|
2017-05-20 12:34:53 +00:00
|
|
|
return l:venv_dir
|
|
|
|
endif
|
|
|
|
endfor
|
2017-05-06 18:11:43 +00:00
|
|
|
endfor
|
|
|
|
|
2017-10-10 20:49:47 +00:00
|
|
|
return $VIRTUAL_ENV
|
2017-05-06 18:11:43 +00:00
|
|
|
endfunction
|
2017-06-18 10:03:31 +00:00
|
|
|
|
|
|
|
" Given a buffer number and a command name, find the path to the executable.
|
|
|
|
" First search on a virtualenv for Python, if nothing is found, try the global
|
|
|
|
" command. Returns an empty string if cannot find the executable
|
|
|
|
function! ale#python#FindExecutable(buffer, base_var_name, path_list) abort
|
|
|
|
if ale#Var(a:buffer, a:base_var_name . '_use_global')
|
|
|
|
return ale#Var(a:buffer, a:base_var_name . '_executable')
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:virtualenv = ale#python#FindVirtualenv(a:buffer)
|
|
|
|
|
|
|
|
if !empty(l:virtualenv)
|
|
|
|
for l:path in a:path_list
|
2017-10-01 17:47:54 +00:00
|
|
|
let l:ve_executable = ale#path#Simplify(
|
|
|
|
\ join([l:virtualenv, s:bin_dir, l:path], s:sep)
|
|
|
|
\)
|
2017-06-18 10:03:31 +00:00
|
|
|
|
2017-11-04 10:46:19 +00:00
|
|
|
if executable(l:ve_executable)
|
2017-06-18 10:03:31 +00:00
|
|
|
return l:ve_executable
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
|
2017-06-27 09:06:03 +00:00
|
|
|
return ale#Var(a:buffer, a:base_var_name . '_executable')
|
2017-06-18 10:03:31 +00:00
|
|
|
endfunction
|