【发布时间】:2011-03-06 18:38:54
【问题描述】:
如何获取可用升级包的列表并使用 python 将其写入文件?
当我运行apt-get upgrade > output
它在 bash 中工作。我想我必须在python程序中发送陷阱信号(Ctrl+C)。
关于如何实现这一点的任何建议?
我现在在代码上尝试了这个:
#!/usr/bin/env python
import subprocess
apt = subprocess.Popen([r"apt-get", "-V", "upgrade", ">", "/usr/src/python/upgrade.log"], stdin=subprocess.PIPE)
apt_stdin = apt.communicate()[0]
但它退出并且不写入文件。
这可行,但是当我将它移植到其他 Debian 系统时出现错误:
import apt
cache=apt.Cache()
cache.update()
cache.open(None)
cache.upgrade()
for pkg in cache.get_changes():
# print pkg.name, pkg.summary
fileHandle = open('/tmp/upgrade.log', 'a')
fileHandle.write(pkg.name + " - " + pkg.summary + "\n")
还有错误....
/usr/lib/python2.5/site-packages/apt/__init__.py:18: FutureWarning: apt API not stable yet
warnings.warn("apt API not stable yet", FutureWarning)
Traceback (most recent call last):
File "apt-notify.py", line 13, in <module>
for pkg in cache.get_changes():
AttributeError: 'Cache' object has no attribute 'get_changes'
【问题讨论】:
-
也许你的其他 debian 系统正在使用旧版本的 python-apt
-
是正确的,代码运行的地方使用 python-apt 0.7.94.2 并且给出错误的系统使用 python-apt 0.7.7.1+nmu1,我不能在不更改操作系统版本的情况下更新它们。 ..