4 Check for obsolete binary packages
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2000-2006 James Troup <james@nocrew.org>
8 @copyright: 2009 Torsten Werner <twerner@debian.org>
9 @copyright: 2015 Niels Thykier <niels@thykier.net>
10 @license: GNU General Public License version 2 or later
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 ################################################################################
29 # | priviledged positions? What privilege? The honour of working harder
30 # | than most people for absolutely no recognition?
32 # Manoj Srivastava <srivasta@debian.org> in <87lln8aqfm.fsf@glaurung.internal.golden-gryphon.com>
34 ################################################################################
38 from itertools import chain, product
39 from collections import defaultdict
41 from daklib.config import Config
42 from daklib.dbconn import *
43 from daklib import utils
44 from daklib.cruft import *
45 from daklib.rm import remove, ReverseDependencyChecker
47 ################################################################################
50 def usage(exit_code=0):
51 print """Usage: dak auto-decruft
52 Automatic removal of common kinds of cruft
54 -h, --help show this help and exit.
55 -n, --dry-run don't do anything, just show what would have been done
56 -s, --suite=SUITE check suite SUITE.
57 --if-newer-version-in OS remove all packages in SUITE with a lower version than
58 in OS (e.g. -s experimental --if-newer-version-in
60 --if-newer-version-in-rm-msg RMMSG
61 use RMMSG in the removal message (e.g. "NVIU")
65 ################################################################################
68 def compute_sourceless_groups(suite_id, session):
69 """Find binaries without a source
72 @param suite_id: The id of the suite denoted by suite_name
74 @type session: SQLA Session
75 @param session: The database session in use
77 rows = query_without_source(suite_id, session)
78 message = '[auto-cruft] no longer built from source, no reverse dependencies'
79 arch_all_id_tuple = tuple([get_architecture('all', session=session)])
80 arch_all_list = ["all"]
84 "name": "sourceless:%s" % package,
85 "packages": tuple([package]),
86 "architectures": arch_all_list,
87 "architecture_ids": arch_all_id_tuple,
90 package: arch_all_list,
96 def compute_nbs_groups(suite_id, suite_name, session):
97 """Find binaries no longer built
100 @param suite_id: The id of the suite denoted by suite_name
102 @type suite_name: string
103 @param suite_name: The name of the suite to remove from
105 @type session: SQLA Session
106 @param session: The database session in use
108 rows = queryNBS(suite_id, session)
109 arch2ids = dict((a.arch_string, a.arch_id) for a in get_suite_architectures(suite_name))
112 (pkg_list, arch_list, source, _) = row
113 message = '[auto-cruft] NBS (no longer built by %s, no reverse dependencies)' % source
114 removal_request = dict((pkg, arch_list) for pkg in pkg_list)
116 "name": "NBS:%s" % source,
117 "packages": tuple(sorted(pkg_list)),
118 "architectures": sorted(arch_list, cmp=utils.arch_compare_sw),
119 "architecture_ids": tuple(arch2ids[arch] for arch in arch_list),
121 "removal_request": removal_request,
126 def remove_groups(groups, suite_id, suite_name, session):
128 message = group["message"]
130 "architecture_ids": group["architecture_ids"],
131 "packages": group["packages"],
134 q = session.execute("""
135 SELECT b.package, b.version, a.arch_string, b.id
137 JOIN bin_associations ba ON b.id = ba.bin
138 JOIN architecture a ON b.architecture = a.id
139 JOIN suite su ON ba.suite = su.id
140 WHERE a.id IN :architecture_ids AND b.package IN :packages AND su.id = :suite_id
143 remove(session, message, [suite_name], list(q), partial=True, whoami="DAK's auto-decrufter")
146 def auto_decruft_suite(suite_name, suite_id, session, dryrun, debug):
147 """Run the auto-decrufter on a given suite
149 @type suite_name: string
150 @param suite_name: The name of the suite to remove from
153 @param suite_id: The id of the suite denoted by suite_name
155 @type session: SQLA Session
156 @param session: The database session in use
159 @param dryrun: If True, just print the actions rather than actually doing them
162 @param debug: If True, print some extra information
164 all_architectures = [a.arch_string for a in get_suite_architectures(suite_name)]
165 pkg_arch2groups = defaultdict(set)
168 full_removal_request = []
169 group_generator = chain(
170 compute_sourceless_groups(suite_id, session),
171 compute_nbs_groups(suite_id, suite_name, session)
173 for group in group_generator:
174 group_name = group["name"]
175 pkgs = group["packages"]
176 affected_archs = group["architectures"]
177 removal_request = group["removal_request"]
178 # If we remove an arch:all package, then the breakage can occur on any
179 # of the architectures.
180 if "all" in affected_archs:
181 affected_archs = all_architectures
182 for pkg_arch in product(pkgs, affected_archs):
183 pkg_arch2groups[pkg_arch].add(group_name)
184 groups[group_name] = group
185 group_order.append(group_name)
187 full_removal_request.extend(removal_request.iteritems())
191 print "N: Found no candidates"
194 print "N: Considering to remove the following packages:"
195 for group_name in sorted(groups):
196 group_info = groups[group_name]
197 pkgs = group_info["packages"]
198 archs = group_info["architectures"]
199 print "N: * %s: %s [%s]" % (group_name, ", ".join(pkgs), " ".join(archs))
202 print "N: Compiling ReverseDependencyChecker (RDC) - please hold ..."
203 rdc = ReverseDependencyChecker(session, suite_name)
205 print "N: Computing initial breakage..."
207 breakage = rdc.check_reverse_depends(full_removal_request)
209 by_breakers = [(len(breakage[x]), x, breakage[x]) for x in breakage]
210 by_breakers.sort(reverse=True)
212 print "N: - Removal would break %s (package, architecture)-pairs" % (len(breakage))
213 print "N: - full breakage:"
214 for _, breaker, broken in by_breakers:
215 bname = "%s/%s" % breaker
216 broken_str = ", ".join("%s/%s" % b for b in sorted(broken))
217 print "N: * %s => %s" % (bname, broken_str)
219 averted_breakage = set()
221 for _, package_arch, breakage in by_breakers:
222 if breakage <= averted_breakage:
223 # We already avoided this break
225 guilty_groups = pkg_arch2groups[package_arch]
227 if not guilty_groups:
228 utils.fubar("Cannot figure what group provided %s" % str(package_arch))
231 # Only output it, if it truly a new group being discarded
232 # - a group can reach this part multiple times, if it breaks things on
233 # more than one architecture. This being rather common in fact.
234 already_discard = True
235 if any(group_name for group_name in guilty_groups if group_name in groups):
236 already_discard = False
238 if not already_discard:
239 avoided = sorted(breakage - averted_breakage)
240 print "N: - skipping removal of %s (breakage: %s)" % (", ".join(sorted(guilty_groups)), str(avoided))
242 averted_breakage |= breakage
243 for group_name in guilty_groups:
244 if group_name in groups:
245 del groups[group_name]
249 print "N: Nothing left to remove"
253 print "N: Now considering to remove: %s" % str(", ".join(sorted(groups.iterkeys())))
255 # Rebuild the removal request with the remaining groups and off
256 # we go to (not) break the world once more time
257 full_removal_request = []
258 for group_info in groups.itervalues():
259 full_removal_request.extend(group_info["removal_request"].iteritems())
260 breakage = rdc.check_reverse_depends(full_removal_request)
263 print "N: Removal looks good"
266 print "Would remove the equivalent of:"
267 for group_name in group_order:
268 if group_name not in groups:
270 group_info = groups[group_name]
271 pkgs = group_info["packages"]
272 archs = group_info["architectures"]
273 message = group_info["message"]
275 # Embed the -R just in case someone wants to run it manually later
276 print ' dak rm -m "{message}" -s {suite} -a {architectures} -p -R -b {packages}'.format(
277 message=message, suite=suite_name,
278 architectures=",".join(archs), packages=" ".join(pkgs),
282 print "Note: The removals may be interdependent. A non-breaking result may require the execution of all"
283 print "of the removals"
285 remove_groups(groups.itervalues(), suite_id, suite_name, session)
288 def sources2removals(source_list, suite_id, session):
289 """Compute removals items given a list of names of source packages
291 @type source_list: list
292 @param source_list: A list of names of source packages
295 @param suite_id: The id of the suite from which these sources should be removed
297 @type session: SQLA Session
298 @param session: The database session in use
301 @return: A list of items to be removed to remove all sources and their binaries from the given suite
304 params = {"suite_id": suite_id, "sources": tuple(source_list)}
305 q = session.execute("""
306 SELECT s.source, s.version, 'source', s.id
308 JOIN src_associations sa ON sa.source = s.id
309 JOIN suite su ON sa.suite = su.id
310 WHERE su.id = :suite_id AND s.source IN :sources""", params)
312 q = session.execute("""
313 SELECT b.package, b.version, a.arch_string, b.id
315 JOIN bin_associations ba ON b.id = ba.bin
316 JOIN architecture a ON b.architecture = a.id
317 JOIN suite su ON ba.suite = su.id
318 JOIN source s ON b.source = s.id
319 JOIN src_associations sa ON s.id = sa.source AND sa.suite = su.id
320 WHERE su.id = :suite_id AND s.source IN :sources""", params)
325 def decruft_newer_version_in(othersuite, suite_name, suite_id, rm_msg, session, dryrun):
326 """Compute removals items given a list of names of source packages
328 @type othersuite: str
329 @param othersuite: The name of the suite to compare with (e.g. "unstable" for "NVIU")
332 @param suite: The name of the suite from which to do removals (e.g. "experimental" for "NVIU")
335 @param suite_id: The id of the suite from which these sources should be removed
338 @param rm_msg: The removal message (or tag, e.g. "NVIU")
340 @type session: SQLA Session
341 @param session: The database session in use
344 @param dryrun: If True, just print the actions rather than actually doing them
346 nvi_list = [x[0] for x in newer_version(othersuite, suite_name, session)]
348 message = "[auto-cruft] %s" % rm_msg
350 print " dak rm -m \"%s\" -s %s %s" % (message, suite_name, " ".join(nvi_list))
352 removals = sources2removals(nvi_list, suite_id, session)
353 remove(session, message, [suite_name], removals, whoami="DAK's auto-decrufter")
355 ################################################################################
361 Arguments = [('h',"help","Auto-Decruft::Options::Help"),
362 ('n',"dry-run","Auto-Decruft::Options::Dry-Run"),
363 ('d',"debug","Auto-Decruft::Options::Debug"),
364 ('s',"suite","Auto-Decruft::Options::Suite","HasArg"),
365 ('z','if-newer-version-in',"Auto-Decruft::Options::OtherSuite", "HasArg"),
366 ('Z','if-newer-version-in-rm-msg',"Auto-Decruft::Options::OtherSuiteRMMsg", "HasArg")]
367 for i in ["help", "Dry-Run", "Debug", "OtherSuite", "OtherSuiteRMMsg"]:
368 if not cnf.has_key("Auto-Decruft::Options::%s" % (i)):
369 cnf["Auto-Decruft::Options::%s" % (i)] = ""
371 cnf["Auto-Decruft::Options::Suite"] = cnf.get("Dinstall::DefaultSuite", "unstable")
373 apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
375 Options = cnf.subtree("Auto-Decruft::Options")
381 if Options["Dry-Run"]:
386 if Options["OtherSuite"] and not Options["OtherSuiteRMMsg"]:
387 utils.fubar("--if-newer-version-in requires --if-newer-version-in-rm-msg")
389 session = DBConn().session()
391 suite = get_suite(Options["Suite"].lower(), session)
393 utils.fubar("Cannot find suite %s" % Options["Suite"].lower())
395 suite_id = suite.suite_id
396 suite_name = suite.suite_name.lower()
398 auto_decruft_suite(suite_name, suite_id, session, dryrun, debug)
400 if Options["OtherSuite"]:
401 osuite = get_suite(Options["OtherSuite"].lower(), session).suite_name
402 decruft_newer_version_in(osuite, suite_name, suite_id, Options["OtherSuiteRMMsg"], session, dryrun)
404 ################################################################################
406 if __name__ == '__main__':