gpt4 book ai didi

python - 如何使用 apt.progress 类检查 python_apt 中 commit() 的进度?

转载 作者:太空狗 更新时间:2023-10-29 21:57:19 36 4
gpt4 key购买 nike

我正在检查 python_apt 提供的这段代码,但它似乎有点过时了:

https://github.com/jolicloud/python-apt/blob/master/doc/examples/inst.py

我在这里要做的就是跟踪 commit() 方法的进度;目前,当我们调用 commit() 并传入 fprogressiprogress 时,我可以在控制台上看到 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...

最佳答案

显然使用 python_apt commit() 很痛苦,我最终使用 subprocess 等待一切完成(就像它应该be) 并在最后解析输出以验证包确实已升级。

_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

关于python - 如何使用 apt.progress 类检查 python_apt 中 commit() 的进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14793427/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com