Skip to content

Commit d195f27

Browse files
committed
cmake: change Pylint integration
1 parent b7fa6d8 commit d195f27

File tree

8 files changed

+194
-29
lines changed

8 files changed

+194
-29
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# PYLINT_VERSION
1111
# A string denoting the version of pylint that has been found
1212

13-
find_program(PYLINT_EXECUTABLE pylint PATHS /usr/bin)
13+
find_host_program(PYLINT_EXECUTABLE pylint PATHS /usr/bin)
1414

1515
if(PYLINT_EXECUTABLE)
1616
execute_process(COMMAND ${PYLINT_EXECUTABLE} --version OUTPUT_VARIABLE PYLINT_VERSION_RAW ERROR_QUIET)

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ 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:
46+
- Test your Python code:
4747
@code{.sh}
48-
make run_pylint_on_tutorials
48+
make check_pylint
4949
@endcode
5050

5151
Quick start {#tutorial_documentation_quick_start}
@@ -604,7 +604,7 @@ Document the function {#tutorial_documentation_steps_fun}
604604
6. _Optional_: describe return value of the function using the _returns_ command.
605605
7. _Optional_: add "See also" section with links to similar functions or classes
606606
8. _Optional_: add bibliographic reference if any.
607-
9. Test your code. (Python: "make run_pylint_on_tutorials")
607+
9. Test your code. (Python: "make check_pylint")
608608
10. Generate doxygen documentation and verify results.
609609

610610
Write the tutorial {#tutorial_documentation_steps_tutorial}
File renamed without changes.

samples/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_LIST_DIR)
1212

1313
add_subdirectory(cpp)
1414
add_subdirectory(java/tutorial_code)
15-
add_subdirectory(python/tutorial_code)
1615
add_subdirectory(dnn)
1716
add_subdirectory(gpu)
1817
add_subdirectory(tapi)

samples/python/tutorial_code/CMakeLists.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)