Product SiteDocumentation Site

17.3.5. Getting information on packages

The tag, as_nvre, and is_source_package subroutines that worked on header objects read from RPM files, shown previously, also work with package entries returned from the RPM database.
For example, Listing 18-5 shows a script, rpminfo.pl, that prints out descriptive information about a given package.
Listing 18-5: rpminfo.pl
#!/usr/bin/perl
#
# Queries RPM database for given package and prints info.
# Usage:
# rpminfo.pl package_name
#
use strict;
use RPM2;
my $rpm_db = RPM2->open_rpm_db( "-path" => "/var/lib/rpm" );
my $pkg_iter = $rpm_db->find_by_name_iter( $ARGV[0] );
while (my $pkg = $pkg_iter->next() ) {
printInfo( $pkg );
}
$rpm_db->close_rpm_db();
# Prints info on one package.
sub printInfo {
my($pkg) = shift;
print $pkg->as_nvre(), ", ", $pkg->tag("ARCH"), ", ",
$pkg->tag("OS"), ", ", $pkg->tag("PLATFORM"), "\n";
print $pkg->tag("SUMMARY"), "\n";
print "Group: ", $pkg->tag("GROUP"), "\n";
print $pkg->tag("DESCRIPTION"), "\n";
print "Vendor: ", $pkg->tag("VENDOR"), ", ", $pkg->tag("URL"), "\n";
print "Size: ", $pkg->tag("SIZE"), "\n";
}
When you run this script, you’ll see output like the following:
$ ./rpminfo.pl XFree86
XFree86-4.2.0-72, i386, linux, i386-redhat-linux-gnu
The basic fonts, programs and docs for an X workstation.
Group: User Interface/X
XFree86 is an open source implementation of the X Window System. It
provides the basic low level functionality which full fledged
graphical user interfaces (GUIs) such as GNOME and KDE are designed
upon.
Vendor: Red Hat, Inc., http://www.xfree86.org
Size: 30552239

17.3.5.1. Listing the Installed Date

The installed date is a number value representing the number of seconds since the start of the UNIX epoch, January 1, 1970, which predates the start of the Linux epoch by about 20 years. So, when you get the value of the INSTALLTIME tag, you’ll see a meaningless number.
To make sense of this number, pass the value to the Perl localtime function. Listing 18-6 shows an example of this.
Listing 18-6: rpmdate.pl
#!/usr/bin/perl
#
# Queries RPM database for given package,
# prints out name, vendor, and date installed.
# Usage:
# rpmdate.pl package_name
#
use strict;
use RPM2;
my $rpm_db = RPM2->open_rpm_db();
my $pkg_iter = $rpm_db->find_by_name_iter( $ARGV[0] );
while (my $pkg = $pkg_iter->next() ) {
printDate( $pkg );
}
$rpm_db->close_rpm_db();
# Prints installation data for one package.
sub printDate {
my($pkg) = shift;
my $date = localtime( $pkg->tag("INSTALLTIME") );
printf("%-20s %-17s %s\n", $pkg->as_nvre(), $pkg->tag("VENDOR"), $date);
}
Note
The printf function in this script can do something the rpm command cannot do. Even with the --queryformat option, you cannot group multiple items and then set the size; with Perl, you can. Simply assign the multiple values to a string, or use the handy as_nvre subroutine, which gathers up to four tags together into one string.
When you pass the name of a package to this script, you’ll see the date the package was installed. For example:
$ ./rpmdate.pl kernel
kernel-2.4.18-14 Red Hat, Inc. Sat Oct 5 12:29:58 2002