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 

29 

30from daklib import daklog 

31from daklib.archive import ArchiveTransaction 

32from daklib.config import Config 

33from daklib.dbconn import Suite 

34 

35################################################################################ 

36 

37Options = None 

38Logger = None 

39 

40################################################################################ 

41 

42 

43def usage(exit_code=0): 

44 print( 

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

46Manage the contents of one or more debug suites 

47 

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

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

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

51 ) 

52 

53 sys.exit(exit_code) 

54 

55 

56################################################################################ 

57 

58 

59def clean(debug_suite, transaction): 

60 session = transaction.session 

61 

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

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

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

65 raise Exception( 

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

67 ) 

68 

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

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

71 query = """ 

72 WITH 

73 sources_to_keep AS 

74 (SELECT DISTINCT sa.source 

75 FROM src_associations sa 

76 JOIN suite ON sa.suite = suite.id 

77 WHERE suite.debugsuite_id = :debugsuite_id), 

78 sources_removed AS 

79 (DELETE FROM src_associations sa 

80 WHERE sa.suite = :debugsuite_id 

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

82 RETURNING sa.source) 

83 DELETE FROM bin_associations ba 

84 USING binaries b 

85 WHERE ba.suite = :debugsuite_id 

86 AND ba.bin = b.id 

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

88 RETURNING 

89 b.package, 

90 b.version, 

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

92 """ 

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

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

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

96 

97 

98def main(): 

99 global Options, Logger 

100 

101 cnf = Config() 

102 

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

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

105 if key not in cnf: 105 ↛ 103line 105 didn't jump to line 103, because the condition on line 105 was never false

106 cnf[key] = "" 

107 

108 Arguments = [ 

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

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

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

112 ] 

113 

114 debug_suite_names = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv) 

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

116 

117 if Options["Help"]: 

118 usage() 

119 

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

121 

122 with ArchiveTransaction() as transaction: 

123 session = transaction.session 

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

125 if len(debug_suite_names) != 0: 

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

127 sys.exit(1) 

128 raise Exception("Not yet implemented.") 

129 else: 

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

131 Suite.suite_name.in_(debug_suite_names) 

132 ) 

133 

134 for debug_suite in debug_suites: 

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

136 clean(debug_suite, transaction) 

137 if not Options["No-Action"]: 137 ↛ 140line 137 didn't jump to line 140, because the condition on line 137 was never false

138 transaction.commit() 

139 else: 

140 transaction.rollback() 

141 

142 Logger.close() 

143 

144 

145####################################################################################### 

146 

147 

148if __name__ == "__main__": 

149 main()