且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查软件包是否已安装的Python方式

更新时间:2023-11-26 21:44:16

import sys
import rpm

ts = rpm.TransactionSet()
mi = ts.dbMatch( 'name', sys.argv[1] )
try :
    h = mi.next()
    print "%s-%s-%s" % (h['name'], h['version'], h['release'])
except StopIteration:
    print "Package not found"




  1. TransactionSet()将打开RPM数据库

  2. dbMatch(不带参数)将设置一个匹配迭代器来遍历整套已安装的软件包,您可以在match迭代器上调用next来获取下一个条目,即代表一个包的标头对象

  3. dbMatch也可以用于查询特定的包,您需要传递名称标签的名称,以及您要查找的标签的值:

  1. TransactionSet() will open the RPM database
  2. dbMatch with no paramters will set up a match iterator to go over the entire set of installed packages, you can call next on the match iterator to get the next entry, a header object that represents one package
  3. dbMatch can also be used to query specific packages, you need to pass the name of a tag, as well as the value for that tag that you are looking for:

dbMatch('name','mysql')