I have had some knowledge about Python API for yum through several days' learning of yum python source.
Now I will show one example about how to use Python API for yum.
repo = gerRepo() # get the repo path
# create a temporary repo to point to repoid packages
newrepo = yum.yumRepo.YumRepository('repoid') #'repoid' is set to be the temporary repo ID
# check gpg locally
newrepo.gpgcheck = True
newrepo.gpgkey = 'file://%s' % GPGKEY_PATH
newrepo.name = 'reponame'
newrepo.baseurl = [repo] #set repo url
newrepo.basecachedir = basecache # basecache is the cache dir
newrepo.enablegroups = 1 # Enable group installation. This step is very important for using yum installs packages from group xml definition.
yb.repos.add(newrepo)
yb.repos.enableRepo(newrepo.id)
yb.doRepoSetup(thisrepo=newrepo.id)
# mark all packages to install
yb.repos.populateSack()
yb.pkgSack.buildIndexes()
pkgs = yb.repos.getPackageSack().returnPackages()
for i in pkgs:
logger.debug('* Package: %s' % i.name)
try:
yb.install(name=i.name) # Mark package i.name to install and add its transaction to the transaction set.
except:
logger.debug("Package %s was not marked to install\n" % i)
pass
# Mark the all packages in group grpid to install. Note: first of all, newrepo.enablegroups = 1 should be set, otherwise, the group can not be found.
yb.selectGroup(grpid = 'grpid')
# configure callback to watch installation progress
downloadCallback = DownloadCallback(callback, len(pkgs))
yb.repos.setProgressBar(downloadCallback)
# Yum exception happened when the window was changed.
# Here yum doesn't need to process window change signal that is generated
# by mouse dragging, so the signal is ignored.
sig = signal.SIGWINCH
signal.signal(sig, signal.SIG_IGN)
# execute installation
try:
yb.resolveDeps()
yb.buildTransaction()
yb.processTransaction(
TransactionCallback(downloadCallback),
None,
InstallationCallback(callback, len(pkgs)))
except Exception, e:
logger.critical('Error installing packages: %s' % str(e))
raise
# close yum instance
yb.closeRpmDB()
yb.close()