如何使用python_apt检查commit()的进度适当的进展班级?

2024-04-29 08:54:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

Tags: selfpackagecachedefstatusaptpkgchange
1条回答
网友
1楼 · 发布于 2024-04-29 08:54:11

显然,使用python_aptcommit()是一件痛苦的事,我最后使用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

相关问题 更多 >