CMake - check if Pylint is installed

I am using Cmake to look through all .py files in a directory and catch errors and check coding standards with Pylint.

Is there a way to check if Pylint is installed using cmake? Will this code be OS independent (for Ubuntu and Windows for example)?

+3


source to share


2 answers


You should create a FindPylint.cmake file and include()

directory. Then run find_package(Pylint REQUIRED)

.

FindPylint.cmake:

execute_process(
    COMMAND pylint --version
    OUTPUT_VARIABLE pylint_out
    RESULT_VARIABLE pylint_error
    ERROR_VARIABLE pylint_suppress)

if (NOT pylint_error)
    string(REGEX MATCH "pylint .\..\.." pylint_version_string "${pylint_out}")
    string(SUBSTRING "${pylint_version_string}" 7 5 pylint_version)
endif ()

if (pylint_version)
    set(PYLINT_FOUND 1
        CACHE INTERNAL "Pylint version ${pylint_version} found")
endif ()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Pylint REQUIRED_VARS pylint_version
                                    VERSION_VAR pylint_version)

      



Some explanation:

  • The output error is not actually an error, it reads No config file found, using default configuration

    , so we suppress it by ignoring the variable pylint_suppress

    .
  • the output pylint

    has more than just a version, so we need to do some regex / string processing.
  • the variable is CACHE INTERNAL

    not strictly necessary, but may be useful later to check if Pylint is found.
+1


source


CMakeLists.txt

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})

find_package ( Pylint )

if ( PYLINT_FOUND )
  message ( STATUS "pylint version: ${PYLINT_VERSION}" )
endif ()

      

And in the same directory add FindPylint.cmake :



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

      

Based on this code .

0


source







All Articles