Product SiteDocumentation Site

16.3. Programming with the RPM Database

Compared to the RPM C API, discussed in Chapter 15, Programming RPM with C , the Python API is much simpler and requires many fewer programming statements to get your job done.
Just about every Python RPM script needs a transaction set. Create a transaction set with rpm.TransactionSet:
import rpm
ts = rpm.TransactionSet()
The transaction set will automatically open the RPM database if needed.
Note
The code examples in this chapter follow the Red Hat conventions for naming variables, such as ts for a transaction set. This is to make it easier to read the Python examples in the RPM sources, along with Red Hat installer programs written in Python.
You will need a transaction set in just about every Python script that accesses RPM functionality.

16.3.1. Accessing the RPM database

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
See Chapter 4, Using the RPM Database for more on initializing, rebuilding, and verifying RPM databases.
Once you have a transaction set, you can start querying the RPM database.