From b7fa6d84bca4b84fb4a8d94a6d55fbd3e9f1d1db Mon Sep 17 00:00:00 2001 From: tribta Date: Thu, 29 Jun 2017 12:42:45 +0100 Subject: [PATCH 1/2] Using Pylint Tool to Test the python tutorial codes * Adding CMake script to check if pylint is installed * Adding Pylint config file (to choose the tests that are enabled) * Adding CMake script to samples/python/tutorial_code Testing: bad-indentation, mixed-indentation, unnecessary-semicolon, unused-variable --- cmake/FindPylint.cmake | 27 +++++++++++++++++++ .../documentation_tutorial.markdown | 7 ++++- samples/CMakeLists.txt | 1 + samples/python/tutorial_code/CMakeLists.txt | 23 ++++++++++++++++ samples/python/tutorial_code/pylintrc | 16 +++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 cmake/FindPylint.cmake create mode 100644 samples/python/tutorial_code/CMakeLists.txt create mode 100644 samples/python/tutorial_code/pylintrc diff --git a/cmake/FindPylint.cmake b/cmake/FindPylint.cmake new file mode 100644 index 0000000000..a8b870fe20 --- /dev/null +++ b/cmake/FindPylint.cmake @@ -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_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) diff --git a/doc/tutorials/introduction/documenting_opencv/documentation_tutorial.markdown b/doc/tutorials/introduction/documenting_opencv/documentation_tutorial.markdown index c51d7bef36..7549ea9dad 100644 --- a/doc/tutorials/introduction/documenting_opencv/documentation_tutorial.markdown +++ b/doc/tutorials/introduction/documenting_opencv/documentation_tutorial.markdown @@ -43,6 +43,10 @@ Generate documentation {#tutorial_documentation_generate} make doxygen @endcode - Open doc/doxygen/html/index.html file in your favorite browser +- Test your python code: + @code{.sh} + make run_pylint_on_tutorials + @endcode Quick start {#tutorial_documentation_quick_start} =========== @@ -600,7 +604,8 @@ Document the function {#tutorial_documentation_steps_fun} 6. _Optional_: describe return value of the function using the _returns_ command. 7. _Optional_: add "See also" section with links to similar functions or classes 8. _Optional_: add bibliographic reference if any. -9. Generate doxygen documentation and verify results. +9. Test your code. (Python: "make run_pylint_on_tutorials") +10. Generate doxygen documentation and verify results. Write the tutorial {#tutorial_documentation_steps_tutorial} ------------------ diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 1ce0489f1a..06cdae364b 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -12,6 +12,7 @@ if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_LIST_DIR) add_subdirectory(cpp) add_subdirectory(java/tutorial_code) +add_subdirectory(python/tutorial_code) add_subdirectory(dnn) add_subdirectory(gpu) add_subdirectory(tapi) diff --git a/samples/python/tutorial_code/CMakeLists.txt b/samples/python/tutorial_code/CMakeLists.txt new file mode 100644 index 0000000000..c456ffc7e9 --- /dev/null +++ b/samples/python/tutorial_code/CMakeLists.txt @@ -0,0 +1,23 @@ +# ---------------------------------------------------------------------------- +# CMake file to run pylint on the tutorial python files. +# +# ---------------------------------------------------------------------------- +include(${CMAKE_SOURCE_DIR}/cmake/FindPylint.cmake) + +project(run_pylint_on_tutorials) + +if(PYLINT_FOUND) + message(STATUS "pylint version: ${PYLINT_VERSION}") + set(curdir "${CMAKE_CURRENT_SOURCE_DIR}") + file(GLOB_RECURSE PYTHON_SCRIPTS ${curdir}/*.py) + add_custom_target("${PROJECT_NAME}") + + foreach(SCRIPT ${PYTHON_SCRIPTS}) + get_filename_component(SCRIPT_NAME ${SCRIPT} NAME_WE) + add_custom_command(TARGET "${PROJECT_NAME}" + COMMAND ${PYLINT_EXECUTABLE} ${SCRIPT} + --rcfile=${curdir}/pylintrc + COMMENT "Running pylint on : ${SCRIPT_NAME}.py" + ) + endforeach() +endif() diff --git a/samples/python/tutorial_code/pylintrc b/samples/python/tutorial_code/pylintrc new file mode 100644 index 0000000000..35c4a34a96 --- /dev/null +++ b/samples/python/tutorial_code/pylintrc @@ -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 From d195f2775abdd7d5cb3bfba7f84f0631492e1062 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 23 Aug 2017 19:48:41 +0300 Subject: [PATCH 2/2] cmake: change Pylint integration --- CMakeLists.txt | 17 ++- cmake/FindPylint.cmake | 2 +- cmake/OpenCVPylint.cmake | 130 ++++++++++++++++++ cmake/templates/pylint.cmake.in | 44 ++++++ .../documentation_tutorial.markdown | 6 +- .../scripts}/pylintrc | 0 samples/CMakeLists.txt | 1 - samples/python/tutorial_code/CMakeLists.txt | 23 ---- 8 files changed, 194 insertions(+), 29 deletions(-) create mode 100644 cmake/OpenCVPylint.cmake create mode 100644 cmake/templates/pylint.cmake.in rename {samples/python/tutorial_code => platforms/scripts}/pylintrc (100%) delete mode 100644 samples/python/tutorial_code/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 74a624da4f..b1b3624a13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -312,6 +312,7 @@ OCV_OPTION(CV_ENABLE_INTRINSICS "Use intrinsic-based optimized code" ON ) OCV_OPTION(CV_DISABLE_OPTIMIZATION "Disable explicit optimized code (dispatched code/intrinsics/loop unrolling/etc)" OFF ) OCV_OPTION(CV_TRACE "Enable OpenCV code trace" ON) +OCV_OPTION(ENABLE_PYLINT "Add target with Pylint checks" (${BUILD_DOCS} OR ${BUILD_EXAMPLES}) ) if(ENABLE_IMPL_COLLECTION) add_definitions(-DCV_COLLECT_IMPL_DATA) @@ -615,6 +616,11 @@ else() find_package(JNI) endif() +if(ENABLE_PYLINT) + include(cmake/OpenCVPylint.cmake) +endif() + + if(ANDROID AND ANDROID_EXECUTABLE AND ANT_EXECUTABLE AND (ANT_VERSION VERSION_GREATER 1.7) AND (ANDROID_TOOLS_Pkg_Revision GREATER 13)) SET(CAN_BUILD_ANDROID_PROJECTS TRUE) else() @@ -747,7 +753,6 @@ endif() # Extra OpenCV targets: uninstall, package_source, perf, etc. include(cmake/OpenCVExtraTargets.cmake) - # ---------------------------------------------------------------------------- # Process subdirectories # ---------------------------------------------------------------------------- @@ -847,6 +852,13 @@ if(ANDROID OR NOT UNIX) endif() endif() +if(COMMAND ocv_pylint_finalize) + ocv_pylint_add_directory(${CMAKE_CURRENT_LIST_DIR}/samples/python) + ocv_pylint_add_directory(${CMAKE_CURRENT_LIST_DIR}/samples/dnn) + ocv_pylint_add_directory_recurse(${CMAKE_CURRENT_LIST_DIR}/samples/python/tutorial_code) + ocv_pylint_finalize() +endif() + # ---------------------------------------------------------------------------- # Summary: # ---------------------------------------------------------------------------- @@ -1404,6 +1416,9 @@ endif() status("") status(" Python (for build):" PYTHON_DEFAULT_AVAILABLE THEN "${PYTHON_DEFAULT_EXECUTABLE}" ELSE NO) +if(PYLINT_FOUND AND PYLINT_EXECUTABLE) +status(" Pylint:" PYLINT_FOUND THEN "${PYLINT_EXECUTABLE} (ver: ${PYLINT_VERSION}, checks: ${PYLINT_TOTAL_TARGETS})" ELSE NO) +endif() # ========================== java ========================== status("") diff --git a/cmake/FindPylint.cmake b/cmake/FindPylint.cmake index a8b870fe20..117893d97e 100644 --- a/cmake/FindPylint.cmake +++ b/cmake/FindPylint.cmake @@ -10,7 +10,7 @@ # PYLINT_VERSION # A string denoting the version of pylint that has been found -find_program(PYLINT_EXECUTABLE pylint PATHS /usr/bin) +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) diff --git a/cmake/OpenCVPylint.cmake b/cmake/OpenCVPylint.cmake new file mode 100644 index 0000000000..08fd28b770 --- /dev/null +++ b/cmake/OpenCVPylint.cmake @@ -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() diff --git a/cmake/templates/pylint.cmake.in b/cmake/templates/pylint.cmake.in new file mode 100644 index 0000000000..c89ecc4599 --- /dev/null +++ b/cmake/templates/pylint.cmake.in @@ -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() diff --git a/doc/tutorials/introduction/documenting_opencv/documentation_tutorial.markdown b/doc/tutorials/introduction/documenting_opencv/documentation_tutorial.markdown index 7549ea9dad..0f26dafaa0 100644 --- a/doc/tutorials/introduction/documenting_opencv/documentation_tutorial.markdown +++ b/doc/tutorials/introduction/documenting_opencv/documentation_tutorial.markdown @@ -43,9 +43,9 @@ Generate documentation {#tutorial_documentation_generate} make doxygen @endcode - Open doc/doxygen/html/index.html file in your favorite browser -- Test your python code: +- Test your Python code: @code{.sh} - make run_pylint_on_tutorials + make check_pylint @endcode Quick start {#tutorial_documentation_quick_start} @@ -604,7 +604,7 @@ Document the function {#tutorial_documentation_steps_fun} 6. _Optional_: describe return value of the function using the _returns_ command. 7. _Optional_: add "See also" section with links to similar functions or classes 8. _Optional_: add bibliographic reference if any. -9. Test your code. (Python: "make run_pylint_on_tutorials") +9. Test your code. (Python: "make check_pylint") 10. Generate doxygen documentation and verify results. Write the tutorial {#tutorial_documentation_steps_tutorial} diff --git a/samples/python/tutorial_code/pylintrc b/platforms/scripts/pylintrc similarity index 100% rename from samples/python/tutorial_code/pylintrc rename to platforms/scripts/pylintrc diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 06cdae364b..1ce0489f1a 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -12,7 +12,6 @@ if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_LIST_DIR) add_subdirectory(cpp) add_subdirectory(java/tutorial_code) -add_subdirectory(python/tutorial_code) add_subdirectory(dnn) add_subdirectory(gpu) add_subdirectory(tapi) diff --git a/samples/python/tutorial_code/CMakeLists.txt b/samples/python/tutorial_code/CMakeLists.txt deleted file mode 100644 index c456ffc7e9..0000000000 --- a/samples/python/tutorial_code/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -# ---------------------------------------------------------------------------- -# CMake file to run pylint on the tutorial python files. -# -# ---------------------------------------------------------------------------- -include(${CMAKE_SOURCE_DIR}/cmake/FindPylint.cmake) - -project(run_pylint_on_tutorials) - -if(PYLINT_FOUND) - message(STATUS "pylint version: ${PYLINT_VERSION}") - set(curdir "${CMAKE_CURRENT_SOURCE_DIR}") - file(GLOB_RECURSE PYTHON_SCRIPTS ${curdir}/*.py) - add_custom_target("${PROJECT_NAME}") - - foreach(SCRIPT ${PYTHON_SCRIPTS}) - get_filename_component(SCRIPT_NAME ${SCRIPT} NAME_WE) - add_custom_command(TARGET "${PROJECT_NAME}" - COMMAND ${PYLINT_EXECUTABLE} ${SCRIPT} - --rcfile=${curdir}/pylintrc - COMMENT "Running pylint on : ${SCRIPT_NAME}.py" - ) - endforeach() -endif()