I have 'Packages' file in path var/lib/rpm in my Centos:8 based image. How can I read/ get 'Packages' file content
As I read in some documentation that this file is of type berkeley DB.. not sure about this.
You may use the "rpm" python library like below, it outputs the same result as
rpm -qa --dbpath /some/path/to/rpm/db
in JSON format
you maybe ignore the _db_backend for now as it's only value is "bdb"
import sys
import rpm
import json
if (len(sys.argv) != 3):
print ('invalid amount of input arguments!')
sys.exit()
# user inputs
dbtype = sys.argv[1]
dbpath = sys.argv[2]
# add macro to be used by rpm
rpm.addMacro("_db_backend", dbtype)
rpm.addMacro("_dbpath", dbpath)
# Open database
ts = rpm.TransactionSet()
ts.openDB()
# remove macro for future cases
rpm.delMacro("_db_backend")
rpm.delMacro("_dbpath")
allPackages = []
# retrieve all packages from BDB
mi = ts.dbMatch()
# build JSON object from all packages for easier parsing
for hdr in mi:
pkgDict = {}
allPackages.append(pkgDict)
pkgDict['NAME'] = hdr[rpm.RPMTAG_NAME]
pkgDict['VERSION'] = hdr[rpm.RPMTAG_VERSION]
pkgDict['RELEASE'] = hdr[rpm.RPMTAG_RELEASE]
pkgDict['ARCH'] = hdr[rpm.RPMTAG_ARCH]
print (json.dumps(allPackages))