Skip to content

Commit b7fa6d8

Browse files
committed
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
1 parent 9c14a2f commit b7fa6d8

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

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_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)

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 run_pylint_on_tutorials
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 run_pylint_on_tutorials")
608+
10. Generate doxygen documentation and verify results.
604609

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

samples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ 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)
1516
add_subdirectory(dnn)
1617
add_subdirectory(gpu)
1718
add_subdirectory(tapi)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# ----------------------------------------------------------------------------
2+
# CMake file to run pylint on the tutorial python files.
3+
#
4+
# ----------------------------------------------------------------------------
5+
include(${CMAKE_SOURCE_DIR}/cmake/FindPylint.cmake)
6+
7+
project(run_pylint_on_tutorials)
8+
9+
if(PYLINT_FOUND)
10+
message(STATUS "pylint version: ${PYLINT_VERSION}")
11+
set(curdir "${CMAKE_CURRENT_SOURCE_DIR}")
12+
file(GLOB_RECURSE PYTHON_SCRIPTS ${curdir}/*.py)
13+
add_custom_target("${PROJECT_NAME}")
14+
15+
foreach(SCRIPT ${PYTHON_SCRIPTS})
16+
get_filename_component(SCRIPT_NAME ${SCRIPT} NAME_WE)
17+
add_custom_command(TARGET "${PROJECT_NAME}"
18+
COMMAND ${PYLINT_EXECUTABLE} ${SCRIPT}
19+
--rcfile=${curdir}/pylintrc
20+
COMMENT "Running pylint on : ${SCRIPT_NAME}.py"
21+
)
22+
endforeach()
23+
endif()

samples/python/tutorial_code/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)