Product SiteDocumentation Site

Additional query subroutines

The find_by_name_iter subroutine finds a package by its name. The RPM2 module also supports a number of other query routines, listed in Table 18-1.
Table 18-1 RPM2 module query routines
Routine
Usage
find_all()
Returns a list with all the packages in the database
find_all_iter()
Returns an iterator over all the packages in the database
find_by_file($filename)
Finds all packages that own the given file, returning a list
find_by_file_iter($filename)
Finds all packages that own the given file, returning an iterator
find_by_name($package_name)
Finds all packages with the given name, returning a list
find_by_name_iter($package_name)
Finds all packages with the given name, returning an iterator
find_by_provides($capability)
Finds all packages that provide the given capability, returning a list
find_by_provides_iter($capability)
Finds all packages that provide the given capability, returning an iterator
find_by_requires($capability)
Finds all packages that require the given capability, returning a list
find_by_requires_iter($capability)
Finds all packages that require the given capability, returning an iterator
To verify the find routines, you can try the following script and compare the results with the rpm command. Listing 18-4 shows the script that finds what package provides a capability and also which packages require the capability.
Listing 18-4: rpmprovides.pl
#!/usr/bin/perl
#
# Queries RPM database for given package,
# listing what it provides and what other
# packages require the capability.
#
# Usage:
# rpmprovides.pl package_name
#
use strict;
use RPM2;
my $rpm_db = RPM2->open_rpm_db();
my $pkg_iter = $rpm_db->find_by_provides_iter( $ARGV[0] );
print "Provides: ", $ARGV[0], "\n";
while (my $pkg = $pkg_iter->next() ) {
print "\t", $pkg->as_nvre(), "\n";
}
# Now, what packages require this capability.
my $pkg_iter2 = $rpm_db->find_by_requires_iter( $ARGV[0] );
print "Requires: ", $ARGV[0], "\n";
while (my $pkg2 = $pkg_iter2->next() ) {
print "\t", $pkg2->as_nvre(), "\n";
}
$rpm_db->close_rpm_db();
When you run this script with the name of a capability, you'll see output like the following:
$ ./rpmprovides.pl httpd
Provides: httpd
httpd-2.0.40-8
Requires: httpd
mod_perl-1.99_05-3
5:redhat-config-httpd-1.0.1-13
mod_python-3.0.0-10
1:mod_ssl-2.0.40-8
Note
The 5: in 5:redhat-config-httpd-1.0.1-13 and 1: in 1:mod_ssl-2.0.40-8 represent the EPOCH tag value.
To verify this script, run the rpm -q command to see if you get the same packages listed. For example:
$ rpm -q --whatprovides httpd
httpd-2.0.40-8
$ rpm -q --whatrequires httpd
mod_perl-1.99_05-3
redhat-config-httpd-1.0.1-13
mod_python-3.0.0-10
mod_ssl-2.0.40-8
In both cases, you see the same packages listed. You can use this technique to verify your scripts.
Note
The find_by_provides_iter subroutine requires the name of a package, such as bash. You cannot pass a file name, such as /bin/bash, to get the name of the package that provides this capability (a file, really).