I'm trying to find out how I can compare 2 lists of RPMS (Currently installed) and (Available in local repository) and see which RPMS are out of date. I've been tinkering with regex but there are so many different naming standards for RPMS that i can't get a good list to work with. I don't have the actual RPMS on my drive so i can't do rpm -qif.
pattern1 = re.compile(r'^([a-zA-Z0-9_\-\+]*)-([a-zA-Z0-9_\.]*)-([a-zA-Z0-9_\.]*)\.(.*)')
for rpm in listOfRpms:
packageInfo = pattern1.search(rpm[0]).groups()
print packageInfo
This works for a vast majority but not all (2300 / 2400)
yum-metadata-parser-1.1.2-2.el5
('yum-metadata-parser', '1.1.2', '2', 'el5') **What I need
But none these work for instance unless I break some others that worked before..
In RPM parlance, 2.el5
is the release field; 2 and el5 are not separate fields. However, release need not have a .
in it as your examples show. Drop the \.(.*)
from the end to capture the release field in one shot.
So now you have a package name, version, and release. The easiest way to compare them is to use rpm's python module:
import rpm
# t1 and t2 are tuples of (version, release)
def compare(t1, t2):
v1, r1 = t1
v2, r2 = t2
return rpm.labelCompare(('1', v1, r1), ('1', v2, r2))
What's that extra '1'
, you ask? That's epoch, and it overrides other version comparison considerations. Further, it's generally not available in the filename. Here, we're faking it to '1' for purposes of this exercise, but that may not be accurate at all. This is one of two reasons your logic is going to be off if you're going by file names alone.
The other reason that your logic may be different from rpm
's is the Obsoletes
field, which allows a package to be upgraded to a package with an entirely different name. If you're OK with these limitations, then proceed.
If you don't have the rpm
python library at hand, here's the logic for comparing each of release, version, and epoch as of rpm 4.4.2.3
:
[a-zA-Z]+
and numeric fields [0-9]+
separated by junk [^a-zA-Z0-9]*
.See lib/rpmvercmp.c
in the RPM source for the gory details.