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

最新文章

  1. setTimeout调用带参数的函数的方法
  2. 小机房的树 codevs 2370
  3. Android loading进度条使用简单总结
  4. updatePanel导致JS失效的解决办法(转)
  5. C++primer 练习12.6
  6. CocoaPods使用命令
  7. 线程池ThreadPoolExecutor使用简介
  8. js监听滚动条事件
  9. HDU 2222 Keywords Search (AC自动机)
  10. 【集训笔记】二分图及其应用【HDOJ1068【HDOJ1150【HDOJ1151
  11. pdftk的使用介绍
  12. Python批量修改文件名与后缀
  13. 在Ubuntu14.04下安装 labelImg (标数据用)
  14. 动手搭建第一个小程序音视频Demo
  15. AngularJS - 使用RequireJS还是Browserify?
  16. 封装一个 员工类 使用preparedStatement 查询数据 (2) 使用 arrayList 集合
  17. JS prototype chaining(原型链)整理中······
  18. python 图像转矩阵,矩阵转图像
  19. [笔记]_ELVE_正则表达式
  20. 【BZOJ2310】ParkII 插头DP

热门文章

  1. hdu 1907 尼姆博弈
  2. Apple Pay强势来袭,开发者应做的事情(转)
  3. cf660E Different Subsets For All Tuples
  4. java私有构造函数
  5. python常用模块详解(一)
  6. Python入门--14--字典
  7. Android开发把项目打包成apk-(转)
  8. GIL锁、死锁、递归锁、定时器
  9. codeforces 1041 e 构造
  10. VMware 虚拟机下载链接