Product SiteDocumentation Site

Comparing versions

The RPM2 module overrides the spaceship operator, <=>, to perform version comparisons between packages. The script in Listing 18-8 shows how to compare all local RPM files against the newest installed version of the same package, if the package is installed.
Listing 18-8: rpmver.pl
#!/usr/bin/perl -w
#
# Compare versions of all *.rpm files against the
# latest packages installed (if installed)
#
# Usage:
# rpmver.pl
# This script looks for all *.rpm files.
#
use strict;
use RPM2;
my $rpm_db = RPM2->open_rpm_db();
for my $filename (<*.rpm>) {
my $h = RPM2->open_package( $filename );
# Ensure we compare against the newest
# package of the given name.
my ($installed) =
sort { $b <=> $a } $rpm_db->find_by_name($h->name);
if (not $installed) {
printf "Package %s not installed.\n", $h->as_nvre;
} else {
my ($result) = ($h <=> $installed);
if ($result < 0) {
printf "Installed package %s newer than file %s\n",
$installed->as_nvre,
$h->as_nvre;
} else {
printf "File %s newer than installed package %s\n",
$h->as_nvre,
$installed->as_nvre;
}
}
}
The sort { $a <=> $b } in front of the find_by_name call sorts all the packages of that name by the version number, so that the comparison is performed against the newest installed version of the package. The ($h <=> $installed) compares the header from the RPM file on disk against the newest installed version of the package.
When you run this script, you’ll see output like the following, depending on which RPM files you have in the local directory:
$ perl rpmver.pl
Package acroread-4.0-0 not installed.
Package canvas-7.0b2.0-1 not installed.
Installed package jikes-1.18-1 newer than file jikes-1.14-1
Installed package SDL-1.2.4-5 newer than file SDL-0.9.9-4
Package ted-2.8-1 not installed.