Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - Check if Pylint is installed

Tags:

cmake

pylint

I'm using Cmake to go through all the .py files inside a directory and detect errors and check coding standards using Pylint.

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

like image 914
João Cartucho Avatar asked Oct 29 '22 06:10

João Cartucho


1 Answers

You should create a FindPylint.cmake file and include() its 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 error output is not actually an error, it reads No config file found, using default configuration, so we suppress it by ignoring the pylint_suppress variable.
  • the pylint output has more than just the version, so we need to do some regex/string handling.
  • the CACHE INTERNAL variable is not strictly necessary, but could come in handy later to check if Pylint was found.
like image 145
Nibor Avatar answered Jan 02 '23 20:01

Nibor