关于python中的rpm模块

时间:2013-01-22 15:23:12

标签: python rpm

我查找了一些信息,rpm模块只能用于搜索已安装的rpm包Information.I想使用python rpm模块搜索文件夹中的* .rpm文件,并知道他们的信息,如发布或版本.Is这可能使用rpm模块吗?

2 个答案:

答案 0 :(得分:2)

如果有人在搜索答案时仍然在这里结束,这里是你如何使用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]

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

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

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

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

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

答案 1 :(得分: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'