2

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.

Sherein
  • 947
  • 1
  • 11
  • 23

3 Answers3

3

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))
nabeel
  • 319
  • 1
  • 9
2

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
...
1

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.

Mike Andrews
  • 3,045
  • 18
  • 28
  • Can you add more data about what do you mean by refer to the RPM source code to figure out how to interpret it. – Sherein Dec 16 '19 at 12:56
  • The RPM source code is up on github: https://github.com/rpm-software-management/rpm. This is likely way more than you wanted to get into. Perhaps, start by seeing if the `rpm` program itself does what you want, with its command line options. Do `man rpm`, and find tutorials online about how to use it. – Mike Andrews Dec 16 '19 at 16:10