Skip to content

Instantly share code, notes, and snippets.

@theirix
Created July 5, 2014 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theirix/fcb65ec50cbfe873a8c8 to your computer and use it in GitHub Desktop.
Save theirix/fcb65ec50cbfe873a8c8 to your computer and use it in GitHub Desktop.
Find upgradable debian packages with major version changes
#!/usr/bin/env python
"""
apt-majorchanges.py
Find upgradable debian packages with major version changes
"""
import apt
__author__ = "theirix"
__license__ = "BSD"
def normalize_version(ver):
"""Converts a debian version string to dot-separated list"""
for sep in ['+', '-']:
if ver.rfind(sep) != -1:
ver = ver[0:ver.rfind(sep)]
return ver.split('.')
def major_change(version_old, version_new):
"""Checks if two version describe a major version change"""
try:
vold = normalize_version(version_old)
vnew = normalize_version(version_new)
if len(vold) != len(vnew):
# allow upgrade like 1.2.3 -> 1.2.3.9
if len(vold) == len(vnew)-1 and vold == vnew[:-1]:
return False
# allow upgrade like 1.2.3 -> 1.2.4.9
if len(vold) >= 3 and len(vold) == len(vnew)-1 and \
vold[:-1] == vnew[:-2]:
return False
return True
else:
# disallow 1.2.3 => 1.3.0
if vold[:-1] != vnew[:-1]:
return True
return False
except Exception:
print "Wrong versions", version_old, version_new
raise
def main():
"""Entry"""
cache = apt.Cache()
cache.upgrade(True)
upgradable = cache.get_changes()
major_upgrades = [p for p in upgradable if
p.is_installed and
major_change(p.installed.version, p.candidate.version)]
print "Packages with major version changes:", \
len(major_upgrades), "of", len(upgradable)
for package in major_upgrades:
print " " + package.fullname + " (" + package.installed.version + \
" => " + package.candidate.version + ")"
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment