简体   繁体   English

使用 python 检查 CMAKE 版本

[英]Check CMAKE version using python

I'm building a project for first time and its really confusing.我第一次构建一个项目,它真的很混乱。 I've included a project through using gitmodules and I'm using visual studio and I want to static link that project in my own project.我已经通过使用 gitmodules 包含了一个项目,并且我正在使用 Visual Studio,我想在我自己的项目中链接该项目。 issue is, is that the project doesn't come with a visual studio solution so I'm left with running cmake on this, however I saw online through looking on GitHub that people use scripts to run all the prerequisite stuff for building.问题是,该项目没有附带 Visual Studio 解决方案,所以我只剩下在此运行 cmake,但是我通过查看 GitHub 在线看到人们使用脚本来运行构建的所有先决条件。 so I've made batch scripts and python scripts and its going well so far.所以我制作了批处理脚本和 python 脚本,到目前为止进展顺利。

But I haven't been able to find anywhere online that shows how to get the cmake version using python.但我无法在网上找到任何地方显示如何使用 python 获得 cmake 版本。

I want the python script to detect if cmake installed or not and if it is installed then to check its version.我希望 python 脚本检测是否安装了 cmake 以及是否已安装然后检查其版本。

Current my python code looks like this当前我的 python 代码如下所示

if cmake --version <= 3.22.1:
    exit(1)

this gives me errors though "SyntaxError: invalid syntax"尽管“SyntaxError:无效语法”但这给了我错误

Running cmake --version in the terminal shows the version as expected.在终端中运行 cmake --version 会按预期显示版本。 "cmake version 3.22.1" “cmake 版本 3.22.1”

Im trying to get the cmake version from the call to be used in an if statement in python rather than just calling cmake --version in python.我试图从调用中获取 cmake 版本,以便在 python 中的 if 语句中使用,而不仅仅是在 Z23EEEB4347BDD7556BZ23EEEB7347BDD7556BZ23EEEB7347BDD7556BDcmake --version 中调用 cmake --version。

does anyone know how to do this?有谁知道如何做到这一点? also if this is x,y problem please let me know I don't know what I'm doing!如果这是 x,y 问题,请告诉我我不知道我在做什么!

thanks谢谢

Use Subprocess使用Subprocess

subprocess = subprocess.Popen("cmake --version", shell=True, stdout=subprocess.PIPE)

subprocess_return = subprocess.stdout.read()

print(subprocess_return)

You now have the output of the command inside the subprocess_return variable, Now you can just parse it to get the version number.您现在在subprocess_return变量中拥有命令的 output,现在您可以解析它以获得版本号。

import subprocess import re try: msg = subprocess.Popen("cmake --version", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout.read().decode() # guess versions = msg.split()[2] pat = re.compile(r'[0-9]\.[0-9].+\.[0-9]') if pat.match(versions): print(versions.split(".")) else: print("Invalid!") except: print("Invalid!")

You can check CMake version against 3.22.1 like this:您可以像这样检查 CMake 版本与 3.22.1:

import subprocess
from packaging import version

def get_cmake_version():
    output = subprocess.check_output(['cmake', '--version']).decode('utf-8')
    line = output.splitlines()[0]
    version = line.split()[2]
    return(version)

if version.parse(get_cmake_version()) < version.parse("3.22.1"):
    exit(1)

get_cmake_version runs cmake --version , getting its output with check_output() . get_cmake_version运行cmake --version ,通过check_output()获取其 output 。 Then with a combination of split() and splitlines() , it then gets the third word on the first line and returns it.然后使用split()splitlines()的组合,然后获取第一行的第三个单词并返回它。 Then we parse the version number returned by get_cmake_version() with version.parse from the packaging module and compare it to 3.22.1 .然后我们用打包模块中的version.parse解析get_cmake_version()返回的版本号,并与3.22.1进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM