Replacing Unstable Shell RPM Checks with Python’s rpm‑python Library
The article explains why a fragile Shell script for comparing local RPM packages to a central repository was rewritten in Python using the rpm‑python library, demonstrates how to install and import the library, and showcases practical code examples for querying both installed and offline RPM packages.
1. The Problem with Shell Scripts
A colleague’s Shell script that compared local RPM package versions with a central repository proved unstable and hard to maintain because RPM version strings have many formats, making simple string parsing impossible.
To solve this, the author searched for a Python interface that could directly access RPM metadata on Linux machines. yum install -y rpm-python After installing the rpm-python package (located in /usr/lib64/python2.7/site-packages/), the library can be imported in Python scripts.
>>> import rpm
>>> rpm.__path__
['/usr/lib64/python2.7/site-packages/rpm']2. Discovering a Helpful Resource
Searching online revealed a website that aggregates real‑world code examples for Python libraries. By entering a library name, the site returns usage snippets extracted from open‑source projects, along with the number of projects that use the library.
The site (https://www.programcreek.com/python/) lists roughly 2,000 libraries and provides concrete examples that are more practical than the official Python documentation.
3. Using the rpm Library
Getting Information About an Uninstalled RPM Package
>> import rpm
>>> ts = rpm.TransactionSet()
>>> rpmhdr = ts.hdrFromFdno("/root/librbd1-devel-10.2.10-0.el7.centos.x86_64.rpm")
>>> rpmhdr["NAME"]
'librbd1-devel'
>>> rpmhdr["VERSION"]
'10.2.10'
>>> rpmhdr["RELEASE"]
'0.el7.centos'
>>> rpmhdr["ARCH"]
'x86_64'Getting Information About an Installed RPM Package
>> import rpm
>>> ts = rpm.TransactionSet()
>>> query = ts.dbMatch("name", "librbd1")
>>> query.count()
1
>>> pkg_info = next(query)
>>> pkg_info["NAME"]
'librbd1'
>>> pkg_info["VERSION"]
'12.2.9.1.1'
>>> pkg_info["RELEASE"]
'0.el7.centos'
>>> pkg_info["ARCH"]
'x86_64'These examples were extracted from the aforementioned website, which curates code snippets from real projects rather than the standard library documentation.
4. Key Takeaways
Python’s rpm-python library provides a reliable way to query both installed and offline RPM packages, avoiding the brittleness of Shell parsing.
The programcreek.com site is a valuable reference for discovering practical usage patterns of many Python libraries.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
