如何通过 python apt 监控软件包删除或安装的进度?

如何通过 python apt 监控软件包删除或安装的进度?

例如,假设我想通过下面的脚本删除 ubuntu 中的 chromium-browser。我该如何监控删除进度?

#!/usr/bin/env python
# aptremove.py

import apt
import apt_pkg
import sys

def remove():
    pkg_name = "chromium-browser"
    cache = apt.cache.Cache()
    cache.open(None)
    pkg = cache[pkg_name]
    cache.update()
    pkg.mark_delete(True, purge=True)
    resolver = apt.cache.ProblemResolver(cache)

    if pkg.is_installed is False:
        print (pkg_name + " not installed so not removed")
    else:
        for pkg in cache.get_changes():
            if pkg.mark_delete:
                print pkg_name + " is installed and will be removed"
                print " %d package(s) will be removed" % cache.delete_count
                resolver.remove(pkg)
    try:
        cache.commit()
        cache.close()
    except Exception, arg:
        print >> sys.stderr, "Sorry, package removal failed [{err}]".format(err=str(arg))

remove()

相关内容