A simple script which queries a rpm package and prints few important rpm tags.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import sys, os, rpm
def get_rpm_info(rpm_file):
"""Returns rpm information by querying a rpm"""
ts = rpm.ts()
fdno = os.open(rpm_file, os.O_RDONLY)
try:
hdr = ts.hdrFromFdno(fdno)
except rpm.error:
fdno = os.open(rpm_file, os.O_RDONLY)
ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES)
hdr = ts.hdrFromFdno(fdno)
os.close(fdno)
return { 'name': hdr[rpm.RPMTAG_NAME], 'ver' : "%s-%s" % (hdr[rpm.RPMTAG_VERSION],\
hdr[rpm.RPMTAG_RELEASE]), 'epoch': hdr[rpm.RPMTAG_EPOCH],\
'arch': hdr[rpm.RPMTAG_ARCH] }
if __name__ == '__main__':
blob = sys.argv[1]
rpm_info = get_rpm_info(blob)
for key in rpm_info:
print '%s:%s' % (key.ljust(11), rpm_info[key])
|
This recipe describe how to extract information of a rpm by querying a rpm file.
Example here queries rpm for the tags name, version, release etc. However rpm module supports many more tags.
Use dir(rpm) to see full list attributes.
get_rpm_info function:
rpm.ts is an alias for rpm.TransactionSet.
You may have noticed and wondering about try / except block used in this function.
The pupose is you may not have rpm vendor's public key imported to your rpm database.
Generally all rpms has signed by rpm packager (vendor).
You need to import packager's public key in order to check origin of the package and package integrity.
If the public key is not present on your system you need to set flags to ignore signature checks otherwise query fails.
It is always recommeneded to check the signature but for demonstration pupose I hae included the try/except block.
rpm -qa gpg-pubkey*
shows you registered public keys.
In get_rpm_info function we are reading rpm header into hdr object and then get the required rpm tags.
Further reading: