~ubuntu-core-dev/update-notifier/ubuntu

« back to all changes in this revision

Viewing changes to data/apt-check

  • Committer: mvo
  • Date: 2004-11-02 00:58:06 UTC
  • Revision ID: gustavo@niemeyer.net-20041102005806-9118e041eaa0ebc8
* inital checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
 
4
 
#nice apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst 
5
 
 
6
 
import apt_pkg
7
 
import os
8
 
import sys
9
 
from optparse import OptionParser
10
 
import gettext
11
 
import subprocess
12
 
 
13
 
SYNAPTIC_PINFILE = "/var/lib/synaptic/preferences"
14
 
DISTRO = subprocess.Popen(["lsb_release","-c","-s"],
15
 
                          stdout=subprocess.PIPE).communicate()[0].strip()
16
 
 
17
 
class OpNullProgress(object):
18
 
    def update(self, percent):
19
 
        pass
20
 
    def done(self):
21
 
        pass
22
 
 
23
 
def _(msg):
24
 
    return gettext.dgettext("update-notifier", msg)
25
 
 
26
 
def _handleException(type, value, tb):
27
 
    sys.stderr.write("E: "+ _("Unknown Error: '%s' (%s)") % (type,value))
28
 
    sys.exit(-1)
29
 
 
30
 
def clean(cache,depcache):
31
 
    " unmark (clean) all changes from the given depcache "
32
 
    # mvo: looping is too inefficient with the new auto-mark code
33
 
    #for pkg in cache.Packages:
34
 
    #    depcache.MarkKeep(pkg)
35
 
    depcache.init()
36
 
 
37
 
def saveDistUpgrade(cache,depcache):
38
 
    """ this functions mimics a upgrade but will never remove anything """
39
 
    depcache.upgrade(True)
40
 
    if depcache.del_count > 0:
41
 
        clean(cache,depcache)
42
 
    depcache.upgrade()
43
 
 
44
 
def isSecurityUpgrade(ver):
45
 
    " check if the given version is a security update (or masks one) "
46
 
    security_pockets = [("Ubuntu", "%s-security" % DISTRO),
47
 
                        ("gNewSense", "%s-security" % DISTRO),
48
 
                        ("Debian", "%s-updates" % DISTRO)]
49
 
 
50
 
    for (file, index) in ver.file_list:
51
 
        for origin, archive in security_pockets:
52
 
            if (file.archive == archive and file.origin == origin):
53
 
                return True
54
 
    return False
55
 
 
56
 
def write_package_names(outstream, cache, depcache):
57
 
    " write out package names that change to outstream "
58
 
    pkgs = filter(lambda pkg:
59
 
                  depcache.marked_install(pkg) or depcache.marked_upgrade(pkg),
60
 
                  cache.packages)
61
 
    outstream.write("\n".join(map(lambda p: p.name, pkgs)))
62
 
 
63
 
def write_human_readable_summary(outstream, upgrades, security_updates):
64
 
    " write out human summary summary to outstream "
65
 
    outstream.write(gettext.dngettext("update-notifier",
66
 
                            "%i package can be updated.",
67
 
                            "%i packages can be updated.",
68
 
                            upgrades) % upgrades)
69
 
    outstream.write("\n")
70
 
    outstream.write(gettext.dngettext("update-notifier",
71
 
                            "%i update is a security update.",
72
 
                            "%i updates are security updates.",
73
 
                            security_updates)  % security_updates)
74
 
    outstream.write("\n")
75
 
def init():
76
 
    " init the system, be nice "
77
 
    # FIXME: do a ionice here too?
78
 
    os.nice(19)
79
 
    apt_pkg.init()
80
 
    # force apt to build its caches in memory for now to make sure
81
 
    # that there is no race when the pkgcache file gets re-generated
82
 
    apt_pkg.config.set("Dir::Cache::pkgcache","")
83
 
    
84
 
def run(options=None):
85
 
 
86
 
    # we are run in "are security updates installed automatically?"
87
 
    # question mode
88
 
    if options.security_updates_unattended:
89
 
        res = apt_pkg.config.find_i("APT::Periodic::Unattended-Upgrade", 0)
90
 
        #print res
91
 
        sys.exit(res)
92
 
 
93
 
    # get caches
94
 
    try:
95
 
        cache = apt_pkg.Cache(OpNullProgress())
96
 
    except SystemError, e:
97
 
        sys.stderr.write("E: "+ _("Error: Opening the cache (%s)") % e)
98
 
        sys.exit(-1)
99
 
    depcache = apt_pkg.DepCache(cache)
100
 
 
101
 
    # read the pin files
102
 
    depcache.read_pinfile()
103
 
    # read the synaptic pins too
104
 
    if os.path.exists(SYNAPTIC_PINFILE):
105
 
        depcache.read_pinfile(SYNAPTIC_PINFILE)
106
 
 
107
 
    # init the depcache
108
 
    depcache.init()
109
 
 
110
 
    if depcache.broken_count > 0:
111
 
        sys.stderr.write("E: "+ _("Error: BrokenCount > 0"))
112
 
        sys.exit(-1)
113
 
 
114
 
    # do the upgrade (not dist-upgrade!)
115
 
    try:
116
 
        saveDistUpgrade(cache,depcache)
117
 
    except SystemError, e:
118
 
        sys.stderr.write("E: "+ _("Error: Marking the upgrade (%s)") % e)
119
 
        sys.exit(-1)
120
 
 
121
 
    # analyze the ugprade
122
 
    upgrades = 0
123
 
    security_updates = 0
124
 
    for pkg in cache.packages:
125
 
        # skip packages that are not marked upgraded/installed
126
 
        if not (depcache.marked_install(pkg) or depcache.marked_upgrade(pkg)):
127
 
            continue
128
 
        # check if this is really a upgrade or a false positive
129
 
        # (workaround for ubuntu #7907)
130
 
        inst_ver = pkg.current_ver
131
 
        cand_ver = depcache.get_candidate_ver(pkg)
132
 
        if cand_ver == inst_ver:
133
 
            continue
134
 
 
135
 
        # check for security upgrades
136
 
        upgrades = upgrades + 1 
137
 
        if isSecurityUpgrade(cand_ver):
138
 
            security_updates += 1
139
 
            continue
140
 
 
141
 
        # now check for security updates that are masked by a 
142
 
        # canidate version from another repo (-proposed or -updates)
143
 
        for ver in pkg.version_list:
144
 
            if (inst_ver and apt_pkg.version_compare(ver.ver_str, inst_ver.ver_str) <= 0):
145
 
                #print "skipping '%s' " % ver.VerStr
146
 
                continue
147
 
            if isSecurityUpgrade(ver):
148
 
                security_updates += 1
149
 
                break
150
 
 
151
 
    # print the number of upgrades
152
 
    if options and options.show_package_names:
153
 
        write_package_names(sys.stderr, cache, depcache)
154
 
    elif options and options.readable_output:
155
 
        write_human_readable_summary(sys.stdout, upgrades, security_updates)
156
 
    else:
157
 
        # print the number of regular upgrades and the number of 
158
 
        # security upgrades
159
 
        sys.stderr.write("%s;%s" % (upgrades,security_updates))
160
 
 
161
 
    # return the number of upgrades (if its used as a module)
162
 
    return(upgrades,security_updates)
163
 
 
164
 
 
165
 
if __name__ == "__main__":        
166
 
    # setup a exception handler to make sure that uncaught stuff goes
167
 
    # to the notifier
168
 
    sys.excepthook = _handleException
169
 
    
170
 
    # gettext
171
 
    APP="update-notifier"
172
 
    DIR="/usr/share/locale"
173
 
    gettext.bindtextdomain(APP, DIR)
174
 
    gettext.textdomain(APP)
175
 
 
176
 
    # check arguments
177
 
    parser = OptionParser()
178
 
    parser.add_option("-p",
179
 
                      "--package-names",
180
 
                      action="store_true",
181
 
                      dest="show_package_names",
182
 
                      help=_("Show the packages that are going to be installed/upgraded"))
183
 
    parser.add_option("",
184
 
                      "--human-readable",
185
 
                      action="store_true",
186
 
                      dest="readable_output",
187
 
                      help=_("Show human readable output on stdout"))
188
 
    parser.add_option("",
189
 
                      "--security-updates-unattended",
190
 
                      action="store_true",
191
 
                      help=_("Return the time in days when security updates "
192
 
                             "are installed unattended (0 means disabled)"))
193
 
    (options, args) = parser.parse_args()
194
 
 
195
 
    # run it
196
 
    init()
197
 
    run(options)
 
1
#!/bin/sh
 
2
 
 
3
 
 
4
nice apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst