2013-01-22 88 views
1

我查了一些資料,rpm模塊只能用於搜索安裝的rpm包的信息。我想用python rpm模塊搜索文件夾中的* .rpm文件並且知道他們的信息,比如版本或版本。是否可以使用rpm模塊?關於python中的rpm模塊

+0

我敢肯定,'yum'是所有用Python編寫的,所以你可能要檢查了這一點。我認爲默認情況下會安裝Python RPM庫(現在在Windows上,對不起)。 –

回答

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' 

+1

你會想看看'查詢格式',以便於解析。 –

1

如果有人還在這裏結束了,當搜尋答案,這裏是你如何使用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_ *值作爲鍵的字典式的訪問單個屬性,例如:

arch = hdr[rpm.RPMTAG_ARCH] 

你可以嘗試反向工程的所有可能RPMTAG_ *值使用dir()

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