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