簡體   English   中英

確定軟件包是否安裝了Yum Python API?

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

TLDR ; 我需要一個簡單的Python調用,給出一個包名(例如“ make”),以查看它是否已安裝; 如果沒有,請安裝它(我可以做后一部分)。

問題:

因此,有中給出了幾個代碼示例http://yum.baseurl.org/wiki/YumCodeSnippets ,但比內IPython的周圍kludging在什么每個方法的猜測等,似乎沒有要為任何實際的文件適用於yum的Python API。 顯然所有部落知識。

[編輯]顯然我是偶然發現了API文檔 (當然,在收到可接受的答案之后)。 它不是從主頁鏈接的,但是在這里供以后參考:http: //yum.baseurl.org/api/yum/

我需要做什么:

我有一個依賴於其他系統軟件包(make,gcc等)的部署配置腳本。 我知道我可以這樣安裝它們:http: //yum.baseurl.org/wiki/YumCodeSnippet/SimplestTransaction,但我想選擇是否在安裝之前先進行查詢,因此我可以如果不存在這些軟件包,則直接失敗的另一種選擇,而不是強制安裝。 這樣做的正確調用是什么(或者更好的方法是,有沒有人真正願意在代碼示例之外適當地記錄API?)

在這個項目之前,我從未接觸過Python,我真的很喜歡它,但是...一些模塊文檔比騎獨角獸的妖精更難以捉摸。

import yum

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

您可以在子系統上運行“哪個”以查看系統是否具有您要查找的二進制文件:

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

對於以后偶然發現這篇文章的任何人,這就是我的想法。 請注意,“ 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