Problem Formulation: Compare RPM Versions in Python
- Given two lists of strings that are the RPM versions currently installed on your computer and the versions in a repository or another environment.
- How to compare those lists to find the RPMs that are out of date?
Example: Our goal is to determine a list of packages that have a different newer version.
Input: rpm_1 = ["my_package-3.2.2.rpm", "your_package-3.1.1.rpm", "their_package-3.1.1.rpm", "her_package-3.1.1.rpm", "his_package-3.1.1.rpm", "its_package-3.1.1.rpm"] rpm_2 = ["my_package-9.2.2.rpm", "your_package-3.1.1.rpm", "her_package-3.1.1.rpm", "their_package-9.1.1.rpm"] Output: Stale package my_package3.2.2.rpm has new version: 9.2.2 Stale package their_package3.1.1.rpm has new version: 9.1.1
Background: RPM is an acronym for RedHat Package Manager and it is a free open-source package management system for Linux to install packages with the file suffix .rpm
. An RPM package contains an arbitrary number of files to be extracted into the Linux operating system. It’s used for many modern Linux distributions such as Fedora, CentOS, OpenSUSE, and Oracle Linux.
Solution: We solve the problem with a simple dictionary versions
that tracks the versions of each of the old packages from rpm_1
and compares them with the versions in the newer list rpm_2
.
# RPM names assuming '-' indicates start of version number rpm_1 = ["my_package-3.2.2.rpm", "your_package-3.1.1.rpm", "their_package-3.1.1.rpm", "her_package-3.1.1.rpm", "his_package-3.1.1.rpm", "its_package-3.1.1.rpm"] rpm_2 = ["my_package-9.2.2.rpm", "your_package-3.1.1.rpm", "her_package-3.1.1.rpm", "their_package-9.1.1.rpm"] def compare(rpm_1, rpm_2): versions = dict() for s in rpm_1: name, version = s.split('-') version = version[:-4] versions[name] = version for s in rpm_2: name, version = s.split('-') version = version[:-4] if name in versions and version != versions[name]: print('Stale package', name + versions[name] + '.rpm', 'has new version: ', version) compare(rpm_1, rpm_2)
The output is the list of packages that differ in the version name in comparison to at least one old package name:
Stale package my_package3.2.2.rpm has new version: 9.2.2 Stale package their_package3.1.1.rpm has new version: 9.1.1