我使用Cmake浏览目录中的所有.py文件,并使用Pylint检测错误并检查编码标准。
我有办法检查是否使用cmake安装了Pylint?这段代码是否独立于操作系统(例如Ubuntu和Windows)?
答案 0 :(得分:2)
您应该创建一个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
输出不仅仅是版本,所以我们需要做一些正则表达式/字符串处理。 CACHE INTERNAL
变量并非绝对必要,但稍后可以派上用场检查是否找到了Pylint。答案 1 :(得分:0)
<强>的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。