Skip to content

Commit bee5cb5

Browse files
committed
Merge pull request opencv#9140 from Cartucho:linter_python
2 parents 717b2f4 + d195f27 commit bee5cb5

File tree

6 files changed

+239
-2
lines changed

6 files changed

+239
-2
lines changed

CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ OCV_OPTION(CV_ENABLE_INTRINSICS "Use intrinsic-based optimized code" ON )
312312
OCV_OPTION(CV_DISABLE_OPTIMIZATION "Disable explicit optimized code (dispatched code/intrinsics/loop unrolling/etc)" OFF )
313313
OCV_OPTION(CV_TRACE "Enable OpenCV code trace" ON)
314314

315+
OCV_OPTION(ENABLE_PYLINT "Add target with Pylint checks" (${BUILD_DOCS} OR ${BUILD_EXAMPLES}) )
315316

316317
if(ENABLE_IMPL_COLLECTION)
317318
add_definitions(-DCV_COLLECT_IMPL_DATA)
@@ -615,6 +616,11 @@ else()
615616
find_package(JNI)
616617
endif()
617618

619+
if(ENABLE_PYLINT)
620+
include(cmake/OpenCVPylint.cmake)
621+
endif()
622+
623+
618624
if(ANDROID AND ANDROID_EXECUTABLE AND ANT_EXECUTABLE AND (ANT_VERSION VERSION_GREATER 1.7) AND (ANDROID_TOOLS_Pkg_Revision GREATER 13))
619625
SET(CAN_BUILD_ANDROID_PROJECTS TRUE)
620626
else()
@@ -747,7 +753,6 @@ endif()
747753
# Extra OpenCV targets: uninstall, package_source, perf, etc.
748754
include(cmake/OpenCVExtraTargets.cmake)
749755

750-
751756
# ----------------------------------------------------------------------------
752757
# Process subdirectories
753758
# ----------------------------------------------------------------------------
@@ -847,6 +852,13 @@ if(ANDROID OR NOT UNIX)
847852
endif()
848853
endif()
849854

855+
if(COMMAND ocv_pylint_finalize)
856+
ocv_pylint_add_directory(${CMAKE_CURRENT_LIST_DIR}/samples/python)
857+
ocv_pylint_add_directory(${CMAKE_CURRENT_LIST_DIR}/samples/dnn)
858+
ocv_pylint_add_directory_recurse(${CMAKE_CURRENT_LIST_DIR}/samples/python/tutorial_code)
859+
ocv_pylint_finalize()
860+
endif()
861+
850862
# ----------------------------------------------------------------------------
851863
# Summary:
852864
# ----------------------------------------------------------------------------
@@ -1404,6 +1416,9 @@ endif()
14041416

14051417
status("")
14061418
status(" Python (for build):" PYTHON_DEFAULT_AVAILABLE THEN "${PYTHON_DEFAULT_EXECUTABLE}" ELSE NO)
1419+
if(PYLINT_FOUND AND PYLINT_EXECUTABLE)
1420+
status(" Pylint:" PYLINT_FOUND THEN "${PYLINT_EXECUTABLE} (ver: ${PYLINT_VERSION}, checks: ${PYLINT_TOTAL_TARGETS})" ELSE NO)
1421+
endif()
14071422

14081423
# ========================== java ==========================
14091424
status("")

cmake/FindPylint.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# - Find Pylint
2+
# Find the Pylint executable and extract the version number
3+
#
4+
# OUTPUT Variables
5+
#
6+
# PYLINT_FOUND
7+
# True if the pylint package was found
8+
# PYLINT_EXECUTABLE
9+
# The pylint executable location
10+
# PYLINT_VERSION
11+
# A string denoting the version of pylint that has been found
12+
13+
find_host_program(PYLINT_EXECUTABLE pylint PATHS /usr/bin)
14+
15+
if(PYLINT_EXECUTABLE)
16+
execute_process(COMMAND ${PYLINT_EXECUTABLE} --version OUTPUT_VARIABLE PYLINT_VERSION_RAW ERROR_QUIET)
17+
if (PYLINT_VERSION_RAW)
18+
string(REGEX REPLACE "^pylint ([0-9]+.[0-9]+.[0-9]+),.*" "\\1" PYLINT_VERSION ${PYLINT_VERSION_RAW})
19+
else()
20+
set(PYLINT_VERSION "unknown")
21+
endif()
22+
endif()
23+
24+
include(FindPackageHandleStandardArgs)
25+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Pylint DEFAULT_MSG PYLINT_EXECUTABLE)
26+
27+
mark_as_advanced(PYLINT_EXECUTABLE PYLINT_VERSION)

cmake/OpenCVPylint.cmake

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
if(COMMAND ocv_pylint_add_target)
2+
return()
3+
endif()
4+
5+
find_package(Pylint QUIET)
6+
if(NOT PYLINT_FOUND OR NOT PYLINT_EXECUTABLE)
7+
include("${CMAKE_CURRENT_LIST_DIR}/FindPylint.cmake")
8+
endif()
9+
10+
if(NOT PYLINT_FOUND)
11+
macro(ocv_pylint_add_target) # dummy
12+
endmacro()
13+
return()
14+
endif()
15+
16+
macro(ocv_pylint_cleanup)
17+
foreach(__id ${PYLINT_TARGET_ID})
18+
ocv_clear_vars(
19+
PYLINT_TARGET_${__id}_CWD
20+
PYLINT_TARGET_${__id}_TARGET
21+
PYLINT_TARGET_${__id}_RCFILE
22+
PYLINT_TARGET_${__id}_OPTIONS
23+
)
24+
endforeach()
25+
ocv_clear_vars(PYLINT_TARGET_ID)
26+
endmacro()
27+
ocv_pylint_cleanup()
28+
29+
macro(ocv_pylint_add_target)
30+
cmake_parse_arguments(__pylint "" "CWD;TARGET;RCFILE;" "OPTIONS" ${ARGN})
31+
if(__pylint_UNPARSED_ARGUMENTS)
32+
message(WARNING "Unsupported arguments: ${__pylint_UNPARSED_ARGUMENTS}
33+
(keep versions of opencv/opencv_contrib synchronized)
34+
")
35+
endif()
36+
ocv_assert(__pylint_TARGET)
37+
set(__cwd ${__pylint_CWD})
38+
if(__cwd STREQUAL "default")
39+
get_filename_component(__cwd "${__pylint_TARGET}" DIRECTORY)
40+
endif()
41+
set(__rcfile ${__pylint_RCFILE})
42+
if(NOT __rcfile AND NOT __pylint_OPTIONS)
43+
if(__cwd)
44+
set(__path "${__cwd}")
45+
else()
46+
get_filename_component(__path "${__pylint_TARGET}" DIRECTORY)
47+
endif()
48+
while(__path MATCHES "^${CMAKE_SOURCE_DIR}")
49+
if(EXISTS "${__path}/pylintrc")
50+
set(__rcfile "${__path}/pylintrc")
51+
break()
52+
endif()
53+
if(EXISTS "${__path}/.pylintrc")
54+
set(__rcfile "${__path}/.pylintrc")
55+
break()
56+
endif()
57+
get_filename_component(__path "${__path}" DIRECTORY)
58+
endwhile()
59+
if(NOT __rcfile)
60+
set(__rcfile "${CMAKE_BINARY_DIR}/pylintrc")
61+
endif()
62+
endif()
63+
64+
list(LENGTH PYLINT_TARGET_ID __id)
65+
list(APPEND PYLINT_TARGET_ID ${__id})
66+
set(PYLINT_TARGET_ID "${PYLINT_TARGET_ID}" CACHE INTERNAL "")
67+
set(PYLINT_TARGET_${__id}_CWD "${__cwd}" CACHE INTERNAL "")
68+
set(PYLINT_TARGET_${__id}_TARGET "${__pylint_TARGET}" CACHE INTERNAL "")
69+
set(PYLINT_TARGET_${__id}_RCFILE "${__rcfile}" CACHE INTERNAL "")
70+
set(PYLINT_TARGET_${__id}_OPTIONS "${__pylint_options}" CACHE INTERNAL "")
71+
endmacro()
72+
73+
macro(ocv_pylint_add_directory_recurse __path)
74+
file(GLOB_RECURSE __python_scripts ${__path}/*.py)
75+
list(LENGTH __python_scripts __total)
76+
if(__total EQUAL 0)
77+
message(WARNING "Pylint: Python files are not found: ${__path}")
78+
endif()
79+
foreach(__script ${__python_scripts})
80+
ocv_pylint_add_target(TARGET ${__script} ${ARGN})
81+
endforeach()
82+
endmacro()
83+
84+
macro(ocv_pylint_add_directory __path)
85+
file(GLOB __python_scripts ${__path}/*.py)
86+
list(LENGTH __python_scripts __total)
87+
if(__total EQUAL 0)
88+
message(WARNING "Pylint: Python files are not found: ${__path}")
89+
endif()
90+
foreach(__script ${__python_scripts})
91+
ocv_pylint_add_target(TARGET ${__script} ${ARGN})
92+
endforeach()
93+
endmacro()
94+
95+
function(ocv_pylint_finalize)
96+
if(NOT PYLINT_FOUND)
97+
return()
98+
endif()
99+
100+
file(COPY "${CMAKE_SOURCE_DIR}/platforms/scripts/pylintrc" DESTINATION "${CMAKE_BINARY_DIR}")
101+
102+
set(PYLINT_CONFIG_SCRIPT "")
103+
ocv_cmake_script_append_var(PYLINT_CONFIG_SCRIPT
104+
PYLINT_EXECUTABLE
105+
PYLINT_TARGET_ID
106+
)
107+
set(__sources "")
108+
foreach(__id ${PYLINT_TARGET_ID})
109+
ocv_cmake_script_append_var(PYLINT_CONFIG_SCRIPT
110+
PYLINT_TARGET_${__id}_CWD
111+
PYLINT_TARGET_${__id}_TARGET
112+
PYLINT_TARGET_${__id}_RCFILE
113+
PYLINT_TARGET_${__id}_OPTIONS
114+
)
115+
list(APPEND __sources ${PYLINT_TARGET_${__id}_TARGET} ${PYLINT_TARGET_${__id}_RCFILE})
116+
endforeach()
117+
list(REMOVE_DUPLICATES __sources)
118+
119+
list(LENGTH PYLINT_TARGET_ID __total)
120+
set(PYLINT_TOTAL_TARGETS "${__total}" CACHE INTERNAL "")
121+
message(STATUS "Pylint: registered ${__total} targets. Build 'check_pylint' target to run checks (\"cmake --build . --target check_pylint\" or \"make check_pylint\")")
122+
configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/pylint.cmake.in" "${CMAKE_BINARY_DIR}/pylint.cmake" @ONLY)
123+
124+
add_custom_target(check_pylint
125+
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_BINARY_DIR}/pylint.cmake"
126+
COMMENT "Running pylint"
127+
DEPENDS ${__sources}
128+
SOURCES ${__sources}
129+
)
130+
endfunction()

cmake/templates/pylint.cmake.in

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@PYLINT_CONFIG_SCRIPT@
2+
3+
set(__total 0)
4+
set(__passed 0)
5+
set(__errors 0)
6+
7+
if(NOT DEFINED VERBOSE AND DEFINED ENV{VERBOSE})
8+
set(VERBOSE "$ENV{VERBOSE}")
9+
endif()
10+
11+
foreach(__id ${PYLINT_TARGET_ID})
12+
message("Pylint check: ${PYLINT_TARGET_${__id}_TARGET}")
13+
set(__options ${PYLINT_TARGET_${__id}_OPTIONS})
14+
if(PYLINT_TARGET_${__id}_RCFILE)
15+
set(__options ${__options} --rcfile=${PYLINT_TARGET_${__id}_RCFILE})
16+
endif()
17+
set(__cwd "${PYLINT_TARGET_${__id}_CWD}")
18+
if(NOT __cwd)
19+
set(__cwd ".")
20+
endif()
21+
if(VERBOSE)
22+
message("Run: ${PYLINT_EXECUTABLE} \"${PYLINT_TARGET_${__id}_TARGET}\" ${__options}
23+
directory: \"${__cwd}\"")
24+
endif()
25+
execute_process(COMMAND ${PYLINT_EXECUTABLE} "${PYLINT_TARGET_${__id}_TARGET}" ${__options}
26+
WORKING_DIRECTORY "${__cwd}"
27+
RESULT_VARIABLE __res
28+
)
29+
math(EXPR __total "${__total} + 1")
30+
if(NOT __res EQUAL 0)
31+
math(EXPR __errors "${__errors} + 1")
32+
else()
33+
math(EXPR __passed "${__passed} + 1")
34+
endif()
35+
endforeach()
36+
37+
message("Pylint status:
38+
TOTAL : ${__total}
39+
PASSED: ${__passed}
40+
ERRORS: ${__errors}
41+
")
42+
if(NOT __errors EQUAL 0)
43+
message(SEND_ERROR "ERROR: Pylint check FAILED")
44+
endif()

doc/tutorials/introduction/documenting_opencv/documentation_tutorial.markdown

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Generate documentation {#tutorial_documentation_generate}
4343
make doxygen
4444
@endcode
4545
- Open <i>doc/doxygen/html/index.html</i> file in your favorite browser
46+
- Test your Python code:
47+
@code{.sh}
48+
make check_pylint
49+
@endcode
4650

4751
Quick start {#tutorial_documentation_quick_start}
4852
===========
@@ -600,7 +604,8 @@ Document the function {#tutorial_documentation_steps_fun}
600604
6. _Optional_: describe return value of the function using the _returns_ command.
601605
7. _Optional_: add "See also" section with links to similar functions or classes
602606
8. _Optional_: add bibliographic reference if any.
603-
9. Generate doxygen documentation and verify results.
607+
9. Test your code. (Python: "make check_pylint")
608+
10. Generate doxygen documentation and verify results.
604609

605610
Write the tutorial {#tutorial_documentation_steps_tutorial}
606611
------------------

platforms/scripts/pylintrc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[MESSAGES CONTROL]
2+
3+
# Disable all to choose the Tests one by one
4+
disable=all
5+
6+
# Tests
7+
enable=bad-indentation, # Used when an unexpected number of indentation’s tabulations or spaces has been found.
8+
mixed-indentation, # Used when there are some mixed tabs and spaces in a module.
9+
unnecessary-semicolon, # Used when a statement is ended by a semi-colon (”;”), which isn’t necessary.
10+
unused-variable # Used when a variable is defined but not used. (Use _var to ignore var).
11+
12+
13+
[REPORTS]
14+
15+
# Activate the evaluation score.
16+
score=no

0 commit comments

Comments
 (0)