1#! /usr/bin/env python3
3""" Manage debug suites
5@contact: Debian FTP Master <ftpmaster@debian.org>
6@copyright: 2015, Ansgar Burchardt <ansgar@debian.org>
8"""
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.
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.
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
24################################################################################
26import apt_pkg
27import sys
29from daklib import daklog
30from daklib.archive import ArchiveTransaction
31from daklib.dbconn import *
32from daklib.config import Config
34################################################################################
36Options = None
37Logger = None
39################################################################################
42def usage(exit_code=0):
43 print("""Usage: dak manage-debug-suites [-a|--all|<suite>...]
44Manage the contents of one or more debug suites
46 -a, --all run on all known debug suites
47 -n, --no-action don't do anything
48 -h, --help show this help and exit""")
50 sys.exit(exit_code)
52################################################################################
55def clean(debug_suite, transaction):
56 session = transaction.session
58 # Sanity check: make sure this is a debug suite or we would remove everything
59 any_suite = session.query(Suite).filter_by(debug_suite=debug_suite).first()
60 if any_suite is None: 60 ↛ 61line 60 didn't jump to line 61, because the condition on line 60 was never true
61 raise Exception("Suite '{0}' is not a debug suite".format(debug_suite.suite_name))
63 # Only keep source packages that are still a base suite.
64 # All other sources and their binary packages can go.
65 query = """
66 WITH
67 sources_to_keep AS
68 (SELECT DISTINCT sa.source
69 FROM src_associations sa
70 JOIN suite ON sa.suite = suite.id
71 WHERE suite.debugsuite_id = :debugsuite_id),
72 sources_removed AS
73 (DELETE FROM src_associations sa
74 WHERE sa.suite = :debugsuite_id
75 AND sa.source NOT IN (SELECT source FROM sources_to_keep)
76 RETURNING sa.source)
77 DELETE FROM bin_associations ba
78 USING binaries b
79 WHERE ba.suite = :debugsuite_id
80 AND ba.bin = b.id
81 AND b.source NOT IN (SELECT source FROM sources_to_keep)
82 RETURNING
83 b.package,
84 b.version,
85 (SELECT arch_string FROM architecture WHERE id=b.architecture) AS architecture
86 """
87 result = session.execute(query, {"debugsuite_id": debug_suite.suite_id})
88 for row in result: 88 ↛ 89line 88 didn't jump to line 89, because the loop on line 88 never started
89 Logger.log(["remove", debug_suite.suite_name, row[0], row[1], row[2]])
92def main():
93 global Options, Logger
95 cnf = Config()
97 for i in ["Help", "No-Action", "All"]:
98 key = "Manage-Debug-Suites::Options::%s" % i
99 if key not in cnf: 99 ↛ 97line 99 didn't jump to line 97, because the condition on line 99 was never false
100 cnf[key] = ""
102 Arguments = [('h', "help", "Manage-Debug-Suites::Options::Help"),
103 ('n', "no-action", "Manage-Debug-Suites::Options::No-Action"),
104 ('a', "all", "Manage-Debug-Suites::Options::All")]
106 debug_suite_names = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
107 Options = cnf.subtree("Manage-Debug-Suites::Options")
109 if Options["Help"]:
110 usage()
112 Logger = daklog.Logger('manage-debug-suites', Options['No-Action'])
114 with ArchiveTransaction() as transaction:
115 session = transaction.session
116 if Options['All']: 116 ↛ 117line 116 didn't jump to line 117, because the condition on line 116 was never true
117 if len(debug_suite_names) != 0:
118 print("E: Cannot use both -a and a queue name")
119 sys.exit(1)
120 raise Exception("Not yet implemented.")
121 else:
122 debug_suites = session.query(Suite).filter(Suite.suite_name.in_(debug_suite_names))
124 for debug_suite in debug_suites:
125 Logger.log(['cleaning debug suite {0}'.format(debug_suite.suite_name)])
126 clean(debug_suite, transaction)
127 if not Options['No-Action']: 127 ↛ 130line 127 didn't jump to line 130, because the condition on line 127 was never false
128 transaction.commit()
129 else:
130 transaction.rollback()
132 Logger.close()
134#######################################################################################
137if __name__ == '__main__':
138 main()