CMake - 检查是否安装了 Pylint
CMake - Check if Pylint is installed
我正在使用 Cmake 遍历目录中的所有 .py 文件并检测错误并使用 Pylint 检查编码标准。
有没有办法让我检查是否使用 cmake 安装了 Pylint?此代码是否 OS 独立(例如 Ubuntu 和 Windows)?
您应该创建一个 FindPylint.cmake 文件和 include()
它的目录。然后 运行 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)
一些解释:
- 错误输出实际上不是错误,它显示为
No config file found, using default configuration
,因此我们通过忽略 pylint_suppress
变量来抑制它。
-
pylint
输出不仅仅是版本,所以我们需要做一些 regex/string 处理。
CACHE INTERNAL
变量不是绝对必要的,但稍后可以派上用场以检查是否找到 Pylint。
CMakeLists.txt:
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
find_package ( Pylint )
if ( PYLINT_FOUND )
message ( STATUS "pylint version: ${PYLINT_VERSION}" )
endif ()
并在同一目录下添加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 )
基于此code。
我正在使用 Cmake 遍历目录中的所有 .py 文件并检测错误并使用 Pylint 检查编码标准。
有没有办法让我检查是否使用 cmake 安装了 Pylint?此代码是否 OS 独立(例如 Ubuntu 和 Windows)?
您应该创建一个 FindPylint.cmake 文件和 include()
它的目录。然后 运行 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)
一些解释:
- 错误输出实际上不是错误,它显示为
No config file found, using default configuration
,因此我们通过忽略pylint_suppress
变量来抑制它。 -
pylint
输出不仅仅是版本,所以我们需要做一些 regex/string 处理。 CACHE INTERNAL
变量不是绝对必要的,但稍后可以派上用场以检查是否找到 Pylint。
CMakeLists.txt:
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
find_package ( Pylint )
if ( PYLINT_FOUND )
message ( STATUS "pylint version: ${PYLINT_VERSION}" )
endif ()
并在同一目录下添加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 )
基于此code。