[Tutor] How to Compare rpm version using Python Script
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Dec 2 11:07:37 EST 2019
On 02/12/2019 13:55, Asad wrote:
> I am trying to use Json to serialize the data
...
> It gives a list of rpm separated by space . How can I convert to json so
> that I can then make it a python dictionary object dictOne ?
I'm not sure why you think you need JSON? Can you not just save the
space separated data into a text file and process that line by line?
I'm not sure how JSON helps?
You can then read the lines back and use line.split() to get the
individual fields in a list.
> dictTwo is a required list of rpm's which is to be checked .
If it is a list why is it called a dict?
It is always better to avoid putting types into variable names.
Better to describe the variables purpose.
> for rpmOne in dictOne:
> for rpmTwo in dictTwo:
> if rpmOne["name"] == rpmTwo["name"]:
> if rpmOne["arch"] == "x86_64" :
> if rpmOne["version"] >= rpmTwo["version"] :
> #print rpm["name"],rpm["arch"],rpm["version"], "Installed"
> Installedrpm.append([rpm["name"],rpm["arch"],rpm["version"]])
> else :
> #print rpm["name"],rpm["arch"],rpm["version"] , "Version is
> less than required"
> Installedrpm.append([rpm["name"],rpm["arch"],rpm["version"]])
> elif rpm["arch"] == "i686" :
> if rpm["version"] >= rpmTwo["version"] :
> #print rpm["name"],rpm["arch"],rpm["version"], "Installed"
> Installedrpm.append([rpm["name"],rpm["arch"],rpm["version"]])
> else :
> #print rpm["name"],rpm["arch"],rpm["version"] , "Version is
> less than required"
> Installedrpm.append([rpm["name"],rpm["arch"],rpm["version"]])
> else:
> continue
> else :
>
> NotInstalledrpm.append([rpm["name"],rpm["arch"],rpm["version"]])
This confuses the actual tests with the outcome actions. It is probably
better to put the tests in a comparison function (or set of functions).
Better still create a class from the line data and write some comparison
operators as methods. Then you can do something like:
rpm1 = RPM(file.readline().strip().split())
rpm2 = RPM(file.readline().strip().split())
if rpm1 > rpm2: # save one of them somewhere...
elif rpm1 < rpm2 # deal with the opposite cae
else: # deal with the equality case.
> for install in Installedrpm :
> print install[0],install[1],install[2]
Would become
for rpm in installed_rpms:
print rpm # using the __str__ method of RPM class.
> Please advice if there could be simpler or smarter way to achieve this .
Probably not much simpler to code but a lot simpler to
read the final logic
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list