~mvo/update-manager/not-automatic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# UpdateList.py 
#  
#  Copyright (c) 2004-2008 Canonical
#  
#  Author: Michael Vogt <mvo@debian.org>
# 
#  This program is free software; you can redistribute it and/or 
#  modify it under the terms of the GNU General Public License as 
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

from gettext import gettext as _
from gettext import ngettext
import os
import sys

class UpdateList(object):
  """
  class that contains the list of available updates in 
  self.pkgs[origin] where origin is the user readable string
  """

  class UpdateOrigin:
    def __init__(self, desc, importance):
      self.packages = []
      self.importance = importance
      self.description = desc

  def __init__(self, parent):
    # a map of packages under their origin
    try:
        pipe = os.popen("lsb_release -c -s")
        dist = pipe.read().strip()
        del pipe
    except Exception, e:
        print "Error in lsb_release: %s" % e
        parent.error(_("Failed to detect distribution"),
                     _("A error '%s' occurred while checking what system "
                       "you are using.") % e)
        sys.exit(1)
    self.distUpgradeWouldDelete = 0
    self.pkgs = {}
    self.num_updates = 0
    self.matcher = self.initMatcher(dist)
    
  def initMatcher(self, dist):
      # (origin, archive, description, importance)
      matcher_templates = [
          ("%s-security" % dist, "Ubuntu", _("Important security updates"),10),
          ("%s-updates" % dist, "Ubuntu", _("Recommended updates"), 9),
          ("%s-proposed" % dist, "Ubuntu", _("Proposed updates"), 8),
          ("%s-backports" % dist, "Ubuntu", _("Backports"), 7),
          (dist, "Ubuntu", _("Distribution updates"), 6)
      ]
      matcher = {}
      for (origin, archive, desc, importance) in matcher_templates:
          matcher[(origin, archive)] = self.UpdateOrigin(desc, importance)
      matcher[(None,None)] = self.UpdateOrigin(_("Other updates"), -1)
      return matcher

  def update(self, cache):
    self.held_back = []

    # do the upgrade
    self.distUpgradeWouldDelete = cache.saveDistUpgrade()

    dselect_upgrade_origin = self.UpdateOrigin(_("Previous selected"), 1)

    # sort by origin
    for pkg in cache:
      if pkg.isUpgradable or pkg.markedInstall:
        if pkg.candidateOrigin == None:
            # can happen for e.g. locked packages
            # FIXME: do something more sensible here (but what?)
            print "WARNING: upgradable but no canidateOrigin?!?: ", pkg.name
            continue
        # check where the package belongs
        origin_node = cache.matchPackageOrigin(pkg, self.matcher)
        if not self.pkgs.has_key(origin_node):
          self.pkgs[origin_node] = []
        self.pkgs[origin_node].append(pkg)
        self.num_updates = self.num_updates + 1
      if pkg.isUpgradable and not (pkg.markedUpgrade or pkg.markedInstall):
          self.held_back.append(pkg.name)
    for l in self.pkgs.keys():
      self.pkgs[l].sort(lambda x,y: cmp(x.name,y.name))
    self.keepcount = cache._depcache.KeepCount