简体   繁体   中英

python with yum api - installing package

Hello i'm using the following code to install package through yum api , using pythongs cript i need to build some installation based on this code , now its installed but im getting some errors

#!/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()

errors i got after installation is done

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

even when i removed the ntp and run the script again its give me this error msg after finished installation

plus i want to adjust the installation process , to check if the package is already installed then print its already install and process to next step in the code , else process the installation steps , any tips also for for if condition in correct way using yum api

Hi the previous answer didn't work the correct one is the following:

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()

You need to first check if the pacakge is installed, if so, then skip it:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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