本文介紹了檢查是否安裝了包的 Pythonic 方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
檢查安裝在 Centos/Redhat 中的軟件包列表的 Pythonic 方法?
Pythonic way to check list of packages installed in Centos/Redhat?
在 bash 腳本中,我會這樣做:
In a bash script, I'd do:
rpm -qa | grep -w packagename
推薦答案
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"
- TransactionSet() 將打開 RPM 數據庫
- 不帶參數的dbMatch會設置一個匹配迭代器來遍歷整個安裝的包,你可以在匹配迭代器上調用next來獲取下一個條目,一個代表一個包的頭對象
dbMatch 也可用于查詢特定的包,您需要傳遞一個標簽的名稱,以及您要查找的該標簽的值:
- TransactionSet() will open the RPM database
- 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
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')
這篇關于檢查是否安裝了包的 Pythonic 方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!