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))
As Mike Andrews wrote, use rpm.
For instance you may use this :
$> cd /wherever/you/put/your/Packages/file
$> rpm -qa --last --dbpath $PWD
or
$> rpm -qa --last --dbpath /wherever/you/put/your/Packages/file
xorg-x11-drv-ati-firmware-7.1.0-3.el6.noarch Fri Nov 29 17:53:35 2013
ql2100-firmware-1.19.38-3.1.el6.noarch Fri Nov 29 17:53:35 2013
...
Berkeley DB is an embedded database that runs inside an application. All of the data stored in it is in a binary format, specific to the application that stored it there. Your best option for accessing the data is always to use the application itself - in your case, RPM.
If you've got to do something super special, your best option may be to pull down the sources for RPM and modify it to do what you want.
If you don't already have it installed on your system, there's a db_dump
program that's distributed with Berkeley DB. You can run it on the RPM database. But as the data is all in binary, you'll need to refer to the RPM source code to figure out how to interpret it.