All RPM packages have headers that provide metadata for the package. Header objects can be returned by database queries or loaded from a binary package on disk.
The headerFromPackage function loads the package header from a package on disk. It returns a tuple of a "isSource" flag and the header object. The "isSource" flag is set to 1 if the package header was read from a source rpm or to 0 if the package header was read from a binary rpm.
For example:
The Python interface to the header data is quite elegant. It presents the data in a dictionary form. We'll take the header we just loaded and access the data within it:import os, rpm fd = os.open("/tmp/foo-1.0-1.i386.rpm", os.O_RDONLY) (header, isSource) = rpm.headerFromPackage(fd) fd.close()
in the case of our "foor-1.0-1.i386.rpm" package, this code would output:print header[rpm.RPMTAG_NAME] print header[rpm.RPMTAG_VERSION] print header[rpm.RPMTAG_RELEASE]
You make also access the header data by string name:foo 1.0 1
This method of access is a bit slower because the name must be translated into the tag number dynamically. You also must make sure the strings in header lookups don't get translated, or the lookups will fail.print header['name']