initCommon(); $template->displayHeader(); ?>

17.2. Working with RPM Files

The RPM2 module provides a top-level object, RPM2, that acts as an entry point into the module. From the RPM2 object, you either open the RPM database, covered in the "Programming with the RPM Database" section, or open an RPM package file, covered here.

The first step in working with an RPM file is to open the file inside a Perl script.

17.2.1. Opening package files

The open_package subroutine opens an RPM package file and returns a header object (an RPM2::Header). The basic syntax follows:

my $header = RPM2->open_package( $filename );

For example:

my $header = RPM2->open_package("jikes-1.14-1-glibc-2.2.i386.rpm");

After you’ve opened a package, you can perform a number of query operations on the header object returned by the open_package subroutine.

17.2.2. Listing tags from the package

Each RPM package has information stored under a variety of tags, such as the package name under the NAME tag and the package long description under the DESCRIPTION tag.

Cross Reference

These are the same tags introduced with the --queryformat option to the rpm command discussed in Chapter 5.

The tag subroutine returns the value of a given tag. For example, to get the name of the package, use the NAME tag:

use RPM2;

my $header = RPM2->open_package("jikes-1.14-1-glibc-2.2.i386.rpm" );

print $header->tag("NAME"), "\n";

Pulling this together, Listing 18-1 shows example script that lists the name and one-line short summary of a package file.

Listing 18-1: rpmsum.pl

#!/usr/bin/perl

#

# Lists summary from an RPM package file

# Usage:

# rpmsum.pl package_name.rpm

#

use strict;

use RPM2;

my $header = RPM2->open_package( $ARGV[0] );

print $header->tag("NAME"), ": ", $header->tag("SUMMARY"), "\n";

Enter this script and name the file rpmsum.pl.

When you run this script, you need to pass the name of a package file on the command line. For example:

$ ./rpmsum.pl jikes-1.14-1-glibc-2.2.i386.rpm

jikes: java source to bytecode compiler

17.2.3. Convenience methods

The RPM2 module includes convenience methods for all RPM tags. This means you can use the method name in place of tag("NAME"). For example:

print $header->name(), ": ", $header->summary(), "\n";

17.2.4. Listing the name and version

The RPM2 module provides a handy subroutine for getting the NAME, VERSION, RELEASE, and EPOCH tags, often abbreviated as NVRE. The subroutine, as_nvre, returns a single string with these values in the standard format, with the values separated by minus signs.

Note

Usually, the EPOCH tag has no value. If there is an EPOCH value, you will see it output first, and then a colon, and then the name, version, and release values. For example:

5:redhat-config-httpd-1.0.1-13

In this case, the EPOCH value is 5.

You can call this subroutine on any header object, or any package object to get the full name of the package. For example:

print $header->as_nvre(), "\n";

17.2.5. Checking whether the package is a source package

Another handy subroutine tells you if an RPM file represents a source RPM or a binary RPM. The is_source_package subroutine returns a true value if the package is a source package, and a false value otherwise.

The rpmpkg.pl script, shown in Listing 18-2, shows how to use the as_nvre and is_source_package subroutines.

Listing 18-2: rpmpkg.pl

#!/usr/bin/perl

#

# Queries RPM package file and prints

# out name and whether this is a source pkg.

# Usage:

# rpmpkg.pl package_name

#

use strict;

use RPM2;

my $header = RPM2->open_package( $ARGV[0] );

if ( $header->is_source_package() ) {

print "Source package ", $header->as_nvre(), "\n";

} else {

print $header->as_nvre(), "\n";

}

displayFooter('$Date: 2005/11/02 19:30:06 $'); ?>