Samstag, 4. Oktober 2008

Getting rpm header information with python

Once you have rpm-python installed (a quick zypper -i rpm-python does the trick on openSUSE), fire up a python console. The assumption is, that there is a rpm file in /tmp/mypackage.rpm. Import the module:
import rpm
Now try this:

ts = rpm.ts()
fdno = os.open("/tmp/mypackage.rpm", os.O_RDONLY)
hdr = ts.hdrFromFdno(fdno)
hdr is a dictionary containing all the header information in the rpm. Some typical values which you might want to get might be:
print hdr['name'], hdr['version'], hdr['license']

If these header values exist, you would get something like:
mypackage 0.1 GPLv2 or later
One thing which might jump up to bite is if the gpg key with which the rpm was signed is not available. Now, I'm ususally unterwegs with openSUSE, so obviously I have the SUSE Linux Products GmbH key installed. If you need to query rpms from another system, you might have to import the key (once you've downloaded it) with something like rpm --import key.txt. Otherwise you might get an error like:

warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID 37017186
public key not available
Error with rpm : public key not available