2

您好,我正在使用以下代码通过 yum api 安装软件包,使用 pythongs 脚本我需要基于此代码构建一些安装,现在它已安装但我遇到了一些错误

#!/usr/bin/python
import sys
import platform
import urllib2, urllib
import re
import yum

package="ntp"

print ("Installing ntp")

print ("#################")

yb=yum.YumBase()
searchlist=['name']
arg=['ntp']
matches = yb.searchGenerator(searchlist,arg)
for (package, matched_value) in matches :
    if package.name == 'ntp' : yb.install(package)
    yb.buildTransaction()
    yb.processTransaction()

安装完成后出现的错误

Running rpm_check_debug
Traceback (most recent call last):
  File "./test.py", line 29, in <module>
    yb.processTransaction()
  File "/usr/lib/python2.6/site-packages/yum/__init__.py", line 4928, in processTransaction
    self._doTestTransaction(callback,display=rpmTestDisplay)
  File "/usr/lib/python2.6/site-packages/yum/__init__.py", line 5027, in _doTestTransaction
    raise Errors.YumTestTransactionError, errstring
yum.Errors.YumTestTransactionError: Test Transaction Errors:   package ntp  is already installed

即使我删除了 ntp 并再次运行脚本,安装完成后它也会给我这个错误消息

另外我想调整安装过程,检查包是否已经安装,然后打印它已经安装并处理到代码中的下一步,否则处理安装步骤,任何提示也适用于使用 yum api 以正确方式的 if 条件

4

2 回答 2

2

嗨,上一个答案没有奏效,正确的答案如下:

import yum

yb=yum.YumBase()
inst = yb.rpmdb.returnPackages()
installed=[x.name for x in inst]
packages=['bla1', 'bla2', 'bla3']

for package in packages:
        if package in installed:
                print('{0} is already installed'.format(package))
        else:
                print('Installing {0}'.format(package))
                kwarg = {
                        'name':package
                }
                yb.install(**kwarg)
                yb.resolveDeps()
                yb.buildTransaction()
                yb.processTransaction()
于 2014-12-23T17:24:18.427 回答
0

需要先检查是否安装了pacakge,如果是,则跳过:

yb.conf.cache = 1 # enable cache
installed = yb.rpmdb.returnPackages()

packages = ['a','b','c']
for package in packages:
    if package in installed:
       print('{0} is already installed, skipping...'.format(package))
    else:
       print('Installing {0}'.format(package))
       yb.install(package)
       yb.resolveDeps()
       yb.buildTransaction()

yb.processTransaction()
于 2013-08-21T12:34:49.130 回答