4
#nice apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst
9
from optparse import OptionParser
13
SYNAPTIC_PINFILE = "/var/lib/synaptic/preferences"
14
DISTRO = subprocess.Popen(["lsb_release","-c","-s"],
15
stdout=subprocess.PIPE).communicate()[0].strip()
17
class OpNullProgress(object):
18
def update(self, percent):
24
return gettext.dgettext("update-notifier", msg)
26
def _handleException(type, value, tb):
27
sys.stderr.write("E: "+ _("Unknown Error: '%s' (%s)") % (type,value))
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)
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:
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)]
50
for (file, index) in ver.file_list:
51
for origin, archive in security_pockets:
52
if (file.archive == archive and file.origin == origin):
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),
61
outstream.write("\n".join(map(lambda p: p.name, pkgs)))
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.",
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)
76
" init the system, be nice "
77
# FIXME: do a ionice here too?
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","")
84
def run(options=None):
86
# we are run in "are security updates installed automatically?"
88
if options.security_updates_unattended:
89
res = apt_pkg.config.find_i("APT::Periodic::Unattended-Upgrade", 0)
95
cache = apt_pkg.Cache(OpNullProgress())
96
except SystemError, e:
97
sys.stderr.write("E: "+ _("Error: Opening the cache (%s)") % e)
99
depcache = apt_pkg.DepCache(cache)
102
depcache.read_pinfile()
103
# read the synaptic pins too
104
if os.path.exists(SYNAPTIC_PINFILE):
105
depcache.read_pinfile(SYNAPTIC_PINFILE)
110
if depcache.broken_count > 0:
111
sys.stderr.write("E: "+ _("Error: BrokenCount > 0"))
114
# do the upgrade (not dist-upgrade!)
116
saveDistUpgrade(cache,depcache)
117
except SystemError, e:
118
sys.stderr.write("E: "+ _("Error: Marking the upgrade (%s)") % e)
121
# analyze the ugprade
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)):
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:
135
# check for security upgrades
136
upgrades = upgrades + 1
137
if isSecurityUpgrade(cand_ver):
138
security_updates += 1
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
147
if isSecurityUpgrade(ver):
148
security_updates += 1
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)
157
# print the number of regular upgrades and the number of
159
sys.stderr.write("%s;%s" % (upgrades,security_updates))
161
# return the number of upgrades (if its used as a module)
162
return(upgrades,security_updates)
165
if __name__ == "__main__":
166
# setup a exception handler to make sure that uncaught stuff goes
168
sys.excepthook = _handleException
171
APP="update-notifier"
172
DIR="/usr/share/locale"
173
gettext.bindtextdomain(APP, DIR)
174
gettext.textdomain(APP)
177
parser = OptionParser()
178
parser.add_option("-p",
181
dest="show_package_names",
182
help=_("Show the packages that are going to be installed/upgraded"))
183
parser.add_option("",
186
dest="readable_output",
187
help=_("Show human readable output on stdout"))
188
parser.add_option("",
189
"--security-updates-unattended",
191
help=_("Return the time in days when security updates "
192
"are installed unattended (0 means disabled)"))
193
(options, args) = parser.parse_args()
4
nice apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst