如何使用apt.progress类检查python_apt中commit()的进度?
我在查看一个叫 python_apt
的代码,但感觉这个代码有点过时了:
https://github.com/jolicloud/python-apt/blob/master/doc/examples/inst.py
我现在想做的就是跟踪一下 commit()
这个方法的进展;目前当我们调用 commit()
并传入 fprogress
和 iprogress
时,我在控制台上可以看到 pkg_list
中的所有包都下载得很顺利,但问题似乎出现在这之后。
程序继续执行,但我觉得它应该触发 dpkg_status_change()
,可它并没有?
我不知道安装多个包是否成功?
import apt
from apt.progress.base import InstallProgress
class InstallStatusUpdate(InstallProgress):
def conffile(self, current, new):
print "conffile prompt: %s %s" % (current, new)
def processing(self, pkg, stage):
print "Processing ", pkg, " stage: ", stage
def error(self, pkg, errormsg):
print "Package ", pkg, " error: ", errormsg
def finish_update(self):
print "Installation is complete"
def status_change(self, pkg, percent, status):
print "Package: ", pkg, " at ", percent, " -> ", status
def dpkg_status_change(self, pkg, status):
print "Package ", pkg, ", Status: ", status
def install_updates(self, pkg_list):
fprogress = apt.progress.TextFetchProgress()
iprogress = InstallStatusUpdate()
cache_tmp = apt.Cache()
cache_tmp.update()
cache_tmp.open(None)
for pkg in pkg_list:
try:
self.pkgname = cache_tmp[pkg.name]
if self.pkgname.is_installed and self.pkgname.is_upgradable:
self.pkgname.mark_upgrade()
else:
self.pkgname.mark_install()
except Exception as e:
print e.message
result = self.pkgname.commit(fprogress, iprogress)
#Maybe i'm doing something wrong here but result always = None...
1 个回答
3
看起来使用 python_apt
的 commit()
方法挺麻烦的,我最后选择用 subprocess
来等待所有操作完成(就应该这样),然后在最后解析输出,以确认软件包确实已经升级。
在 _apt_update()
里,我会先检查系统是否在线,然后再尝试执行 apt-get update
,接着继续安装传递给 _apt_install()
的软件包。
def _apt_update(self):
import urllib2
try:
response = urllib2.urlopen('http://74.125.113.99', timeout=1)
#We have internet access
subprocess.Popen(['apt-get', 'update'])
return True
except urllib2.URLError as err: pass
return False
def _apt_install(self, pkg, update=True):
upgraded = 0
if update == True:
self._apt_update()
proc = subprocess.Popen(['apt-get', 'install', pkg, "-y"], stdout=subprocess.PIPE)
for line in proc.stdout:
if "upgraded" in line and "newly installed" in line and "to remove" in line:
values = line.split(",")
for pos in values:
key, value = pos.split(" ")
if value == "upgraded":
upgraded = int(key)
break
print "Upgraded OK (", upgraded, ")"
if upgraded > 0:
return True
else:
return False