Coverage for dak/manage_debug_suites.py: 79%

48 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2026-08-03 16:46 +0000

1"""Manage debug suites 

2 

3@contact: Debian FTP Master <ftpmaster@debian.org> 

4@copyright: 2015, Ansgar Burchardt <ansgar@debian.org> 

5 

6""" 

7 

8# This program is free software; you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation; either version 2 of the License, or 

11# (at your option) any later version. 

12 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17 

18# You should have received a copy of the GNU General Public License 

19# along with this program; if not, write to the Free Software 

20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

21 

22################################################################################ 

23 

24import sys 

25 

26import apt_pkg 

27from sqlalchemy import select, sql 

28 

29from daklib import daklog 

30from daklib.archive import ArchiveTransaction 

31from daklib.config import Config 

32from daklib.dbconn import Suite 

33 

34################################################################################ 

35 

36Options: apt_pkg.Configuration 

37Logger: daklog.Logger 

38 

39################################################################################ 

40 

41 

42def usage(exit_code=0): 

43 print( 

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

45Manage the contents of one or more debug suites 

46 

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

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

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

50 ) 

51 

52 sys.exit(exit_code) 

53 

54 

55################################################################################ 

56 

57 

58def clean(debug_suite, transaction): 

59 session = transaction.session 

60 

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

62 any_suite = session.scalars( 

63 select(Suite).filter_by(debug_suite=debug_suite).limit(1) 

64 ).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.scalars( 

132 select(Suite).where( 

133 Suite.suite_name.in_(debug_suite_names) 

134 | Suite.codename.in_(debug_suite_names) 

135 ) 

136 ) 

137 

138 for debug_suite in debug_suites: 

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

140 clean(debug_suite, transaction) 

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

142 transaction.commit() 

143 else: 

144 transaction.rollback() 

145 

146 Logger.close()