rpm.mi

Match Iterator (rpm.mi)

To get a RPM header from the database you must use dbMatch which returns a match iterator. Then you loop over the match iterator for each matching header. In it's simplest form

import rpm
ts=rpm.ts()
mi=ts.dbMatch("name","kernel")
for hdr in mi:
     print "%s-%s-%s" % (hdr['name'], hdr['version'], hdr['release'])
	  

Calling dbMatch() with no arguments returns all matches within the RPM database, thus is equivalent to rpm -qa

Match iterators can be told to use globs or regular expressions rather than searching on a header key.

import rpm
ts=rpm.ts()
# Equivalent to rpm -qa
mi=ts.dbMatch()
# Apply regex
mi.pattern("name", rpm.RPMMIRE_GLOB, "kernel*")
for hdr in mi:
    print "%s-%s-%s" % (hdr['name'], hdr['version'], hdr['release'])