Product SiteDocumentation Site

Printing Header Information with sprintf

In addition to using the Python dictionary syntax, you can use the sprintf method on a header to format data using a syntax exactly the same as the query format tags supported by the rpm command.
Cross Reference
Chapter 4, Using the RPM Database covers query formats.
The basic syntax is as follows:
You can also use special formatting additions to the tag name. For example:
print "Header signature: ", h.sprintf("%{DSAHEADER:pgpsig}")
print "%-20s: %s" % ('Installed on', h.sprintf("%{INSTALLTID:date}") )
You can combine this information into functions that print out header entries with specific formatting. For example:
def nvr(h):
    return h.sprintf("%{NAME}-%{VERSION}-%{RELEASE}")
Note that you only really need to use sprintf when you need the format modifiers, such as date on %{INSTALLTID:date}. In most other cases, Python’s string-handling functions will work better.