]> git.decadent.org.uk Git - dak.git/blob - dak/manage_debug_suites.py
Add program to remove old packages from debug suites
[dak.git] / dak / manage_debug_suites.py
1 #!/usr/bin/env python
2
3 """ Manage debug suites
4
5 @contact: Debian FTP Master <ftpmaster@debian.org>
6 @copyright: 2015, Ansgar Burchardt <ansgar@debian.org>
7
8 """
9
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24 ################################################################################
25
26 import apt_pkg
27 import sys
28
29 from daklib import daklog
30 from daklib.archive import ArchiveTransaction
31 from daklib.dbconn import *
32 from daklib.config import Config
33
34 ################################################################################
35
36 Options = None
37 Logger = None
38
39 ################################################################################
40
41 def usage (exit_code=0):
42     print """Usage: dak manage-debug-suites [-a|--all|<suite>...]
43 Manage the contents of one or more debug suites
44
45   -a, --all                  run on all known debug suites
46   -n, --no-action            don't do anything
47   -h, --help                 show this help and exit"""
48
49     sys.exit(exit_code)
50
51 ################################################################################
52
53 def clean(debug_suite, transaction):
54     session = transaction.session
55
56     # Sanity check: make sure this is a debug suite or we would remove everything
57     any_suite = session.query(Suite).filter_by(debug_suite=debug_suite).first()
58     if any_suite is None:
59         raise Exception("Suite '{0}' is not a debug suite".format(debug_suite.suite_name))
60
61     # Only keep source packages that are still a base suite.
62     # All other sources and their binary packages can go.
63     query = """
64     WITH
65     sources_to_keep AS
66       (SELECT DISTINCT sa.source
67          FROM src_associations sa
68          JOIN suite ON sa.suite = suite.id
69         WHERE suite.debugsuite_id = :debugsuite_id),
70     sources_removed AS
71       (DELETE FROM src_associations sa
72         WHERE sa.suite = :debugsuite_id
73           AND sa.source NOT IN (SELECT source FROM sources_to_keep)
74        RETURNING sa.source)
75     DELETE FROM bin_associations ba
76      USING binaries b
77      WHERE ba.suite = :debugsuite_id
78        AND ba.bin = b.id
79        AND b.source IN (SELECT source FROM sources_removed)
80     RETURNING
81       b.package,
82       b.version,
83       (SELECT arch_string FROM architecture WHERE id=b.architecture) AS architecture
84     """
85     result = session.execute(query, {"debugsuite_id": debug_suite.suite_id})
86     for row in result:
87         Logger.log(["remove", debug_suite.suite_name, row[0], row[1], row[2]])
88
89 def main ():
90     global Options, Logger
91
92     cnf = Config()
93
94     for i in ["Help", "No-Action", "All"]:
95         if not cnf.has_key("Manage-Debug-Suites::Options::%s" % (i)):
96             cnf["Manage-Debug-Suites::Options::%s" % (i)] = ""
97
98     Arguments = [('h',"help","Manage-Debug-Suites::Options::Help"),
99                  ('n',"no-action","Manage-Debug-Suites::Options::No-Action"),
100                  ('a',"all","Manage-Debug-Suites::Options::All")]
101
102     debug_suite_names = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
103     Options = cnf.subtree("Manage-Debug-Suites::Options")
104
105     if Options["Help"]:
106         usage()
107
108     Logger = daklog.Logger('manage-debug-suites', Options['No-Action'])
109
110     with ArchiveTransaction() as transaction:
111         session = transaction.session
112         if Options['All']:
113             if len(debug_suite_names) != 0:
114                 print "E: Cannot use both -a and a queue name"
115                 sys.exit(1)
116             raise Exception("Not yet implemented.")
117         else:
118             debug_suites = session.query(Suite).filter(Suite.suite_name.in_(debug_suite_names))
119
120         for debug_suite in debug_suites:
121             Logger.log(['cleaning debug suite {0}'.format(debug_suite.suite_name)])
122             clean(debug_suite, transaction)
123         if not Options['No-Action']:
124             transaction.commit()
125         else:
126             transaction.rollback()
127
128     Logger.close()
129
130 #######################################################################################
131
132 if __name__ == '__main__':
133     main()