]> git.decadent.org.uk Git - dak.git/blob - dak/auto_decruft.py
Add dak auto-decruft command based on cruft-report
[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
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):
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 to print the actions rather than actually doing them
72     """""
73     global Options
74     rows = query_without_source(suite_id, session)
75     arch_all_id = get_architecture('all', session=session)
76
77
78     message = '[auto-cruft] no longer built from source and no reverse dependencies'
79     for row in rows:
80         package = row[0]
81         if utils.check_reverse_depends([package], suite_name, [], session, True):
82             continue
83
84         if dryrun:
85             # Embed the -R just in case someone wants to run it manually later
86             print 'Would do:    dak rm -m "%s" -s %s -a all -p -R -b %s' % \
87                   (message, suite_name, package)
88         else:
89             q = session.execute("""
90             SELECT b.package, b.version, a.arch_string, b.id
91             FROM binaries b
92                 JOIN bin_associations ba ON b.id = ba.bin
93                 JOIN architecture a ON b.architecture = a.id
94                 JOIN suite su ON ba.suite = su.id
95             WHERE a.id = :arch_all_id AND b.package = :package AND su.id = :suite_id
96             """, {arch_all_id: arch_all_id, package: package, suite_id: suite_id})
97             remove(session, message, [suite_name], list(q), partial=True, whoami="DAK's auto-decrufter")
98
99
100 def removeNBS(suite_name, suite_id, session, dryrun):
101     """Remove binaries no longer built
102
103     @type suite_name: string
104     @param suite_name: The name of the suite to remove from
105
106     @type suite_id: int
107     @param suite_id: The id of the suite donated by suite_name
108
109     @type session: SQLA Session
110     @param session: The database session in use
111
112     @type dryrun: bool
113     @param dryrun: If True, just to print the actions rather than actually doing them
114     """""
115     global Options
116     rows = queryNBS(suite_id, session)
117     arch2ids = {}
118     for row in rows:
119         (pkg_list, arch_list, source, _) = row
120         if utils.check_reverse_depends(pkg_list, suite_name, arch_list, session, True):
121             continue
122         arch_string = ','.join(arch_list)
123         message = '[auto-cruft] NBS (no longer built by %s and had no reverse dependencies)' % source
124
125         if dryrun:
126             # Embed the -R just in case someone wants to run it manually later
127             pkg_string = ' '.join(pkg_list)
128             print 'Would do:    dak rm -m "%s" -s %s -a %s -p -R -b %s' % \
129                   (message, suite_name, arch_string, pkg_string)
130         else:
131             for architecture in arch_list:
132                 if architecture in arch2ids:
133                     arch2ids[architecture] = utils.get_architecture(architecture, session=session)
134             arch_ids = ", ".join(arch2ids[architecture] for architecture in arch_list)
135             pkg_db_set = ", ".join('"%s"' % package for package in pkg_list)
136             # TODO: Fix this properly to remove the remaining non-bind arguments
137             q = session.execute("""
138             SELECT b.package, b.version, a.arch_string, b.id
139             FROM binaries b
140                 JOIN bin_associations ba ON b.id = ba.bin
141                 JOIN architecture a ON b.architecture = a.id
142                 JOIN suite su ON ba.suite = su.id
143             WHERE a.id IN (%s) AND b.package IN (%s) AND su.id = :suite_id
144             """ % (arch_ids, pkg_db_set), { suite_id: suite_id})
145             remove(session, message, [suite_name], list(q), partial=True, whoami="DAK's auto-decrufter")
146
147 ################################################################################
148
149 def main ():
150     global Options
151     cnf = Config()
152
153     Arguments = [('h',"help","Auto-Decruft::Options::Help"),
154                  ('n',"dry-run","Auto-Decruft::Options::Dry-Run"),
155                  ('s',"suite","Auto-Decruft::Options::Suite","HasArg")]
156     for i in ["help", "Dry-Run"]:
157         if not cnf.has_key("Auto-Decruft::Options::%s" % (i)):
158             cnf["Auto-Decruft::Options::%s" % (i)] = ""
159
160     cnf["Auto-Decruft::Options::Suite"] = cnf.get("Dinstall::DefaultSuite", "unstable")
161
162     apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
163
164     Options = cnf.subtree("Auto-Decruft::Options")
165     if Options["Help"]:
166         usage()
167
168     dryrun = False
169     if Options["Dry-Run"]:
170         dryrun = True
171
172     session = DBConn().session()
173
174     suite = get_suite(Options["Suite"].lower(), session)
175     if not suite:
176         utils.fubar("Cannot find suite %s" % Options["Suite"].lower())
177
178     suite_id = suite.suite_id
179     suite_name = suite.suite_name.lower()
180
181     remove_sourceless_cruft(suite_name, suite_id, session, dryrun)
182     removeNBS(suite_name, suite_id, session, dryrun)
183
184 ################################################################################
185
186 if __name__ == '__main__':
187     main()