need help to parse a specfile from python

Thierry Parmentelat thierry.parmentelat at
Fri Jun 10 10:14:03 UTC 2011

Hi folks

I could use some help on an apparently very simple task, that I can't get to work..

Our build needs to inspect specfiles in order to discover the actual contents, in terms of the exact set of output rpms, and the like

So far we were using a C-code, that relied on a few rpm libraries:
cc -g -Wall spec2make.c -o spec2make -lrpm -lrpmbuild -lrpmio -lpopt

This code more or less needs attention each time we move to a more recent linux distro, 
with fedora15 and rpm-4.9 it's so bad that I'm trying to move to a python-based code instead

The objective is pretty simple, parse a specfile, compute the list of rpms that would be output with rpmbuild;
Something like this would almost do the trick:


import rpm
for p in s.packages:
    print 'Found package',h.format('%{name}'),h.format('%{version}'),h.format('%{release}',h.format('%{arch}'))

However, in many cases like with this kernel specfile, the list of packages actually built may be shorter than this 'theoretical' list

The C version was doing something like this (very simplified) in order to successfully filter out irrelevant packages


  ts = rpmtsCreate();
  parseSpec (ts, filename, ...)
  spec = rpmtsSpec (ts)

  for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {

    (void) headerNEVRA(pkg->header, &name, &unused, &version, &release, &arch);

    if (name && version && release && arch && pkg->fileList) {
	/* print out package info */


Now for my question

1. how can I write a python code that does the equivalent of pkg->fileList ?
I have tried most of the methods available to the 'h' (rpm.hdr) object, and mostly everything returns void information
It feels like the object is not properly initialized...

And I'd also be interested in discussing the following questions, although less critical to me at this point:

2. moving beyond what is described above, I'd also love to be able to pass the python code some data to model any rpmbuild command-line argument; I mean, if the build actually invokes e.g.
rpmbuild --without-doc --define "foo bar" -bs kernel-2.6.spec
then it may impact the set of output rpms, but I would not know how to instruct the python code about that
(the C-code that I mention tried to pass popt options through rpmcliInit, which never quite worked out...)

3. I've never been able to find an online resource that would explain all this, and mostly came up with the python code by guessing, which felt cool at first but turned out very frustrating afterwards :) I guess I'm doing it all wrong because of that..
Any examples / resources out there, apart from the doxygen doc that honestly I found of poor help for this very simple task.

many thanks in advance -- Thierry

More information about the Rpm-list mailing list