Coverage for dak/show_deferred.py: 22%

153 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2025-08-26 22:11 +0000

1#! /usr/bin/env python3 

2 

3"""Overview of the DEFERRED queue, based on queue-report""" 

4# Copyright (C) 2001, 2002, 2003, 2005, 2006 James Troup <james@nocrew.org> 

5# Copyright (C) 2008 Thomas Viehmann <tv@beamnet.de> 

6 

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

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

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

10# (at your option) any later version. 

11 

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

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

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

15# GNU General Public License for more details. 

16 

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

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

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

20 

21################################################################################ 

22 

23import html 

24import os 

25import re 

26import sys 

27import time 

28 

29import apt_pkg 

30import rrdtool 

31 

32from daklib import utils 

33from daklib.dbconn import DBConn, get_active_keyring_paths, get_suites_source_in 

34from daklib.gpg import SignedFile 

35from debian import deb822 

36 

37################################################################################ 

38 

39 

40def header(): 

41 return """<!DOCTYPE html> 

42 <html lang="en"><head><meta charset="utf-8"> 

43 <title>Deferred uploads to Debian</title> 

44 <link rel="stylesheet" href="style.css"> 

45 <link rel="shortcut icon" href="https://www.debian.org/favicon.ico"> 

46 </head> 

47 <body> 

48 <div align="center"> 

49 <a href="https://www.debian.org/"> 

50 <img src="https://www.debian.org/logos/openlogo-nd-50.png" border="0" hspace="0" vspace="0" alt=""></a> 

51 <a href="https://www.debian.org/"> 

52 <img src="https://www.debian.org/Pics/debian.png" border="0" hspace="0" vspace="0" alt="Debian Project"></a> 

53 </div> 

54 <br> 

55 <table class="reddy" width="100%"> 

56 <tr> 

57 <td class="reddy"> 

58 <img src="https://www.debian.org/Pics/red-upperleft.png" align="left" border="0" hspace="0" vspace="0" 

59 alt="" width="15" height="16"></td> 

60 <td rowspan="2" class="reddy">Deferred uploads to Debian</td> 

61 <td class="reddy"> 

62 <img src="https://www.debian.org/Pics/red-upperright.png" align="right" border="0" hspace="0" vspace="0" 

63 alt="" width="16" height="16"></td> 

64 </tr> 

65 <tr> 

66 <td class="reddy"> 

67 <img src="https://www.debian.org/Pics/red-lowerleft.png" align="left" border="0" hspace="0" vspace="0" 

68 alt="" width="16" height="16"></td> 

69 <td class="reddy"> 

70 <img src="https://www.debian.org/Pics/red-lowerright.png" align="right" border="0" hspace="0" vspace="0" 

71 alt="" width="15" height="16"></td> 

72 </tr> 

73 </table> 

74 """ 

75 

76 

77def footer(): 

78 res = '<p class="validate">Timestamp: %s (UTC)</p>' % ( 

79 time.strftime("%d.%m.%Y / %H:%M:%S", time.gmtime()) 

80 ) 

81 res += '<p class="timestamp">There are <a href="/stat.html">graphs about the queues</a> available.</p>' 

82 res += "</body></html>" 

83 return res 

84 

85 

86def table_header(): 

87 return """<h1>Deferred uploads</h1> 

88 <center><table border="0"> 

89 <tr> 

90 <th align="center">Change</th> 

91 <th align="center">Time remaining</th> 

92 <th align="center">Uploader</th> 

93 <th align="center">Closes</th> 

94 </tr> 

95 """ 

96 

97 

98def table_footer(): 

99 return '</table><br><p>non-NEW uploads are <a href="/deferred/">available</a> (<a href="/deferred/status">machine readable version</a>), see the <a href="https://www.debian.org/doc/manuals/developers-reference/ch05.en.html#delayed-incoming">Developer\'s reference</a> for more information on the DELAYED queue.</p></center><br>\n' 

100 

101 

102def table_row(changesname, delay, changed_by, closes, fingerprint): 

103 res = "<tr>" 

104 res += (2 * '<td valign="top">%s</td>') % tuple( 

105 html.escape(x, quote=False) for x in (changesname, delay) 

106 ) 

107 res += ( 

108 '<td valign="top">%s<br><span class="deferredfp">Fingerprint: %s</span></td>' 

109 % (html.escape(changed_by or "Unknown", quote=False), fingerprint) 

110 ) 

111 res += '<td valign="top">%s</td>' % "".join( 

112 [ 

113 '<a href="https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s">#%s</a><br>' 

114 % (close, close) 

115 for close in closes 

116 ] 

117 ) 

118 res += "</tr>\n" 

119 return res 

120 

121 

122def update_graph_database(rrd_dir, *counts): 

123 if not rrd_dir: 

124 return 

125 

126 rrd_file = os.path.join(rrd_dir, "deferred.rrd") 

127 counts = [str(count) for count in counts] 

128 update = [rrd_file, "N:" + ":".join(counts)] 

129 

130 try: 

131 rrdtool.update(*update) 

132 except rrdtool.error: 

133 create = ( 

134 [rrd_file] 

135 + """ 

136--step 

137300 

138--start 

1390 

140DS:day0:GAUGE:7200:0:1000 

141DS:day1:GAUGE:7200:0:1000 

142DS:day2:GAUGE:7200:0:1000 

143DS:day3:GAUGE:7200:0:1000 

144DS:day4:GAUGE:7200:0:1000 

145DS:day5:GAUGE:7200:0:1000 

146DS:day6:GAUGE:7200:0:1000 

147DS:day7:GAUGE:7200:0:1000 

148DS:day8:GAUGE:7200:0:1000 

149DS:day9:GAUGE:7200:0:1000 

150DS:day10:GAUGE:7200:0:1000 

151DS:day11:GAUGE:7200:0:1000 

152DS:day12:GAUGE:7200:0:1000 

153DS:day13:GAUGE:7200:0:1000 

154DS:day14:GAUGE:7200:0:1000 

155DS:day15:GAUGE:7200:0:1000 

156RRA:AVERAGE:0.5:1:599 

157RRA:AVERAGE:0.5:6:700 

158RRA:AVERAGE:0.5:24:775 

159RRA:AVERAGE:0.5:288:795 

160RRA:MIN:0.5:1:600 

161RRA:MIN:0.5:6:700 

162RRA:MIN:0.5:24:775 

163RRA:MIN:0.5:288:795 

164RRA:MAX:0.5:1:600 

165RRA:MAX:0.5:6:700 

166RRA:MAX:0.5:24:775 

167RRA:MAX:0.5:288:795 

168""".strip().split( 

169 "\n" 

170 ) 

171 ) 

172 try: 

173 rrdtool.create(*create) 

174 rrdtool.update(*update) 

175 except rrdtool.error as e: 

176 print( 

177 ( 

178 "warning: queue_report: rrdtool error, skipping %s.rrd: %s" 

179 % (type, e) 

180 ) 

181 ) 

182 except NameError: 

183 pass 

184 

185 

186def get_upload_data(changesfn): 

187 with open(changesfn) as fd: 

188 achanges = deb822.Changes(fd) 

189 changesname = os.path.basename(changesfn) 

190 delay = os.path.basename(os.path.dirname(changesfn)) 

191 m = re.match(r"([0-9]+)-day", delay) 

192 if m: 

193 delaydays = int(m.group(1)) 

194 remainingtime = (delaydays > 0) * max( 

195 0, 24 * 60 * 60 + os.stat(changesfn).st_mtime - time.time() 

196 ) 

197 delay = "%d days %02d:%02d" % ( 

198 max(delaydays - 1, 0), 

199 int(remainingtime / 3600), 

200 int(remainingtime / 60) % 60, 

201 ) 

202 else: 

203 delaydays = 0 

204 remainingtime = 0 

205 

206 uploader = achanges.get("changed-by") 

207 try: 

208 uploader = re.sub(r"^\s*(\S.*)\s+<.*>", r"\1", uploader) 

209 except TypeError: 

210 uploader = None 

211 with open(changesfn, "rb") as f: 

212 fingerprint = SignedFile( 

213 f.read(), keyrings=get_active_keyring_paths(), require_signature=False 

214 ).fingerprint 

215 if "Show-Deferred::LinkPath" in Cnf: 

216 isnew = False 

217 suites = get_suites_source_in(achanges["source"]) 

218 if "unstable" not in suites and "experimental" not in suites: 

219 isnew = True 

220 

221 if not isnew: 

222 # we don't link .changes because we don't want other people to 

223 # upload it with the existing signature. 

224 for afn in [x["name"] for x in achanges["files"]]: 

225 lfn = os.path.join(Cnf["Show-Deferred::LinkPath"], afn) 

226 qfn = os.path.join(os.path.dirname(changesfn), afn) 

227 if os.path.islink(lfn): 

228 os.unlink(lfn) 

229 if os.path.exists(qfn): 

230 os.symlink(qfn, lfn) 

231 os.chmod(qfn, 0o644) 

232 return ( 

233 max(delaydays - 1, 0) * 24 * 60 * 60 + remainingtime, 

234 changesname, 

235 delay, 

236 uploader, 

237 achanges.get("closes", "").split(), 

238 fingerprint, 

239 achanges, 

240 delaydays, 

241 ) 

242 

243 

244def list_uploads(filelist, rrd_dir): 

245 uploads = sorted(get_upload_data(x) for x in filelist) 

246 # print the summary page 

247 print(header()) 

248 if uploads: 

249 print(table_header()) 

250 print("".join(table_row(*x[1:6]) for x in uploads)) 

251 print(table_footer()) 

252 else: 

253 print("<h1>Currently no deferred uploads to Debian</h1>") 

254 print(footer()) 

255 # machine readable summary 

256 if "Show-Deferred::LinkPath" in Cnf: 

257 fn = os.path.join(Cnf["Show-Deferred::LinkPath"], ".status.tmp") 

258 f = open(fn, "w") 

259 try: 

260 counts = [0] * 16 

261 for u in uploads: 

262 counts[u[7]] += 1 

263 print("Changes-file: %s" % u[1], file=f) 

264 fields = """Location: DEFERRED 

265Delayed-Until: %s 

266Delay-Remaining: %s 

267Fingerprint: %s""" % ( 

268 time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time() + u[0])), 

269 u[2], 

270 u[5], 

271 ) 

272 print(fields, file=f) 

273 encoded = u[6].dump() 

274 print(encoded.rstrip(), file=f) 

275 open(os.path.join(Cnf["Show-Deferred::LinkPath"], u[1]), "w").write( 

276 encoded + fields + "\n" 

277 ) 

278 print(file=f) 

279 f.close() 

280 os.rename( 

281 os.path.join(Cnf["Show-Deferred::LinkPath"], ".status.tmp"), 

282 os.path.join(Cnf["Show-Deferred::LinkPath"], "status"), 

283 ) 

284 update_graph_database(rrd_dir, *counts) 

285 except: 

286 os.unlink(fn) 

287 raise 

288 

289 

290def usage(exit_code=0): 

291 if exit_code: 291 ↛ 292line 291 didn't jump to line 292, because the condition on line 291 was never true

292 f = sys.stderr 

293 else: 

294 f = sys.stdout 

295 print( 

296 """Usage: dak show-deferred 

297 -h, --help show this help and exit. 

298 -p, --link-path [path] override output directory. 

299 -d, --deferred-queue [path] path to the deferred queue 

300 -r, --rrd=key Directory where rrd files to be updated are stored 

301 """, 

302 file=f, 

303 ) 

304 sys.exit(exit_code) 

305 

306 

307def init(): 

308 global Cnf, Options 

309 Cnf = utils.get_conf() 

310 Arguments = [ 

311 ("h", "help", "Show-Deferred::Options::Help"), 

312 ("p", "link-path", "Show-Deferred::LinkPath", "HasArg"), 

313 ("d", "deferred-queue", "Show-Deferred::DeferredQueue", "HasArg"), 

314 ("r", "rrd", "Show-Deferred::Options::Rrd", "HasArg"), 

315 ] 

316 args = apt_pkg.parse_commandline(Cnf, Arguments, sys.argv) 

317 for i in ["help"]: 

318 key = "Show-Deferred::Options::%s" % i 

319 if key not in Cnf: 319 ↛ 320line 319 didn't jump to line 320, because the condition on line 319 was never true

320 Cnf[key] = "" 

321 for i, j in [("DeferredQueue", "--deferred-queue")]: 

322 key = "Show-Deferred::%s" % i 

323 if key not in Cnf: 323 ↛ 321line 323 didn't jump to line 321, because the condition on line 323 was never false

324 print( 

325 """%s is mandatory. 

326 set via config file or command-line option %s""" 

327 % (key, j), 

328 file=sys.stderr, 

329 ) 

330 

331 Options = Cnf.subtree("Show-Deferred::Options") 

332 if Options["help"]: 332 ↛ 336line 332 didn't jump to line 336, because the condition on line 332 was never false

333 usage() 

334 

335 # Initialise database connection 

336 DBConn() 

337 

338 return args 

339 

340 

341def main(): 

342 args = init() 

343 if len(args) != 0: 

344 usage(1) 

345 

346 if "Show-Deferred::Options::Rrd" in Cnf: 

347 rrd_dir = Cnf["Show-Deferred::Options::Rrd"] 

348 elif "Dir::Rrd" in Cnf: 

349 rrd_dir = Cnf["Dir::Rrd"] 

350 else: 

351 rrd_dir = None 

352 

353 filelist = [] 

354 for r, d, f in os.walk(Cnf["Show-Deferred::DeferredQueue"]): 

355 filelist.extend(os.path.join(r, x) for x in f if x.endswith(".changes")) 

356 list_uploads(filelist, rrd_dir) 

357 

358 available_changes = set(os.path.basename(x) for x in filelist) 

359 if "Show-Deferred::LinkPath" in Cnf: 

360 # remove dead links 

361 for r, d, f in os.walk(Cnf["Show-Deferred::LinkPath"]): 

362 for af in f: 

363 afp = os.path.join(r, af) 

364 if not os.path.exists(afp) or ( 

365 af.endswith(".changes") and af not in available_changes 

366 ): 

367 os.unlink(afp) 

368 

369 

370if __name__ == "__main__": 

371 main()