[Tutor] How to Compare rpm version using Python Script

Mats Wichmann mats at wichmann.us
Fri Nov 29 09:20:06 EST 2019


On 11/29/19 5:32 AM, Asad wrote:
> Hi All ,
> 
>             I am thinking to write a script to compare the rpm version
> installed in a Linux system with a defined list of the rpm's . It the
> elements matches or the system has a higher version show success or else
> show failed .
> 
> I was planning to use the rpm command :
> 
> /bin/rpm -q --queryformat '%{name}-%{ARCH}-%{version}-%{release}\n'
> 
> to get the list of rpm's installed in the system . How to do the comparison
> for example :
> 
> Expected Value:compat-libcap1-1.10
> INFO: Actual Value:compat-libcap1-1.10-7.el7
> 
> This should show success .
> 
> Please share some knowledge and logic to code this .

We're going to ask that you think about it a bit and show what you've 
tried.  How might you compare individual entries in two lists that are 
each coming from external sources - one from running a command, one read 
from a file (note: I wouldn't advice doing an rpm query individually on 
each packagename, that will become rather...slow...)

Will say that a challenge is going to be to distinguish the package 
names from the versions when you're reading from a file.  For the 
installed packages this is easy, rpm already knows how to do that, and 
it's not a Python problem at all - your example above already shows how, 
you just put a separator between the pieces you want to look like 
different words, for example '%{name} %{version}'.

rpm packages are named in a way that makes it a challenge to figure out 
where the name ends and the version begins. It's not the first dash, 
because you have package names like "python3-libs" (and there are 
packages with more than two levels in the naming).  It's not the first 
digit, with the same name providing an example of that problem. It's not 
the last dash either, since there's one after the upstream version and 
before the build number in most pkgs, like 
'python3-libs-3.7.5-1.fc30.x86_64'.  Can you see a pattern?  Is there a 
Python function you could use to help with this?




More information about the Tutor mailing list