Coverage for dak/manage_debug_suites.py: 79%

48 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2026-01-04 16:18 +0000

1#! /usr/bin/env python3 

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 

26import sys 

27 

28import apt_pkg 

29from sqlalchemy import sql 

30 

31from daklib import daklog 

32from daklib.archive import ArchiveTransaction 

33from daklib.config import Config 

34from daklib.dbconn import Suite 

35 

36################################################################################ 

37 

38Options: apt_pkg.Configuration 

39Logger: daklog.Logger 

40 

41################################################################################ 

42 

43 

44def usage(exit_code=0): 

45 print( 

46 """Usage: dak manage-debug-suites [-a|--all|<suite>...] 

47Manage the contents of one or more debug suites 

48 

49 -a, --all run on all known debug suites 

50 -n, --no-action don't do anything 

51 -h, --help show this help and exit""" 

52 ) 

53 

54 sys.exit(exit_code) 

55 

56 

57################################################################################ 

58 

59 

60def clean(debug_suite, transaction): 

61 session = transaction.session 

62 

63 # Sanity check: make sure this is a debug suite or we would remove everything 

64 any_suite = session.query(Suite).filter_by(debug_suite=debug_suite).first() 

65 if any_suite is None: 65 ↛ 66line 65 didn't jump to line 66 because the condition on line 65 was never true

66 raise Exception( 

67 "Suite '{0}' is not a debug suite".format(debug_suite.suite_name) 

68 ) 

69 

70 # Only keep source packages that are still a base suite. 

71 # All other sources and their binary packages can go. 

72 query = """ 

73 WITH 

74 sources_to_keep AS 

75 (SELECT DISTINCT sa.source 

76 FROM src_associations sa 

77 JOIN suite ON sa.suite = suite.id 

78 WHERE suite.debugsuite_id = :debugsuite_id), 

79 sources_removed AS 

80 (DELETE FROM src_associations sa 

81 WHERE sa.suite = :debugsuite_id 

82 AND sa.source NOT IN (SELECT source FROM sources_to_keep) 

83 RETURNING sa.source) 

84 DELETE FROM bin_associations ba 

85 USING binaries b 

86 WHERE ba.suite = :debugsuite_id 

87 AND ba.bin = b.id 

88 AND b.source NOT IN (SELECT source FROM sources_to_keep) 

89 RETURNING 

90 b.package, 

91 b.version, 

92 (SELECT arch_string FROM architecture WHERE id=b.architecture) AS architecture 

93 """ 

94 result = session.execute(sql.text(query), {"debugsuite_id": debug_suite.suite_id}) 

95 for row in result: 95 ↛ 96line 95 didn't jump to line 96 because the loop on line 95 never started

96 Logger.log(["remove", debug_suite.suite_name, row[0], row[1], row[2]]) 

97 

98 

99def main(): 

100 global Options, Logger 

101 

102 cnf = Config() 

103 

104 for i in ["Help", "No-Action", "All"]: 

105 key = "Manage-Debug-Suites::Options::%s" % i 

106 if key not in cnf: 106 ↛ 104line 106 didn't jump to line 104 because the condition on line 106 was always true

107 cnf[key] = "" 

108 

109 Arguments = [ 

110 ("h", "help", "Manage-Debug-Suites::Options::Help"), 

111 ("n", "no-action", "Manage-Debug-Suites::Options::No-Action"), 

112 ("a", "all", "Manage-Debug-Suites::Options::All"), 

113 ] 

114 

115 debug_suite_names = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv) # type: ignore[attr-defined] 

116 Options = cnf.subtree("Manage-Debug-Suites::Options") 

117 

118 if Options["Help"]: 

119 usage() 

120 

121 Logger = daklog.Logger("manage-debug-suites", Options["No-Action"]) 

122 

123 with ArchiveTransaction() as transaction: 

124 session = transaction.session 

125 if Options["All"]: 125 ↛ 126line 125 didn't jump to line 126 because the condition on line 125 was never true

126 if len(debug_suite_names) != 0: 

127 print("E: Cannot use both -a and a queue name") 

128 sys.exit(1) 

129 raise Exception("Not yet implemented.") 

130 else: 

131 debug_suites = session.query(Suite).filter( 

132 Suite.suite_name.in_(debug_suite_names) 

133 | Suite.codename.in_(debug_suite_names) 

134 ) 

135 

136 for debug_suite in debug_suites: 

137 Logger.log(["cleaning debug suite {0}".format(debug_suite.suite_name)]) 

138 clean(debug_suite, transaction) 

139 if not Options["No-Action"]: 139 ↛ 142line 139 didn't jump to line 142 because the condition on line 139 was always true

140 transaction.commit() 

141 else: 

142 transaction.rollback() 

143 

144 Logger.close() 

145 

146 

147####################################################################################### 

148 

149 

150if __name__ == "__main__": 

151 main()