Coverage for daklib/externalsignature.py: 83%

51 statements  

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

1"""external signature requests 

2 

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

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

5@license: GNU General Public License version 2 or later 

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 

22import json 

23from typing import TYPE_CHECKING, Any 

24 

25import sqlalchemy.dialects.postgresql as pgsql 

26import sqlalchemy.sql as sql 

27 

28import daklib.gpg 

29from daklib.config import Config 

30from daklib.dbconn import ( 

31 Architecture, 

32 BinAssociations, 

33 DBBinary, 

34 DBSource, 

35 ExternalSignatureRequests, 

36 Suite, 

37) 

38 

39if TYPE_CHECKING: 

40 from collections.abc import Iterable 

41 

42 from sqlalchemy.orm import Session 

43 

44 

45def export_external_signature_requests(session: "Session", path: str) -> None: 

46 tbl_arch = Architecture.__table__ 

47 tbl_ba = BinAssociations.__table__ 

48 tbl_bin = DBBinary.__table__ 

49 tbl_src = DBSource.__table__ 

50 tbl_esr = ExternalSignatureRequests.__table__ 

51 tbl_suite = Suite.__table__ 

52 

53 query = ( 

54 sql.select( 

55 tbl_bin.c.package, 

56 tbl_src.c.source, 

57 tbl_suite.c.suite_name, 

58 tbl_suite.c.codename, 

59 tbl_arch.c.arch_string, 

60 sql.func.max(tbl_bin.c.version), 

61 ) 

62 .select_from( 

63 tbl_esr.join(tbl_suite) 

64 .join(tbl_ba, tbl_ba.c.id == tbl_esr.c.association_id) 

65 .join(tbl_bin) 

66 .join(tbl_arch) 

67 .join(tbl_src, tbl_bin.c.source == tbl_src.c.id) 

68 ) 

69 .group_by( 

70 tbl_bin.c.package, 

71 tbl_src.c.source, 

72 tbl_suite.c.suite_name, 

73 tbl_suite.c.codename, 

74 tbl_arch.c.arch_string, 

75 ) 

76 ) 

77 requests = session.execute(query) 

78 

79 data = { 

80 "packages": [ 

81 { 

82 "package": row[0], 

83 "source": row[1], 

84 "suite": row[2], 

85 "codename": row[3], 

86 "architecture": row[4], 

87 "version": row[5], 

88 } 

89 for row in requests 

90 ], 

91 } 

92 

93 with open(path, "w") as fh: 

94 json.dump(data, fh, indent=2) 

95 

96 

97def sign_external_signature_requests( 

98 session: "Session", path: str, keyids: "Iterable[str]", args: dict[str, Any] = {} 

99) -> None: 

100 outpath = "{}.gpg".format(path) 

101 with open(path, "r") as infile, open(outpath, "w") as outfile: 

102 daklib.gpg.sign(infile, outfile, keyids, inline=False, **args) 

103 

104 

105def add_external_signature_request( 

106 session: "Session", target_suite: Suite, suite: Suite, binary: DBBinary 

107) -> None: 

108 tbl_ba = BinAssociations.__table__ 

109 tbl_esr = ExternalSignatureRequests.__table__ 

110 

111 select = sql.select( # type: ignore[call-overload] 

112 tbl_ba.c.id, target_suite.suite_id 

113 ).where((tbl_ba.c.suite == suite.suite_id) & (tbl_ba.c.bin == binary.binary_id)) 

114 insert = ( 

115 pgsql.insert(tbl_esr) # type: ignore[arg-type] 

116 .from_select([tbl_esr.c.association_id, tbl_esr.c.suite_id], select) 

117 .on_conflict_do_nothing() 

118 ) 

119 session.execute(insert) 

120 

121 

122def check_upload_for_external_signature_request( 

123 session: "Session", target_suite: Suite, suite: Suite, binary: DBBinary 

124) -> None: 

125 if "External-Signature-Requests" not in Config(): 

126 return 

127 config = Config().subtree("External-Signature-Requests") 

128 config_sources = config.subtree("Sources") 

129 

130 source = binary.source 

131 

132 if source.source not in config_sources: 132 ↛ 133line 132 didn't jump to line 133 because the condition on line 132 was never true

133 return 

134 src_config = config_sources.subtree(source.source) 

135 

136 if binary.package not in src_config.value_list("Packages"): 

137 return 

138 

139 suites = config.value_list("Default-Suites") 

140 if "Suites" in src_config: 140 ↛ 141line 140 didn't jump to line 141 because the condition on line 140 was never true

141 suites = src_config.value_list("Suites") 

142 if target_suite.suite_name not in suites: 

143 return 

144 

145 archs = config.value_list("Default-Architectures") 

146 if "Architectures" in src_config: 146 ↛ 148line 146 didn't jump to line 148 because the condition on line 146 was always true

147 archs = src_config.value_list("Architectures") 

148 if binary.architecture.arch_string not in archs: 148 ↛ 149line 148 didn't jump to line 149 because the condition on line 148 was never true

149 return 

150 

151 add_external_signature_request(session, target_suite, suite, binary)