有沒有辦法讓python顯示安裝在服務器上的所有apt/yum軟件包?我有一個程序,只能抓住一個我指定的包,但我想知道是否有像python模塊apt-show-versions/yum check-update,因爲python-yum和python-apt只能做單個包。Python顯示所有已安裝的軟件包
謝謝。
編輯:
下面是代碼我目前:
# For finding the package version and using the package name -i
def aptpkg(package_name):
cache = apt.Cache()
pkg = cache[package_name]
host = subprocess.Popen('hostname', stdout=subprocess.PIPE, universal_newlines=True).stdout.read().strip()
if pkg.is_installed:
print host
print 'Current ' + package_name + ' installed:', pkg.installed.version
con.execute("insert into ansible_packagelist(date, host, package_name, installed_version) values (current_timestamp,%s,%s,%s)", (host, package_name, pkg.installed.version,))
else:
print host, package_name + ' is not installed on this system.\n'
if pkg.is_upgradable:
print 'Upgradeable version of ' + package_name + ' :', pkg.candidate.version
con.execute("update ansible_packagelist set upgradeable_version = %s where package_name = %s", (pkg.candidate.version, package_name))
db.commit()
你可以粘貼你目前擁有的代碼嗎? – 2014-09-29 17:47:32
您是否嘗試過'[p for apt_pkg.packages如果p.is_installed]'? http://apt.alioth.debian.org/python-apt-doc/library/apt_pkg.html#apt_pkg.packages – Dunes 2014-10-10 21:09:12