Parse an RPM name into its components

TomOnTime asked:

Is there a name-parsing tool that is part of the official RPM tool package?

I have a list of filenames. Each is the filename of an RPM package. I don’t have the actual packages, just the filenames. For each I need to extract the package name and version ($NAME and $VERSION). The reason I need this is I am writing a script that then makes sure that “yum install $VERSION” installs $VERSION. This is part of a system that builds packages and verifies they are properly uploaded.

The list of filenames looks like:

$ cat /tmp/packages.txt

I found the following code which is a BASH function that does the task:

function parse_rpm() { RPM=$1;B=${RPM##*/};B=${B%.rpm};A=${B##*.};B=${B%.*};R=${B##*-};B=${B%-*};V=${B##*-};B=${B%-*};N=$B;echo "$N $V $R $A"; }

for i in $(</tmp/packages.txt) ; do
    parse_rpm $i

It works. Mostly. There are some exceptions:

$ parse_rpm CentOS/6/x86_64/sei_dnsmaster-1.0-99.el6.x86_64.rpm
sei_dnsmaster 1.0 99.el6 x86_64

Notice that it didn’t get the version correctly (it should be 1.0-99)

I’m wondering (1) if there is a tool in the rpmdev package that does this correctly. (2) If not, is there an official regex I could use. (3) What is the python equivalent of that regex?

Thanks in advance!

My answer:

You don’t need to do any of this; RPM has a query format argument which will let you specify exactly the data you want to receive. It will even output without line endings if you don’t specify them.

For instance:

rpm --queryformat "%{NAME} %{VERSION} %{RELEASE} %{ARCH}" -q coreutils
rpm --queryformat "The version of %{NAME} is %{VERSION}n" -q coreutils

rpm --queryformat "%{NAME} %{VERSION} %{RELEASE} %{ARCH}" -qp file.rpm

The complete list of variables you can use can be obtained with:

rpm --querytags

Note that in the case of RELEASE, output like 84.el6 is normal and expected, since this is actually how RPM packages are versioned when packaged by or for a distribution.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.