博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
querying rpm database
阅读量:7121 次
发布时间:2019-06-28

本文共 2129 字,大约阅读时间需要 7 分钟。

Call dbMatch on a transaction set to create a match iterator. As with the C API, a match iterator allows your code to iterate over the packages that match a given criteria.
A call to dbMatch with no parameters means to set up a match iterator to go over the entire set of installed packages. The basic format follows:
import rpm
 
ts = rpm.TransactionSet()
 
mi = ts.dbMatch()
for h in mi:
# Do something with header object...
In this example, the call to dbMatch returns a match iterator. The for loop iterates over the match iterator, returning one header each time.
In addition to this syntax, you can call next on the match iterator to get the next entry, a header object that represents one package. For example:
import rpm
 
ts = rpm.TransactionSet()
 
mi = ts.dbMatch()
while mi:
h = mi.next()
# Do something with the header object
The explicit call to next on the match iterator will likely no longer be supported in a future version of the RPM Python API, since the PEP-234 (Python Enhancement Proposal) calls for one means or the other for iterating, but not both.
For example, Listing 17-1 shows a Python script to print out the name, version, and release information for all installed packages.
Listing 17-1: rpmqa.py
#!/usr/bin/python
 
# Acts like rpm -qa and lists the names of all the installed packages.
# Usage:
# python rpmqa.py
 
import rpm
 
ts = rpm.TransactionSet()
 
mi = ts.dbMatch()
for h in mi:
 
print "%s-%s-%s" % (h['name'], h['version'], h['release'])
When you call this script, you should see output like the following, truncated for space:
$ python rpmqa.py
libbonoboui-2.0.1-2
attr-2.0.8-3
dhclient-3.0pl1-9
file-3.37-8
hdparm-5.2-1
ksymoops-2.4.5-1
imlib-1.9.13-9
logwatch-2.6-8
mtr-0.49-7
openssh-clients-3.4p1-2
pax-3.0-4
python-optik-1.3-2
dump-0.4b28-4
sendmail-8.12.5-7
sudo-1.6.6-1
mkbootdisk-1.4.8-1
telnet-0.17-23
usbutils-0.9-7
wvdial-1.53-7
docbook-dtds-1.0-14
urw-fonts-2.0-26
db4-utils-4.0.14-14
libogg-devel-1.0-1
Note
If you set the execute permission on this script, you can skip the explicit call to the python command. For example:
$ ./rpmqa.pyr

转载于:https://www.cnblogs.com/hustdc/p/7154302.html

你可能感兴趣的文章
SylixOS USB Gadget层介绍
查看>>
python设计模式之单例模式
查看>>
Pandas里面dataframe 中loc和iloc函数的区别
查看>>
MySql 常用函数
查看>>
java日志框架log4j详细配置及与slf4j联合使用教程
查看>>
java ArrayList中的subList方法
查看>>
java 7 的新特性
查看>>
Android设备新型恶意软件,融合银行木马、键盘记录器和移动勒索软件等功能
查看>>
SpringBoot使用jsp
查看>>
爬取了陈奕迅新歌《我们》10万条评论数据发现:原来,有些人只适合遇见
查看>>
如何在win10系统上安装linux子系统
查看>>
Android零基础入门第68节:完善RecyclerView,添加首尾视图
查看>>
mcs-51单片机CPU的内部结构及工作原理
查看>>
深浅copy
查看>>
eclipse运行maven项目,tomcat启动报错
查看>>
kali 安装grub theme
查看>>
Ubuntu安装JDK,搭建java环境
查看>>
AFNetworking框架下的SSL服务器证书的自定义验证
查看>>
redis——哈希(hash)
查看>>
开启Restful
查看>>