2

我查找了一些信息,rpm模块只能用于搜索已安装的rpm包信息。我想使用python rpm模块搜索文件夹中的*.rpm文件并了解它们的信息,如发行版或版本。这可以使用吗rpm 模块?

4

2 回答 2

3

如果有人在寻找答案时仍然在这里结束,您可以使用 python-rpm 做到这一点:

import os
import rpm

fdno = os.open(PATH_TO_RPM_FILE, os.O_RDONLY)
ts = rpm.ts()
hdr = ts.hdrFromFdno(fdno)
os.close(fdno)

(注意对 os.close() 的调用)

现在hdr保存 RPM 标头信息。您可以使用 RPMTAG_* 值作为键以 dict 样式访问各个属性,例如:

arch = hdr[rpm.RPMTAG_ARCH]

您可以尝试使用以下方法对所有可能的 RPMTAG_* 值进行逆向工程dir()

import rpm
print '\n'.join(filter(lambda x: x.startswith('RPMTAG'), dir(rpm)))

您也可以调用keys()on hdr,但它会将可能的键作为整数返回,这可能不那么友好。

我发现当要处理大量 RPM 文件时,使用 python-rpm 而不是调用命令行工具作为子进程可以显着提高性能。

有关详细信息,请参阅http://rpm5.org/docs/api/classRpmhdr.html

于 2014-04-03T09:13:25.137 回答
0

据我所知,没有办法做到这一点。您最简单的选择是直接调用 rpm 命令并解析数据

subprocess.check_output( ["rpm", "-qip", "CentOS_Image/Packages/python-2.6.6-29.el6_2.2.x86_64.rpm" ] )

'Name        : python\nVersion     : 2.6.6\nRelease     : 29.el6_2.2\nArchitecture: x86_64\nInstall Date: (not installed)\nGroup       : Development/Languages\nSize        : 21290059\nLicense     : Python\nSignature   : RSA/SHA1, Mon 18 Jun 2012 14:47:20 BST, Key ID 0946fca2c105b9de\nSource RPM  : python-2.6.6-29.el6_2.2.src.rpm\nBuild Date  : Mon 18 Jun 2012 14:21:55 BST\nBuild Host  : c6b5.bsys.dev.centos.org\nRelocations : (not relocatable)\nPackager    : CentOS BuildSystem <http://bugs.centos.org>\nVendor      : CentOS\nURL         : http://www.python.org/\nSummary     : An interpreted, interactive, object-oriented programming language\nDescription :\nPython is an interpreted, interactive, object-oriented programming\nlanguage often compared to Tcl, Perl, Scheme or Java. Python includes\nmodules, classes, exceptions, very high level dynamic data types and\ndynamic typing. Python supports interfaces to many system calls and\nlibraries, as well as to various windowing systems (X11, Motif, Tk,\nMac and MFC).\n\nProgrammers can write new built-in modules for Python in C or C++.\nPython can be used as an extension language for applications that need\na programmable interface. This package contains most of the standard\nPython modules, as well as modules for interfacing to the Tix widget\nset for Tk and RPM.\n\nNote that documentation for Python is provided in the python-docs\npackage.\n'
于 2013-01-22T15:30:07.767 回答