Transaction sets provide a number of methods for working with the RPM database at the database level. Use these methods if you need to interact with the database as a whole, as opposed to accessing individual packages in the database. For example, you can initialize or rebuild the RPM database with these methods. You can also use a handy trick for accessing another RPM database instead of the default system database.
16.3.1.1. Setting the Database Location
A transaction set will open the RPM database assuming the default location. To specify a different RPM database location, call addMacro, as shown following:
rpm.addMacro("_dbpath", path_to_rpm_database)
You can work with more than one RPM database by setting the _dbpath macro, creating a transaction set, and then removing the macro. After doing this, you can create another transaction set for the default RPM database, allowing your script to work with more than one database. For example:
# Open the rpmdb-redhat database
rpm.addMacro("_dbpath", "/usr/lib/rpmdb/i386-redhat-linux/redhat")
solvets = rpm.TransactionSet()
solvets.openDB()
rpm.delMacro("_dbpath")
# Open default database
ts = rpm.TransactionSet()
This example uses the rpmdb-redhat package, which holds a database of all Red Hat Linux packages. The explicit call to openDB opens the RPM database. In most Python scripts, though, you do not want to call openDB. Instead, a transaction set will open the database as needed.
The call to delMacro removes the _dbpath macro, allowing the next call to TransactionSet to use the default RPM database.
Note
Do not call closeDB on a transaction set. This method does indeed close the RPM database, but it also disables the ability to automatically open the RPM database as needed.
16.3.1.2. Initializing, Rebuilding, and Verifying the Database
The transaction set provides an initDB method to initialize a new RPM database. This acts like the rpm --initdb command.
ts.initDB()
The rebuildDB method regenerates the RPM database indices, like the rpm --rebuilddb command:
ts.rebuildDB()
The rebuildDB method regenerates the RPM database indices, like the rpm --rebuilddb command.
The verifyDB method checks that the RPM database and indices are readable by the Berkeley DB library:
ts.verifyDB()
Calling this method is the same as running the db_verify command on each of the database files in /var/lib/rpm.
Cross Reference
Once you have a transaction set, you can start querying the RPM database.