NoPaste

20-updates

von hec_tech

Dein Code:
  1. #!/usr/bin/python
  2. #
  3. #   20-updates - create the system updates section of the MOTD
  4. #   Copyright (c) 2013 Nick Charlton
  5. #
  6. #   Authors: Nick Charlton <hello@nickcharlton.net>
  7. #   Based upon prior work by Dustin Kirkland and Michael Vogt.
  8. #
  9. #   This program is free software; you can redistribute it and/or modify
  10. #   it under the terms of the GNU General Public License as published by
  11. #   the Free Software Foundation; either version 2 of the License, or
  12. #   (at your option) any later version.
  13. #
  14. #   This program is distributed in the hope that it will be useful,
  15. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. #   GNU General Public License for more details.
  18. #
  19. #   You should have received a copy of the GNU General Public License along
  20. #   with this program; if not, write to the Free Software Foundation, Inc.,
  21. #   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22.  
  23. import sys
  24. import subprocess
  25. import apt_pkg
  26.  
  27. DISTRO = subprocess.Popen(["lsb_release", "-c", "-s"],
  28.                           stdout=subprocess.PIPE).communicate()[0].strip()
  29.  
  30. class OpNullProgress(object):
  31.     '''apt progress handler which supresses any output.'''
  32.     def update(self):
  33.         pass
  34.     def done(self):
  35.         pass
  36.  
  37. def is_security_upgrade(pkg):
  38.     '''
  39.     Checks to see if a package comes from a DISTRO-security source.
  40.     '''
  41.     security_package_sources = [("Ubuntu", "%s-security" % DISTRO),
  42.                                ("Debian", "%s-security" % DISTRO)]
  43.  
  44.     for (file, index) in pkg.file_list:
  45.         for origin, archive in security_package_sources:
  46.             if (file.archive == archive and file.origin == origin):
  47.                 return True
  48.     return False
  49.  
  50. # init apt and config
  51. apt_pkg.init()
  52.  
  53. # open the apt cache
  54. try:
  55.     cache = apt_pkg.Cache(OpNullProgress())
  56. except SystemError, e:
  57.     sys.stderr.write("Error: Opening the cache (%s)" % e)
  58.     sys.exit(-1)
  59.  
  60. # setup a DepCache instance to interact with the repo
  61. depcache = apt_pkg.DepCache(cache)
  62.  
  63. # take into account apt policies
  64. depcache.read_pinfile()
  65.  
  66. # initialise it
  67. depcache.init()
  68.  
  69. # give up if packages are broken
  70. if depcache.broken_count > 0:
  71.     sys.stderr.write("Error: Broken packages exist.")
  72.     sys.exit(-1)
  73.  
  74. # mark possible packages
  75. try:
  76.     # run distro-upgrade
  77.     depcache.upgrade(True)
  78.     # reset if packages get marked as deleted -> we don't want to break anything
  79.     if depcache.del_count > 0:
  80.         depcache.init()
  81.  
  82.     # then a standard upgrade
  83.     depcache.upgrade()
  84. except SystemError, e:
  85.     sys.stderr.write("Error: Couldn't mark the upgrade (%s)" % e)
  86.     sys.exit(-1)
  87.  
  88. # run around the packages
  89. upgrades = 0
  90. security_upgrades = 0
  91. for pkg in cache.packages:
  92.     candidate = depcache.get_candidate_ver(pkg)
  93.     current = pkg.current_ver
  94.  
  95.     # skip packages not marked as upgraded/installed
  96.     if not (depcache.marked_install(pkg) or depcache.marked_upgrade(pkg)):
  97.         continue
  98.  
  99.     # increment the upgrade counter
  100.     upgrades += 1
  101.  
  102.     # keep another count for security upgrades
  103.     if is_security_upgrade(candidate):
  104.         security_upgrades += 1
  105.  
  106.     # double check for security upgrades masked by another package
  107.     for version in pkg.version_list:
  108.         if (current and apt_pkg.version_compare(version.ver_str, current.ver_str) <= 0):
  109.             continue
  110.         if is_security_upgrade(version):
  111.             security_upgrades += 1
  112.             break
  113.  
  114. print "%d updates to install." % upgrades
  115. print "%d are security updates." % security_upgrades
  116. print "" # leave a trailing blank line

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. Alternativ kannst du den gesamten Eintrag auch als Datei herunterladen.