Here’s something very basic yet poorly documented. That’s why it looks like almost everybody’s trying to hack their way into this module, the yum module for Python.

First of all don’t forget that this module is only available if you’re using a Red Hat branch of distribution (fedora, centos etc.) For other distributions, such as Debian, there are other api’s, such as apt itself.

Below you’ll find some basic ways of getting configuration settings of the current yum.

import yum
yb = yum.YumBase()
print yb.conf.logfile # this will obviously printout the logfile's path
for i in yb.conf.reposdir : print i # and this will printout the directories and files for the repositories
print yb.conf.skip_broken # usually false. when set to true, your yum commands will take action of is the --skip-broken parameters was given to the yum itself.
print yb.conf.errorlevel # this is the level of errors you'd like to get as an output. it's between 0-10 and 0 is only critical ones, while 10 is more like a debug feature. Usually it is set to default 2, but since you'll be running in a script, after your script gets stable, its a good idea to set this to 0 and then distribute it.
print yb.conf.config_file_path # obvious again, the file path for your yum's config file.

 

for more configuration options, you can always see like this;

dir(yb.conf)

and don’t forget this perfect module, to reverse engineer some function if you don’t know what arguments it gets and if it’s poorly documented (ex: it doesn’t have a valid __doc__ attribute) :

import inspect
inspect.getargspec(somefunction)

And about the arch method. What yum knows about our architecture.

import yum
yb = yum.YumBase()
print yb.arch.compatarches # these are the compatible architectures with our system.
# below are the arch types that yum thinks we are usig (hopefully true!)
print yb.arch.canonarch
print yb.arch.basearch
# With the function below, you can get a list of architectures which are compatible with each other.
# Let's say you're using an x86_64 arch, then u can use packages from these ones,
yb.arch.get_arch_list('x86_64')
['x86_64', 'athlon', 'i686', 'i586', 'i486', 'i386', 'noarch']

If you’d like to get the currently installed packages on your system, rpm is the module you’d like to load.

import rpm
bold = "\033[1m"
reset = "\033[0;0m"
trans = rpm.TransactionSet()
for header in trans.dbMatch() : print "%s%s%s-%s:%s-%s.%s%s%s" % (bold,header['name'],reset,header['epochnum'],header['version'],header['release'],bold,header['arch'],reset)

Note that this script outputs various parts in bold. You can use this in any script you’d like.

Yet here’s a nice method to install new packages.

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

 

Experiment, enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *