Product SiteDocumentation Site

Examining the package header

The code in Listing 17-1 introduces the package header object, an object of the hdr class. This represents a package header, and contains entries such as the name, version, pre- and post-installation scripts, and triggers.

The hdr Class

You can access each entry in the header using Python's dictionary syntax. This is much more convenient than calling headerGetEntry in C programs. The basic syntax to access header entries follows:
value = h['tag_name']
For example, to get the package name, use the following code:
name = h['name']
You can also use a set of predefined RPMTAG_ constants that match the C API. These constants are defined in the rpm module. For example:
name = h[rpm.RPMTAG_NAME]
Using the rpm constants such as rpm.RPMTAG_NAME is faster than using the strings such as 'name'.
For header entries that hold an array of strings, such as the list of files in the package, the data returned is a Python list. For example:
print "Files:"
files = h['FILENAMES']
for name in files:
    print name
You can use file info sets to achieve more compact code. For example:
print "Files:"
fi = h.fiFromHeader()
print fi
The requires, provides, obsoletes, and conflicts information each appear as three separate but related lists for each set of information, with three lists for the requires information, three for the provides information, and so on. You can extract this information using Python dependency sets using the simple code following:
print h.dsFromHeader('providename')
print h.dsFromHeader('requirename')
print h.dsFromHeader('obsoletename')
print h.dsFromHeader('conflictname')
Cross Reference
The script in Listing 17-3 shows how to print out this information.