]> git.decadent.org.uk Git - dak.git/blob - dak/auto_decruft.py
auto-decruft: Remember to commit database changes
[dak.git] / dak / auto_decruft.py
1 #!/usr/bin/env python
2
3 """
4 Check for obsolete binary packages
5
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
11 """
12
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.
17
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.
22
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
26
27 ################################################################################
28
29 # | priviledged positions? What privilege? The honour of working harder
30 # | than most people for absolutely no recognition?
31 #
32 # Manoj Srivastava <srivasta@debian.org> in <87lln8aqfm.fsf@glaurung.internal.golden-gryphon.com>
33
34 ################################################################################
35
36 import sys
37 import apt_pkg
38 from itertools import chain, product
39 from collections import defaultdict
40
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
46
47 ################################################################################
48
49
50 def usage(exit_code=0):
51     print """Usage: dak auto-decruft
52 Automatic removal of common kinds of cruft
53
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
59                             unstable)
60   --if-newer-version-in-rm-msg RMMSG
61                             use RMMSG in the removal message (e.g. "NVIU")
62   """
63     sys.exit(exit_code)
64
65 ################################################################################
66
67
68 def compute_sourceless_groups(suite_id, session):
69     """Find binaries without a source
70
71     @type suite_id: int
72     @param suite_id: The id of the suite denoted by suite_name
73
74     @type session: SQLA Session
75     @param session: The database session in use
76     """""
77     rows = query_without_source(suite_id, session)
78     message = '[auto-cruft] no longer built from source, no reverse dependencies'
79     arch = get_architecture('all', session=session)
80     arch_all_id_tuple = tuple([arch.arch_id])
81     arch_all_list = ["all"]
82     for row in rows:
83         package = row[0]
84         group_info = {
85             "name": "sourceless:%s" % package,
86             "packages": tuple([package]),
87             "architectures": arch_all_list,
88             "architecture_ids": arch_all_id_tuple,
89             "message": message,
90             "removal_request": {
91                 package: arch_all_list,
92             },
93         }
94         yield group_info
95
96
97 def compute_nbs_groups(suite_id, suite_name, session):
98     """Find binaries no longer built
99
100     @type suite_id: int
101     @param suite_id: The id of the suite denoted by suite_name
102
103     @type suite_name: string
104     @param suite_name: The name of the suite to remove from
105
106     @type session: SQLA Session
107     @param session: The database session in use
108     """""
109     rows = queryNBS(suite_id, session)
110     arch2ids = dict((a.arch_string, a.arch_id) for a in get_suite_architectures(suite_name))
111
112     for row in rows:
113         (pkg_list, arch_list, source, _) = row
114         message = '[auto-cruft] NBS (no longer built by %s, no reverse dependencies)' % source
115         removal_request = dict((pkg, arch_list) for pkg in pkg_list)
116         group_info = {
117             "name": "NBS:%s" % source,
118             "packages": tuple(sorted(pkg_list)),
119             "architectures": sorted(arch_list, cmp=utils.arch_compare_sw),
120             "architecture_ids": tuple(arch2ids[arch] for arch in arch_list),
121             "message": message,
122             "removal_request": removal_request,
123         }
124         yield group_info
125
126
127 def remove_groups(groups, suite_id, suite_name, session):
128     for group in groups:
129         message = group["message"]
130         params = {
131             "architecture_ids": group["architecture_ids"],
132             "packages": group["packages"],
133             "suite_id": suite_id
134         }
135         q = session.execute("""
136             SELECT b.package, b.version, a.arch_string, b.id
137             FROM binaries b
138                 JOIN bin_associations ba ON b.id = ba.bin
139                 JOIN architecture a ON b.architecture = a.id
140                 JOIN suite su ON ba.suite = su.id
141             WHERE a.id IN :architecture_ids AND b.package IN :packages AND su.id = :suite_id
142             """, params)
143
144         remove(session, message, [suite_name], list(q), partial=True, whoami="DAK's auto-decrufter")
145
146
147 def dedup(*args):
148     seen = set()
149     for iterable in args:
150         for value in iterable:
151             if value not in seen:
152                 seen.add(value)
153                 yield value
154
155
156 def merge_group(groupA, groupB):
157     """Merges two removal groups into one
158
159     Note that some values are taken entirely from groupA (e.g. name and message)
160
161     @type groupA: dict
162     @param groupA: A removal group
163
164     @type groupB: dict
165     @param groupB: Another removal group
166
167     @rtype: dict
168     @returns: A merged group
169     """
170     pkg_list = sorted(dedup(groupA["packages"], groupB["packages"]))
171     arch_list = sorted(dedup(groupA["architectures"], groupB["architectures"]), cmp=utils.arch_compare_sw)
172     arch_list_id = dedup(groupA["architecture_ids"], groupB["architecture_ids"])
173     removalA = groupA["removal_request"]
174     removalB = groupB["removal_request"]
175     new_removal = {}
176     for pkg in dedup(removalA, removalB):
177         listA = removalA[pkg] if pkg in removalA else []
178         listB = removalB[pkg] if pkg in removalB else []
179         new_removal[pkg] = sorted(dedup(listA, listB), cmp=utils.arch_compare_sw)
180
181     merged_group = {
182         "name": groupA["name"],
183         "packages": tuple(pkg_list),
184         "architectures": arch_list,
185         "architecture_ids": tuple(arch_list_id),
186         "message": groupA["message"],
187         "removal_request": new_removal,
188     }
189
190     return merged_group
191
192
193 def auto_decruft_suite(suite_name, suite_id, session, dryrun, debug):
194     """Run the auto-decrufter on a given suite
195
196     @type suite_name: string
197     @param suite_name: The name of the suite to remove from
198
199     @type suite_id: int
200     @param suite_id: The id of the suite denoted by suite_name
201
202     @type session: SQLA Session
203     @param session: The database session in use
204
205     @type dryrun: bool
206     @param dryrun: If True, just print the actions rather than actually doing them
207
208     @type debug: bool
209     @param debug: If True, print some extra information
210     """
211     all_architectures = [a.arch_string for a in get_suite_architectures(suite_name)]
212     pkg_arch2groups = defaultdict(set)
213     group_order = []
214     groups = {}
215     full_removal_request = []
216     group_generator = chain(
217         compute_sourceless_groups(suite_id, session),
218         compute_nbs_groups(suite_id, suite_name, session)
219     )
220     for group in group_generator:
221         group_name = group["name"]
222         if group_name not in groups:
223             pkgs = group["packages"]
224             affected_archs = group["architectures"]
225             # If we remove an arch:all package, then the breakage can occur on any
226             # of the architectures.
227             if "all" in affected_archs:
228                 affected_archs = all_architectures
229             for pkg_arch in product(pkgs, affected_archs):
230                 pkg_arch2groups[pkg_arch].add(group_name)
231             groups[group_name] = group
232             group_order.append(group_name)
233         else:
234             # This case usually happens when versions differ between architectures...
235             if debug:
236                 print "N: Merging group %s" % (group_name)
237             groups[group_name] = merge_group(groups[group_name], group)
238
239     for group_name in group_order:
240         removal_request = groups[group_name]["removal_request"]
241         full_removal_request.extend(removal_request.iteritems())
242
243     if not groups:
244         if debug:
245             print "N: Found no candidates"
246         return
247
248     if debug:
249         print "N: Considering to remove the following packages:"
250         for group_name in sorted(groups):
251             group_info = groups[group_name]
252             pkgs = group_info["packages"]
253             archs = group_info["architectures"]
254             print "N: * %s: %s [%s]" % (group_name, ", ".join(pkgs), " ".join(archs))
255
256     if debug:
257         print "N: Compiling ReverseDependencyChecker (RDC) - please hold ..."
258     rdc = ReverseDependencyChecker(session, suite_name)
259     if debug:
260         print "N: Computing initial breakage..."
261
262     breakage = rdc.check_reverse_depends(full_removal_request)
263     while breakage:
264         by_breakers = [(len(breakage[x]), x, breakage[x]) for x in breakage]
265         by_breakers.sort(reverse=True)
266         if debug:
267             print "N: - Removal would break %s (package, architecture)-pairs" % (len(breakage))
268             print "N: - full breakage:"
269             for _, breaker, broken in by_breakers:
270                 bname = "%s/%s" % breaker
271                 broken_str = ", ".join("%s/%s" % b for b in sorted(broken))
272                 print "N:    * %s => %s" % (bname, broken_str)
273
274         averted_breakage = set()
275
276         for _, package_arch, breakage in by_breakers:
277             if breakage <= averted_breakage:
278                 # We already avoided this break
279                 continue
280             guilty_groups = pkg_arch2groups[package_arch]
281
282             if not guilty_groups:
283                 utils.fubar("Cannot figure what group provided %s" % str(package_arch))
284
285             if debug:
286                 # Only output it, if it truly a new group being discarded
287                 # - a group can reach this part multiple times, if it breaks things on
288                 #   more than one architecture.  This being rather common in fact.
289                 already_discard = True
290                 if any(group_name for group_name in guilty_groups if group_name in groups):
291                     already_discard = False
292
293                 if not already_discard:
294                     avoided = sorted(breakage - averted_breakage)
295                     print "N: - skipping removal of %s (breakage: %s)" % (", ".join(sorted(guilty_groups)), str(avoided))
296
297             averted_breakage |= breakage
298             for group_name in guilty_groups:
299                 if group_name in groups:
300                     del groups[group_name]
301
302         if not groups:
303             if debug:
304                 print "N: Nothing left to remove"
305             return
306
307         if debug:
308             print "N: Now considering to remove: %s" % str(", ".join(sorted(groups.iterkeys())))
309
310         # Rebuild the removal request with the remaining groups and off
311         # we go to (not) break the world once more time
312         full_removal_request =  []
313         for group_info in groups.itervalues():
314             full_removal_request.extend(group_info["removal_request"].iteritems())
315         breakage = rdc.check_reverse_depends(full_removal_request)
316
317     if debug:
318         print "N: Removal looks good"
319
320     if dryrun:
321         print "Would remove the equivalent of:"
322         for group_name in group_order:
323             if group_name not in groups:
324                 continue
325             group_info = groups[group_name]
326             pkgs = group_info["packages"]
327             archs = group_info["architectures"]
328             message = group_info["message"]
329
330             # Embed the -R just in case someone wants to run it manually later
331             print '    dak rm -m "{message}" -s {suite} -a {architectures} -p -R -b {packages}'.format(
332                 message=message, suite=suite_name,
333                 architectures=",".join(archs), packages=" ".join(pkgs),
334             )
335
336         print
337         print "Note: The removals may be interdependent.  A non-breaking result may require the execution of all"
338         print "of the removals"
339     else:
340         remove_groups(groups.itervalues(), suite_id, suite_name, session)
341
342
343 def sources2removals(source_list, suite_id, session):
344     """Compute removals items given a list of names of source packages
345
346     @type source_list: list
347     @param source_list: A list of names of source packages
348
349     @type suite_id: int
350     @param suite_id: The id of the suite from which these sources should be removed
351
352     @type session: SQLA Session
353     @param session: The database session in use
354
355     @rtype: list
356     @return: A list of items to be removed to remove all sources and their binaries from the given suite
357     """
358     to_remove = []
359     params = {"suite_id": suite_id, "sources": tuple(source_list)}
360     q = session.execute("""
361                     SELECT s.source, s.version, 'source', s.id
362                     FROM source s
363                          JOIN src_associations sa ON sa.source = s.id
364                     WHERE sa.suite = :suite_id AND s.source IN :sources""", params)
365     to_remove.extend(q)
366     q = session.execute("""
367                     SELECT b.package, b.version, a.arch_string, b.id
368                     FROM binaries b
369                          JOIN bin_associations ba ON b.id = ba.bin
370                          JOIN architecture a ON b.architecture = a.id
371                          JOIN source s ON b.source = s.id
372                     WHERE ba.suite = :suite_id AND s.source IN :sources""", params)
373     to_remove.extend(q)
374     return to_remove
375
376
377 def decruft_newer_version_in(othersuite, suite_name, suite_id, rm_msg, session, dryrun):
378     """Compute removals items given a list of names of source packages
379
380     @type othersuite: str
381     @param othersuite: The name of the suite to compare with (e.g. "unstable" for "NVIU")
382
383     @type suite: str
384     @param suite: The name of the suite from which to do removals (e.g. "experimental" for "NVIU")
385
386     @type suite_id: int
387     @param suite_id: The id of the suite from which these sources should be removed
388
389     @type rm_msg: str
390     @param rm_msg: The removal message (or tag, e.g. "NVIU")
391
392     @type session: SQLA Session
393     @param session: The database session in use
394
395     @type dryrun: bool
396     @param dryrun: If True, just print the actions rather than actually doing them
397     """
398     nvi_list = [x[0] for x in newer_version(othersuite, suite_name, session)]
399     if nvi_list:
400         message = "[auto-cruft] %s" % rm_msg
401         if dryrun:
402             print "    dak rm -m \"%s\" -s %s %s" % (message, suite_name, " ".join(nvi_list))
403         else:
404             removals = sources2removals(nvi_list, suite_id, session)
405             remove(session, message, [suite_name], removals, whoami="DAK's auto-decrufter")
406
407 ################################################################################
408
409 def main ():
410     global Options
411     cnf = Config()
412
413     Arguments = [('h',"help","Auto-Decruft::Options::Help"),
414                  ('n',"dry-run","Auto-Decruft::Options::Dry-Run"),
415                  ('d',"debug","Auto-Decruft::Options::Debug"),
416                  ('s',"suite","Auto-Decruft::Options::Suite","HasArg"),
417                  # The "\0" seems to be the only way to disable short options.
418                  ("\0",'if-newer-version-in',"Auto-Decruft::Options::OtherSuite", "HasArg"),
419                  ("\0",'if-newer-version-in-rm-msg',"Auto-Decruft::Options::OtherSuiteRMMsg", "HasArg")]
420     for i in ["help", "Dry-Run", "Debug", "OtherSuite", "OtherSuiteRMMsg"]:
421         if not cnf.has_key("Auto-Decruft::Options::%s" % (i)):
422             cnf["Auto-Decruft::Options::%s" % (i)] = ""
423
424     cnf["Auto-Decruft::Options::Suite"] = cnf.get("Dinstall::DefaultSuite", "unstable")
425
426     apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
427
428     Options = cnf.subtree("Auto-Decruft::Options")
429     if Options["Help"]:
430         usage()
431
432     debug = False
433     dryrun = False
434     if Options["Dry-Run"]:
435         dryrun = True
436     if Options["Debug"]:
437         debug = True
438
439     if Options["OtherSuite"] and not Options["OtherSuiteRMMsg"]:
440         utils.fubar("--if-newer-version-in requires --if-newer-version-in-rm-msg")
441
442     session = DBConn().session()
443
444     suite = get_suite(Options["Suite"].lower(), session)
445     if not suite:
446         utils.fubar("Cannot find suite %s" % Options["Suite"].lower())
447
448     suite_id = suite.suite_id
449     suite_name = suite.suite_name.lower()
450
451     auto_decruft_suite(suite_name, suite_id, session, dryrun, debug)
452
453     if Options["OtherSuite"]:
454         osuite = get_suite(Options["OtherSuite"].lower(), session).suite_name
455         decruft_newer_version_in(osuite, suite_name, suite_id, Options["OtherSuiteRMMsg"], session, dryrun)
456
457     if not dryrun:
458         session.commit()
459
460 ################################################################################
461
462 if __name__ == '__main__':
463     main()