Skip to content

Instantly share code, notes, and snippets.

@nickcharlton
Created July 31, 2013 00:21
Show Gist options
  • Save nickcharlton/6118272 to your computer and use it in GitHub Desktop.
Save nickcharlton/6118272 to your computer and use it in GitHub Desktop.
import sys
import apt_pkg
# init apt and config
apt_pkg.init()
# open the apt cache
try:
cache = apt_pkg.Cache()
except SystemError, e:
sys.stderr.write("Error: Opening the cache (%s)" % e)
sys.exit(-1)
# setup a DepCache instance to interact with the repo
depcache = apt_pkg.DepCache(cache)
# take into account apt policies
depcache.read_pinfile()
# initialise it
depcache.init()
# give up if packages are broken
if depcache.broken_count > 0:
sys.stderr.write("Error: Broken packages exist.")
sys.exit(-1)
# mark possible packages
try:
# run distro-upgrade
depcache.upgrade(True)
# reset if packages get marked as deleted -> we don't want to break anything
if depcache.del_count > 0:
depcache.init()
# then a standard upgrade
depcache.upgrade()
except SystemError, e:
sys.stderr.write("Error: Couldn't mark the upgrade (%s)" % e)
sys.exit(-1)
# run around the packages
upgrades = 0
for pkg in cache.packages:
# skip packages not marked as upgraded/installed
if not (depcache.marked_install(pkg) or depcache.marked_upgrade(pkg)):
continue
# increment the upgrade counter
upgrades = upgrades + 1
print "Amount of upgrades: %d" % upgrades
# return a count of possible packages
# or, maybe instead, collect a list of them to make it more useful
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment