Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check progress of commit() in python_apt by using apt.progress class?

Tags:

python

debian

apt

I'm checking out this code provided by python_apt but it appears to be a bit outdated:

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

All I'm attempting to do here is follow the progress of the commit() method; currently when we call commit() and pass in fprogress and iprogress, I can see on the console that all packages in the pkg_list are downloaded correctly, the problem appears to come after this.

Program continues execution and it doesn't trigger dpkg_status_change() as I believe it should?

I have no way of knowing if the installation of multiple packages was a success?

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...
like image 879
Dayan Avatar asked Feb 10 '13 00:02

Dayan


1 Answers

Apparently working with python_apt commit() is a pain, I ended up using subprocess to wait for everything to finish (like it should be) and parse the output at the end to verify that the package was indeed upgraded.

In _apt_update() i check to make sure the system is online before attempting a apt-get update, then proceed with he installation of the package passed in to _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
like image 154
Dayan Avatar answered Oct 16 '22 03:10

Dayan