]> git.decadent.org.uk Git - dak.git/blob - dak/auto_decruft.py
auto-decruft: Batch check source-less cruft
[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
39 from daklib.config import Config
40 from daklib.dbconn import *
41 from daklib import utils
42 from daklib.cruft import *
43 from daklib.rm import remove, ReverseDependencyChecker
44
45 ################################################################################
46
47 def usage(exit_code=0):
48     print """Usage: dak cruft-report
49 Check for obsolete or duplicated packages.
50
51   -h, --help                show this help and exit.
52   -n, --dry-run             don't do anything, just show what would have been done
53   -s, --suite=SUITE         check suite SUITE."""
54     sys.exit(exit_code)
55
56 ################################################################################
57
58 def remove_sourceless_cruft(suite_name, suite_id, session, dryrun, debug):
59     """Remove binaries without a source
60
61     @type suite_name: string
62     @param suite_name: The name of the suite to remove from
63
64     @type suite_id: int
65     @param suite_id: The id of the suite donated by suite_name
66
67     @type session: SQLA Session
68     @param session: The database session in use
69
70     @type dryrun: bool
71     @param dryrun: If True, just print the actions rather than actually doing them
72
73     @type debug: bool
74     @param debug: If True, print some extra information
75     """""
76     global Options
77     rows = query_without_source(suite_id, session)
78     arch_all_id = get_architecture('all', session=session)
79     discarded_removal = set()
80
81     message = '[auto-cruft] no longer built from source, no reverse dependencies'
82     all_packages = dict((row[0], None) for row in rows)
83     if not all_packages:
84         if debug:
85             print "N: Found no candidates"
86         return
87     if debug:
88         print "N: Considering to remove %s" % str(sorted(all_packages.iterkeys()))
89     if debug:
90         print "N: Compiling ReverseDependencyChecker (RDC) - please hold ..."
91
92     rdc = ReverseDependencyChecker(session, suite_name)
93     if debug:
94         print "N: Computing initial breakage..."
95     breakage = rdc.check_reverse_depends(all_packages)
96     while breakage:
97         by_breakers = [(len(breakage[x]), x, breakage[x]) for x in breakage]
98         by_breakers.sort(reverse=True)
99         if debug:
100             print "N: - Removal would break %s (package, architecture)-pairs" % (len(breakage))
101             print "N: - full breakage:"
102             for _, breaker, broken in by_breakers:
103                 bname = "%s/%s" % breaker
104                 broken_str = ", ".join("%s/%s" % b for b in sorted(broken))
105                 print "N:    * %s => %s" % (bname, broken_str)
106
107         _, worst_package_arch, worst_breakage = by_breakers.pop(0)
108         averted_breakage = set(worst_breakage)
109         del all_packages[worst_package_arch[0]]
110         discarded_removal.add(worst_package_arch[0])
111         if debug:
112             print "N: - skip removal of %s (due to %s)" % (worst_package_arch[0], sorted(averted_breakage))
113         for _, package_arch, breakage in by_breakers:
114             package = package_arch[0]
115             if breakage <= averted_breakage:
116                 # We already avoided this break
117                 continue
118             if package in discarded_removal:
119                 averted_breakage |= breakage
120                 continue
121             if debug:
122                 print "N: - skip removal of %s (due to %s)" % (
123                     package, str(sorted(breakage - averted_breakage)))
124             discarded_removal.add(package)
125             averted_breakage |= breakage
126             del all_packages[package]
127
128         if not all_packages:
129             if debug:
130                 print "N: Nothing left to remove"
131             return
132
133         if debug:
134             print "N: Now considering to remove %s" % str(sorted(all_packages.iterkeys()))
135         breakage = rdc.check_reverse_depends(all_packages)
136
137     if debug:
138         print "N: Removal looks good"
139
140     if dryrun:
141         # Embed the -R just in case someone wants to run it manually later
142         print 'Would do:    dak rm -m "%s" -s %s -a all -p -R -b %s' % \
143               (message, suite_name, " ".join(sorted(all_packages)))
144     else:
145         params = {
146             arch_all_id: arch_all_id,
147             all_packages: tuple(all_packages),
148             suite_id: suite_id
149         }
150         q = session.execute("""
151         SELECT b.package, b.version, a.arch_string, b.id
152         FROM binaries b
153             JOIN bin_associations ba ON b.id = ba.bin
154             JOIN architecture a ON b.architecture = a.id
155             JOIN suite su ON ba.suite = su.id
156         WHERE a.id = :arch_all_id AND b.package IN :all_packages AND su.id = :suite_id
157         """, params)
158         remove(session, message, [suite_name], list(q), partial=True, whoami="DAK's auto-decrufter")
159
160
161
162
163
164 def removeNBS(suite_name, suite_id, session, dryrun):
165     """Remove binaries no longer built
166
167     @type suite_name: string
168     @param suite_name: The name of the suite to remove from
169
170     @type suite_id: int
171     @param suite_id: The id of the suite donated by suite_name
172
173     @type session: SQLA Session
174     @param session: The database session in use
175
176     @type dryrun: bool
177     @param dryrun: If True, just print the actions rather than actually doing them
178     """""
179     global Options
180     rows = queryNBS(suite_id, session)
181     arch2ids = {}
182     for row in rows:
183         (pkg_list, arch_list, source, _) = row
184         if utils.check_reverse_depends(pkg_list, suite_name, arch_list, session, cruft=True, quiet=True):
185             continue
186         arch_string = ','.join(arch_list)
187         message = '[auto-cruft] NBS (no longer built by %s, no reverse dependencies)' % source
188
189         if dryrun:
190             # Embed the -R just in case someone wants to run it manually later
191             pkg_string = ' '.join(pkg_list)
192             print 'Would do:    dak rm -m "%s" -s %s -a %s -p -R -b %s' % \
193                   (message, suite_name, arch_string, pkg_string)
194         else:
195             for architecture in arch_list:
196                 if architecture in arch2ids:
197                     arch2ids[architecture] = utils.get_architecture(architecture, session=session)
198             arch_ids = tuple(arch2ids[architecture] for architecture in arch_list)
199             params = {
200                 suite_id: suite_id,
201                 arch_ids: arch2ids,
202                 pkg_list: tuple(pkg_list),
203             }
204             q = session.execute("""
205             SELECT b.package, b.version, a.arch_string, b.id
206             FROM binaries b
207                 JOIN bin_associations ba ON b.id = ba.bin
208                 JOIN architecture a ON b.architecture = a.id
209                 JOIN suite su ON ba.suite = su.id
210             WHERE a.id IN :arch_ids AND b.package IN :pkg_db_set AND su.id = :suite_id
211             """, params)
212             remove(session, message, [suite_name], list(q), partial=True, whoami="DAK's auto-decrufter")
213
214 ################################################################################
215
216 def main ():
217     global Options
218     cnf = Config()
219
220     Arguments = [('h',"help","Auto-Decruft::Options::Help"),
221                  ('n',"dry-run","Auto-Decruft::Options::Dry-Run"),
222                  ('d',"debug","Auto-Decruft::Options::Debug"),
223                  ('s',"suite","Auto-Decruft::Options::Suite","HasArg")]
224     for i in ["help", "Dry-Run", "Debug"]:
225         if not cnf.has_key("Auto-Decruft::Options::%s" % (i)):
226             cnf["Auto-Decruft::Options::%s" % (i)] = ""
227
228     cnf["Auto-Decruft::Options::Suite"] = cnf.get("Dinstall::DefaultSuite", "unstable")
229
230     apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
231
232     Options = cnf.subtree("Auto-Decruft::Options")
233     if Options["Help"]:
234         usage()
235
236     debug = False
237     dryrun = False
238     if Options["Dry-Run"]:
239         dryrun = True
240     if Options["Debug"]:
241         debug = True
242
243     session = DBConn().session()
244
245     suite = get_suite(Options["Suite"].lower(), session)
246     if not suite:
247         utils.fubar("Cannot find suite %s" % Options["Suite"].lower())
248
249     suite_id = suite.suite_id
250     suite_name = suite.suite_name.lower()
251
252     remove_sourceless_cruft(suite_name, suite_id, session, dryrun, debug)
253     #removeNBS(suite_name, suite_id, session, dryrun)
254
255 ################################################################################
256
257 if __name__ == '__main__':
258     main()