Last active
January 23, 2023 13:39
-
-
Save trvrm/fdc51a125b8f0b3cbe28d58151a862b8 to your computer and use it in GitHub Desktop.
Check for ubuntu pending updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# modified from /usr/lib/update-notifier/apt-check | |
import subprocess | |
def checkUpgrades(): | |
import apt_pkg | |
DISTRO = subprocess.check_output(["lsb_release", "-c", "-s"], universal_newlines=True).strip() | |
def isSecurityUpgrade(ver): | |
" check if the given version is a security update (or masks one) " | |
security_pockets = [("Ubuntu", "%s-security" % DISTRO), | |
("gNewSense", "%s-security" % DISTRO), | |
("Debian", "%s-updates" % DISTRO)] | |
for (file, index) in ver.file_list: | |
for origin, archive in security_pockets: | |
if (file.archive == archive and file.origin == origin): | |
return True | |
return False | |
apt_pkg.init() | |
cache= apt_pkg.Cache() | |
depcache=apt_pkg.DepCache(cache) | |
depcache.upgrade(True) | |
total=0 | |
security=0 | |
for package in cache.packages: | |
if not (depcache.marked_install(package) or depcache.marked_upgrade(package)): | |
continue | |
installed_version = package.current_ver | |
candidate_version = depcache.get_candidate_ver(package) | |
if installed_version==candidate_version:continue | |
total+=1 | |
if isSecurityUpgrade(candidate_version): | |
security+=1 | |
return { | |
"total":total, | |
"security":security | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment