mirror of https://github.com/opencv/opencv.git
Merge pull request #9140 from Cartucho:linter_python
commit
bee5cb504b
6 changed files with 239 additions and 2 deletions
@ -0,0 +1,27 @@ |
|||||||
|
# - Find Pylint |
||||||
|
# Find the Pylint executable and extract the version number |
||||||
|
# |
||||||
|
# OUTPUT Variables |
||||||
|
# |
||||||
|
# PYLINT_FOUND |
||||||
|
# True if the pylint package was found |
||||||
|
# PYLINT_EXECUTABLE |
||||||
|
# The pylint executable location |
||||||
|
# PYLINT_VERSION |
||||||
|
# A string denoting the version of pylint that has been found |
||||||
|
|
||||||
|
find_host_program(PYLINT_EXECUTABLE pylint PATHS /usr/bin) |
||||||
|
|
||||||
|
if(PYLINT_EXECUTABLE) |
||||||
|
execute_process(COMMAND ${PYLINT_EXECUTABLE} --version OUTPUT_VARIABLE PYLINT_VERSION_RAW ERROR_QUIET) |
||||||
|
if (PYLINT_VERSION_RAW) |
||||||
|
string(REGEX REPLACE "^pylint ([0-9]+.[0-9]+.[0-9]+),.*" "\\1" PYLINT_VERSION ${PYLINT_VERSION_RAW}) |
||||||
|
else() |
||||||
|
set(PYLINT_VERSION "unknown") |
||||||
|
endif() |
||||||
|
endif() |
||||||
|
|
||||||
|
include(FindPackageHandleStandardArgs) |
||||||
|
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Pylint DEFAULT_MSG PYLINT_EXECUTABLE) |
||||||
|
|
||||||
|
mark_as_advanced(PYLINT_EXECUTABLE PYLINT_VERSION) |
@ -0,0 +1,130 @@ |
|||||||
|
if(COMMAND ocv_pylint_add_target) |
||||||
|
return() |
||||||
|
endif() |
||||||
|
|
||||||
|
find_package(Pylint QUIET) |
||||||
|
if(NOT PYLINT_FOUND OR NOT PYLINT_EXECUTABLE) |
||||||
|
include("${CMAKE_CURRENT_LIST_DIR}/FindPylint.cmake") |
||||||
|
endif() |
||||||
|
|
||||||
|
if(NOT PYLINT_FOUND) |
||||||
|
macro(ocv_pylint_add_target) # dummy |
||||||
|
endmacro() |
||||||
|
return() |
||||||
|
endif() |
||||||
|
|
||||||
|
macro(ocv_pylint_cleanup) |
||||||
|
foreach(__id ${PYLINT_TARGET_ID}) |
||||||
|
ocv_clear_vars( |
||||||
|
PYLINT_TARGET_${__id}_CWD |
||||||
|
PYLINT_TARGET_${__id}_TARGET |
||||||
|
PYLINT_TARGET_${__id}_RCFILE |
||||||
|
PYLINT_TARGET_${__id}_OPTIONS |
||||||
|
) |
||||||
|
endforeach() |
||||||
|
ocv_clear_vars(PYLINT_TARGET_ID) |
||||||
|
endmacro() |
||||||
|
ocv_pylint_cleanup() |
||||||
|
|
||||||
|
macro(ocv_pylint_add_target) |
||||||
|
cmake_parse_arguments(__pylint "" "CWD;TARGET;RCFILE;" "OPTIONS" ${ARGN}) |
||||||
|
if(__pylint_UNPARSED_ARGUMENTS) |
||||||
|
message(WARNING "Unsupported arguments: ${__pylint_UNPARSED_ARGUMENTS} |
||||||
|
(keep versions of opencv/opencv_contrib synchronized) |
||||||
|
") |
||||||
|
endif() |
||||||
|
ocv_assert(__pylint_TARGET) |
||||||
|
set(__cwd ${__pylint_CWD}) |
||||||
|
if(__cwd STREQUAL "default") |
||||||
|
get_filename_component(__cwd "${__pylint_TARGET}" DIRECTORY) |
||||||
|
endif() |
||||||
|
set(__rcfile ${__pylint_RCFILE}) |
||||||
|
if(NOT __rcfile AND NOT __pylint_OPTIONS) |
||||||
|
if(__cwd) |
||||||
|
set(__path "${__cwd}") |
||||||
|
else() |
||||||
|
get_filename_component(__path "${__pylint_TARGET}" DIRECTORY) |
||||||
|
endif() |
||||||
|
while(__path MATCHES "^${CMAKE_SOURCE_DIR}") |
||||||
|
if(EXISTS "${__path}/pylintrc") |
||||||
|
set(__rcfile "${__path}/pylintrc") |
||||||
|
break() |
||||||
|
endif() |
||||||
|
if(EXISTS "${__path}/.pylintrc") |
||||||
|
set(__rcfile "${__path}/.pylintrc") |
||||||
|
break() |
||||||
|
endif() |
||||||
|
get_filename_component(__path "${__path}" DIRECTORY) |
||||||
|
endwhile() |
||||||
|
if(NOT __rcfile) |
||||||
|
set(__rcfile "${CMAKE_BINARY_DIR}/pylintrc") |
||||||
|
endif() |
||||||
|
endif() |
||||||
|
|
||||||
|
list(LENGTH PYLINT_TARGET_ID __id) |
||||||
|
list(APPEND PYLINT_TARGET_ID ${__id}) |
||||||
|
set(PYLINT_TARGET_ID "${PYLINT_TARGET_ID}" CACHE INTERNAL "") |
||||||
|
set(PYLINT_TARGET_${__id}_CWD "${__cwd}" CACHE INTERNAL "") |
||||||
|
set(PYLINT_TARGET_${__id}_TARGET "${__pylint_TARGET}" CACHE INTERNAL "") |
||||||
|
set(PYLINT_TARGET_${__id}_RCFILE "${__rcfile}" CACHE INTERNAL "") |
||||||
|
set(PYLINT_TARGET_${__id}_OPTIONS "${__pylint_options}" CACHE INTERNAL "") |
||||||
|
endmacro() |
||||||
|
|
||||||
|
macro(ocv_pylint_add_directory_recurse __path) |
||||||
|
file(GLOB_RECURSE __python_scripts ${__path}/*.py) |
||||||
|
list(LENGTH __python_scripts __total) |
||||||
|
if(__total EQUAL 0) |
||||||
|
message(WARNING "Pylint: Python files are not found: ${__path}") |
||||||
|
endif() |
||||||
|
foreach(__script ${__python_scripts}) |
||||||
|
ocv_pylint_add_target(TARGET ${__script} ${ARGN}) |
||||||
|
endforeach() |
||||||
|
endmacro() |
||||||
|
|
||||||
|
macro(ocv_pylint_add_directory __path) |
||||||
|
file(GLOB __python_scripts ${__path}/*.py) |
||||||
|
list(LENGTH __python_scripts __total) |
||||||
|
if(__total EQUAL 0) |
||||||
|
message(WARNING "Pylint: Python files are not found: ${__path}") |
||||||
|
endif() |
||||||
|
foreach(__script ${__python_scripts}) |
||||||
|
ocv_pylint_add_target(TARGET ${__script} ${ARGN}) |
||||||
|
endforeach() |
||||||
|
endmacro() |
||||||
|
|
||||||
|
function(ocv_pylint_finalize) |
||||||
|
if(NOT PYLINT_FOUND) |
||||||
|
return() |
||||||
|
endif() |
||||||
|
|
||||||
|
file(COPY "${CMAKE_SOURCE_DIR}/platforms/scripts/pylintrc" DESTINATION "${CMAKE_BINARY_DIR}") |
||||||
|
|
||||||
|
set(PYLINT_CONFIG_SCRIPT "") |
||||||
|
ocv_cmake_script_append_var(PYLINT_CONFIG_SCRIPT |
||||||
|
PYLINT_EXECUTABLE |
||||||
|
PYLINT_TARGET_ID |
||||||
|
) |
||||||
|
set(__sources "") |
||||||
|
foreach(__id ${PYLINT_TARGET_ID}) |
||||||
|
ocv_cmake_script_append_var(PYLINT_CONFIG_SCRIPT |
||||||
|
PYLINT_TARGET_${__id}_CWD |
||||||
|
PYLINT_TARGET_${__id}_TARGET |
||||||
|
PYLINT_TARGET_${__id}_RCFILE |
||||||
|
PYLINT_TARGET_${__id}_OPTIONS |
||||||
|
) |
||||||
|
list(APPEND __sources ${PYLINT_TARGET_${__id}_TARGET} ${PYLINT_TARGET_${__id}_RCFILE}) |
||||||
|
endforeach() |
||||||
|
list(REMOVE_DUPLICATES __sources) |
||||||
|
|
||||||
|
list(LENGTH PYLINT_TARGET_ID __total) |
||||||
|
set(PYLINT_TOTAL_TARGETS "${__total}" CACHE INTERNAL "") |
||||||
|
message(STATUS "Pylint: registered ${__total} targets. Build 'check_pylint' target to run checks (\"cmake --build . --target check_pylint\" or \"make check_pylint\")") |
||||||
|
configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/pylint.cmake.in" "${CMAKE_BINARY_DIR}/pylint.cmake" @ONLY) |
||||||
|
|
||||||
|
add_custom_target(check_pylint |
||||||
|
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_BINARY_DIR}/pylint.cmake" |
||||||
|
COMMENT "Running pylint" |
||||||
|
DEPENDS ${__sources} |
||||||
|
SOURCES ${__sources} |
||||||
|
) |
||||||
|
endfunction() |
@ -0,0 +1,44 @@ |
|||||||
|
@PYLINT_CONFIG_SCRIPT@ |
||||||
|
|
||||||
|
set(__total 0) |
||||||
|
set(__passed 0) |
||||||
|
set(__errors 0) |
||||||
|
|
||||||
|
if(NOT DEFINED VERBOSE AND DEFINED ENV{VERBOSE}) |
||||||
|
set(VERBOSE "$ENV{VERBOSE}") |
||||||
|
endif() |
||||||
|
|
||||||
|
foreach(__id ${PYLINT_TARGET_ID}) |
||||||
|
message("Pylint check: ${PYLINT_TARGET_${__id}_TARGET}") |
||||||
|
set(__options ${PYLINT_TARGET_${__id}_OPTIONS}) |
||||||
|
if(PYLINT_TARGET_${__id}_RCFILE) |
||||||
|
set(__options ${__options} --rcfile=${PYLINT_TARGET_${__id}_RCFILE}) |
||||||
|
endif() |
||||||
|
set(__cwd "${PYLINT_TARGET_${__id}_CWD}") |
||||||
|
if(NOT __cwd) |
||||||
|
set(__cwd ".") |
||||||
|
endif() |
||||||
|
if(VERBOSE) |
||||||
|
message("Run: ${PYLINT_EXECUTABLE} \"${PYLINT_TARGET_${__id}_TARGET}\" ${__options} |
||||||
|
directory: \"${__cwd}\"") |
||||||
|
endif() |
||||||
|
execute_process(COMMAND ${PYLINT_EXECUTABLE} "${PYLINT_TARGET_${__id}_TARGET}" ${__options} |
||||||
|
WORKING_DIRECTORY "${__cwd}" |
||||||
|
RESULT_VARIABLE __res |
||||||
|
) |
||||||
|
math(EXPR __total "${__total} + 1") |
||||||
|
if(NOT __res EQUAL 0) |
||||||
|
math(EXPR __errors "${__errors} + 1") |
||||||
|
else() |
||||||
|
math(EXPR __passed "${__passed} + 1") |
||||||
|
endif() |
||||||
|
endforeach() |
||||||
|
|
||||||
|
message("Pylint status: |
||||||
|
TOTAL : ${__total} |
||||||
|
PASSED: ${__passed} |
||||||
|
ERRORS: ${__errors} |
||||||
|
") |
||||||
|
if(NOT __errors EQUAL 0) |
||||||
|
message(SEND_ERROR "ERROR: Pylint check FAILED") |
||||||
|
endif() |
@ -0,0 +1,16 @@ |
|||||||
|
[MESSAGES CONTROL] |
||||||
|
|
||||||
|
# Disable all to choose the Tests one by one |
||||||
|
disable=all |
||||||
|
|
||||||
|
# Tests |
||||||
|
enable=bad-indentation, # Used when an unexpected number of indentation’s tabulations or spaces has been found. |
||||||
|
mixed-indentation, # Used when there are some mixed tabs and spaces in a module. |
||||||
|
unnecessary-semicolon, # Used when a statement is ended by a semi-colon (”;”), which isn’t necessary. |
||||||
|
unused-variable # Used when a variable is defined but not used. (Use _var to ignore var). |
||||||
|
|
||||||
|
|
||||||
|
[REPORTS] |
||||||
|
|
||||||
|
# Activate the evaluation score. |
||||||
|
score=no |
Loading…
Reference in new issue