Yum Python API

Running larger sites demands for scripting node installation.

I like to use python for the task (surprise surprise). As it happens the mayor package managment tool for Red Hat, CentOS and Scientific Linux “yum” is itself written in python.

Since yum lacks documentation on how to use inside your own python code I have googled the web and found this nice page: Deciphering the Yum API.

For some reasons the code didn’t work for me on Scientific Linux 4 (Red Hat 3.4.6-9) which still uses python2.3. So I fiddled around a bit and got it working. Here are the updated examples.

Listing packages:

import yum

yb = yum.YumBase()
yb.doConfigSetup()
yb.doTsSetup()
yb.doRpmDBSetup()
for pkg in yb.rpmdb.getPkgList():
  print pkg

Searching packages:

In newer versions of yum there seems to be YumBase.searchGenerator, wich should be prefered for performance and memory footprint. However in the enterprise class distros i have to deal with, I could not use it. So I present a solution with YumBase.searchPackages.

import yum

yb = yum.YumBase()
yb.doConfigSetup()
yb.doRepoSetup()
yb.doSackSetup()
yb.doTsSetup()
yb.doRpmDBSetup()
fields = ['name']        # fields to look at
criteria = ["k3b"]       # strings to find in fields
matches = yb.searchPackages(fields, criteria)
for match in matches:
  print match

Ask if a certain package is installed:

import yum

yb = yum.YumBase()
yb.doConfigSetup()
yb.doTsSetup()
yb.doRpmDBSetup()
# prints 1 if installed else 0
print yb.rpmdb.installed('vim-enhanced')

Installing packages:

Install the editor joe. Note that we do not use the “standard” YumBase but the “command line” YumBaseCli. This class provides the installPkgs() function which magically sorts out which package we want by just saying “joe”. Note also that by using YumBaseCli in this way, we are bypassing some argument checks the command line yum would have done. To make sure yum doesn’t ask any questions we do “ybc.conf.setConfigOption(‘assumeyes’,True)”.
Beware of the consequences.

import sys

sys.path.append('/usr/share/yum-cli')

import cli

ybc = cli.YumBaseCli()
ybc.doConfigSetup()
ybc.doTsSetup()
ybc.doRpmDBSetup()
ybc.installPkgs(['joe'])
ybc.buildTransaction()
ybc.conf.setConfigOption('assumeyes',True)
ybc.doTransaction()

Tags: , , ,

4 Responses to “Yum Python API”

  1. Mark Brandis Says:

    Hi!

    I find this article most useful. One problem remains for me. I try to use downgradePkgs. It works if there are no dependencies to solve. If there is a dependency, the cli shows me the solution but does not use it.

    For example I try to downgrade k3b and k3b-libs need to be downgraded, too. But yum does not automatically solve the dependency. When I use both packages as arguments ['k3b','k3b-libs'] or a glob ['k3b*'] everything works fine.

    Do you have any idea how to solve that without knowing all dependencies beforehand?

    Thanks in advance
    Mark

  2. snies Says:

    Hi Mark,

    i will have a look asap, but at the moment i am quite busy.

    Cheers,
    Stephan

  3. Mark Brandis Says:

    I have a crude solution:

    result = 1
    while result != 0:
    self.yuba.downgradePkgs(args)
    self.yuba.buildTransaction()
    self.yuba.conf.setConfigOption(‘assumeyes’, True)
    result = self.yuba.doTransaction()
    if result != 0:
    old_args = args[:]
    error = self.yuba._run_rpm_check_debug()
    logging.debug(error)
    for line in error:
    if re.search(r’.*=.*is needed’, line, re.I):
    pkg = (line.split(” = “))[0]
    args.append(pkg)
    logging.debug(“old: %s, new: %s” % (old_args, args))
    if args == old_args:
    return result

  4. Leo Says:

    Hello,

    Could you please post also the python script which prints installation progress (percents or something else)

Leave a Reply