简体   繁体   English

确定软件包是否安装了Yum Python API?

[英]Determine if package installed with Yum Python API?

TLDR ; TLDR ; I need simple a Python call given a package name (eg, 'make') to see if it's installed; 我需要一个简单的Python调用,给出一个包名(例如“ make”),以查看它是否已安装; if not, install it (I can do the latter part). 如果没有,请安装它(我可以做后一部分)。

Problem: 问题:

So there are a few code examples given in http://yum.baseurl.org/wiki/YumCodeSnippets , but other than kludging around inside ipython and guessing at what each method does, there doesn't appear to be any actual documentation for the Python API for yum. 因此,有中给出了几个代码示例http://yum.baseurl.org/wiki/YumCodeSnippets ,但比内IPython的周围kludging在什么每个方法的猜测等,似乎没有要为任何实际的文件适用于yum的Python API。 It's apparently all tribal knowledge. 显然所有部落知识。

[edit] Apparently I just accidentally discovered the API documentation (after receiving an acceptable answer, of course). [编辑]显然我是偶然发现了API文档 (当然,在收到可接受的答案之后)。 It's not linked from the main page, but here it is for future reference: http://yum.baseurl.org/api/yum/ 它不是从主页链接的,但是在这里供以后参考:http: //yum.baseurl.org/api/yum/

What I need to do: 我需要做什么:

I have a deployment configuration script that relies on other system packages (make, gcc, etc.). 我有一个依赖于其他系统软件包(make,gcc等)的部署配置脚本。 I know I can install them like this: http://yum.baseurl.org/wiki/YumCodeSnippet/SimplestTransaction but I'd like to have the option to query if they're already installed before doing so, so I can have the additional option of simply failing if the packages aren't present instead of forcing installation. 我知道我可以这样安装它们:http: //yum.baseurl.org/wiki/YumCodeSnippet/SimplestTransaction,但我想选择是否在安装之前先进行查询,因此我可以如果不存在这些软件包,则直接失败的另一种选择,而不是强制安装。 What's the proper call to do this (or better, has anyone actually bothered to properly document the API outside of code samples?) 这样做的正确调用是什么(或者更好的方法是,有没有人真正愿意在代码示例之外适当地记录API?)

I've never touched Python prior to this project, and I'm really liking it, but... some of the module documentation is more elusive than unicorn-riding leprechauns. 在这个项目之前,我从未接触过Python,我真的很喜欢它,但是...一些模块文档比骑独角兽的妖精更难以捉摸。

import yum

yb = yum.YumBase()
if yb.rpmdb.searchNevra(name='make'):
   print "installed"
else:
   print "not installed"

You could run 'which' on the subsystem to see if the system has the binaries you are looking for: 您可以在子系统上运行“哪个”以查看系统是否具有您要查找的二进制文件:

import os
os.system("which gcc")
os.system("which obscurepackagenotgoingtobefound")

For anyone who stumbles across this post later, here's what I came up with. 对于以后偶然发现这篇文章的任何人,这就是我的想法。 Note that "testing" and "skip_install" are flags that I parse from the script invocation. 请注意,“ testing”和“ skip_install”是我从脚本调用中解析的标志。

    print "Checking for prerequisites (Apache, PHP + PHP development, autoconf, make, gcc)"
    prereqs = list("httpd", "php", "php-devel", "autoconf", "make", "gcc")

    missing_packages = set()
    for package in prereqs:
        print "Checking for {0}... ".format([package]),

        # Search the RPM database to check if the package is installed
        res = yb.rpmdb.searchNevra(name=package)
        if res:
            for pkg in res:
                print pkg, "installed!"
        else:
            missing_packages.add(package)
            print package, "not installed!"
            # Install the package if missing
            if not skip_install:
                if testing:
                    print "TEST- mock install ", package
                else:
                    try:
                        yb.install(name=package)
                    except yum.Errors.InstallError, err:
                        print >> sys.stderr, "Failed during install of {0} package!".format(package)
                        print >> sys.stderr, str(err)
                        sys.exit(1)

    # Done processing all package requirements, resolve dependencies and finalize transaction
    if len(missing_packages) > 0:
        if skip_install:
            # Package not installed and set to not install, so fail
            print >> sys.stderr, "Please install the {0} packages and try again.".format(
                ",".join(str(name) for name in missing_packages))
            sys.exit(1)
        else:
            if testing:
                print "TEST- mock resolve deps and process transaction"
            else:
                yb.resolveDeps()
                yb.processTransaction()
import yum

yb = yum.YumBase()
yb.isPackageInstalled('make')

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

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