如何使用 rpm-python 管理 rpm 包

By iswbm / Published At 2024-06-01 / In categories Programming, Python

python

安装 rpm-python 包

在使用之前请先安装 rpm 的 Python API 库

$ yum install -y rpm-python

然后就可以在 Python 中 使用 rpm-python了

>>> import rpm
>>> rpm.__path__
['/usr/lib64/python2.7/site-packages/rpm']

由于不是通过 pip 安装的,因此 rpm-python 是安装在 /usr/lib64/python2.7/site-packages/ 目录下的。

获取未安装的 rpm 包信息

>>> 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'

获取已安装的 rpm 包信息

>>> 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'

参考文章

iswbm

Author

iswbm

Cloud Computing & Container & Front-end & Back-end R&D Engineer. I like to explore new technologies, and in my spare time, I also play around with Logseq and other efficiency tools. You can follow me on GitHub to learn more, or add me on WeChat (stromwbm) to communicate with me.