]> git.decadent.org.uk Git - dak.git/blob - dak/show_deferred.py
Merge remote-tracking branch 'jcristau/cs-set-log-suite'
[dak.git] / dak / show_deferred.py
1 #!/usr/bin/env python
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
23 import sys, os, re, time
24 import apt_pkg
25 import rrdtool
26
27 from debian import deb822
28
29 from daklib.dbconn import *
30 from daklib.gpg import SignedFile
31 from daklib import utils
32 from daklib.regexes import re_html_escaping, html_escaping
33
34 ################################################################################
35
36 row_number = 1
37
38 def html_escape(s):
39     return re_html_escaping.sub(lambda x: html_escaping.get(x.group(0)), s)
40
41 ################################################################################
42
43 def header():
44   return  """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
45         <html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
46         <title>Deferred uploads to Debian</title>
47         <link type="text/css" rel="stylesheet" href="style.css">
48         <link rel="shortcut icon" href="https://www.debian.org/favicon.ico">
49         </head>
50         <body>
51         <div align="center">
52         <a href="https://www.debian.org/">
53      <img src="https://www.debian.org/logos/openlogo-nd-50.png" border="0" hspace="0" vspace="0" alt=""></a>
54         <a href="https://www.debian.org/">
55      <img src="https://www.debian.org/Pics/debian.png" border="0" hspace="0" vspace="0" alt="Debian Project"></a>
56         </div>
57         <br />
58         <table class="reddy" width="100%">
59         <tr>
60         <td class="reddy">
61     <img src="https://www.debian.org/Pics/red-upperleft.png" align="left" border="0" hspace="0" vspace="0"
62      alt="" width="15" height="16"></td>
63         <td rowspan="2" class="reddy">Deferred uploads to Debian</td>
64         <td class="reddy">
65     <img src="https://www.debian.org/Pics/red-upperright.png" align="right" border="0" hspace="0" vspace="0"
66      alt="" width="16" height="16"></td>
67         </tr>
68         <tr>
69         <td class="reddy">
70     <img src="https://www.debian.org/Pics/red-lowerleft.png" align="left" border="0" hspace="0" vspace="0"
71      alt="" width="16" height="16"></td>
72         <td class="reddy">
73     <img src="https://www.debian.org/Pics/red-lowerright.png" align="right" border="0" hspace="0" vspace="0"
74      alt="" width="15" height="16"></td>
75         </tr>
76         </table>
77         """
78
79 def footer():
80     res = "<p class=\"validate\">Timestamp: %s (UTC)</p>" % (time.strftime("%d.%m.%Y / %H:%M:%S", time.gmtime()))
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.encode('utf-8')
84
85 def table_header():
86     return """<h1>Deferred uploads</h1>
87       <center><table border="0">
88         <tr>
89           <th align="center">Change</th>
90           <th align="center">Time remaining</th>
91           <th align="center">Uploader</th>
92           <th align="center">Closes</th>
93         </tr>
94         """
95
96 def table_footer():
97     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="ftp://ftp-master.debian.org/pub/UploadQueue/README">UploadQueue-README</a> for more information.</p></center><br/>\n'
98
99 def table_row(changesname, delay, changed_by, closes, fingerprint):
100     global row_number
101
102     res = '<tr class="%s">'%((row_number%2) and 'odd' or 'even')
103     res += (2*'<td valign="top">%s</td>')%tuple(map(html_escape,(changesname,delay)))
104     res += '<td valign="top">%s<br><span class=\"deferredfp\">Fingerprint: %s</span></td>' % (html_escape(changed_by), fingerprint)
105     res += ('<td valign="top">%s</td>' %
106              ''.join(map(lambda close:  '<a href="https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s">#%s</a><br>' % (close, close),closes)))
107     res += '</tr>\n'
108     row_number+=1
109     return res
110
111 def update_graph_database(rrd_dir, *counts):
112     if not rrd_dir:
113         return
114
115     rrd_file = os.path.join(rrd_dir, 'deferred.rrd')
116     counts = [str(count) for count in counts]
117     update = [rrd_file, "N:"+":".join(counts)]
118
119     try:
120         rrdtool.update(*update)
121     except rrdtool.error:
122         create = [rrd_file]+"""
123 --step
124 300
125 --start
126 0
127 DS:day0:GAUGE:7200:0:1000
128 DS:day1:GAUGE:7200:0:1000
129 DS:day2:GAUGE:7200:0:1000
130 DS:day3:GAUGE:7200:0:1000
131 DS:day4:GAUGE:7200:0:1000
132 DS:day5:GAUGE:7200:0:1000
133 DS:day6:GAUGE:7200:0:1000
134 DS:day7:GAUGE:7200:0:1000
135 DS:day8:GAUGE:7200:0:1000
136 DS:day9:GAUGE:7200:0:1000
137 DS:day10:GAUGE:7200:0:1000
138 DS:day11:GAUGE:7200:0:1000
139 DS:day12:GAUGE:7200:0:1000
140 DS:day13:GAUGE:7200:0:1000
141 DS:day14:GAUGE:7200:0:1000
142 DS:day15:GAUGE:7200:0:1000
143 RRA:AVERAGE:0.5:1:599
144 RRA:AVERAGE:0.5:6:700
145 RRA:AVERAGE:0.5:24:775
146 RRA:AVERAGE:0.5:288:795
147 RRA:MIN:0.5:1:600
148 RRA:MIN:0.5:6:700
149 RRA:MIN:0.5:24:775
150 RRA:MIN:0.5:288:795
151 RRA:MAX:0.5:1:600
152 RRA:MAX:0.5:6:700
153 RRA:MAX:0.5:24:775
154 RRA:MAX:0.5:288:795
155 """.strip().split("\n")
156         try:
157             rc = rrdtool.create(*create)
158             ru = rrdtool.update(*update)
159         except rrdtool.error as e:
160             print('warning: queue_report: rrdtool error, skipping %s.rrd: %s' % (type, e))
161     except NameError:
162         pass
163
164 def get_upload_data(changesfn):
165     achanges = deb822.Changes(file(changesfn))
166     changesname = os.path.basename(changesfn)
167     delay = os.path.basename(os.path.dirname(changesfn))
168     m = re.match(r'([0-9]+)-day', delay)
169     if m:
170         delaydays = int(m.group(1))
171         remainingtime = (delaydays>0)*max(0,24*60*60+os.stat(changesfn).st_mtime-time.time())
172         delay = "%d days %02d:%02d" %(max(delaydays-1,0), int(remainingtime/3600),int(remainingtime/60)%60)
173     else:
174         delaydays = 0
175         remainingtime = 0
176
177     uploader = achanges.get('changed-by')
178     uploader = re.sub(r'^\s*(\S.*)\s+<.*>',r'\1',uploader)
179     with utils.open_file(changesfn) as f:
180         fingerprint = SignedFile(f.read(), keyrings=get_active_keyring_paths(), require_signature=False).fingerprint
181     if Cnf.has_key("Show-Deferred::LinkPath"):
182         isnew = 0
183         suites = get_suites_source_in(achanges['source'])
184         if 'unstable' not in suites and 'experimental' not in suites:
185             isnew = 1
186
187         for b in achanges['binary'].split():
188             suites = get_suites_binary_in(b)
189             if 'unstable' not in suites and 'experimental' not in suites:
190                 isnew = 1
191
192         if not isnew:
193             # we don't link .changes because we don't want other people to
194             # upload it with the existing signature.
195             for afn in map(lambda x: x['name'],achanges['files']):
196                 lfn = os.path.join(Cnf["Show-Deferred::LinkPath"],afn)
197                 qfn = os.path.join(os.path.dirname(changesfn),afn)
198                 if os.path.islink(lfn):
199                     os.unlink(lfn)
200                 if os.path.exists(qfn):
201                     os.symlink(qfn,lfn)
202                     os.chmod(qfn, 0o644)
203     return (max(delaydays-1,0)*24*60*60+remainingtime, changesname, delay, uploader, achanges.get('closes','').split(), fingerprint, achanges, delaydays)
204
205 def list_uploads(filelist, rrd_dir):
206     uploads = map(get_upload_data, filelist)
207     uploads.sort()
208     # print the summary page
209     print header()
210     if uploads:
211         print table_header()
212         print ''.join(map(lambda x: table_row(*x[1:6]), uploads)).encode('utf-8')
213         print table_footer()
214     else:
215         print '<h1>Currently no deferred uploads to Debian</h1>'
216     print footer()
217     # machine readable summary
218     if Cnf.has_key("Show-Deferred::LinkPath"):
219         fn = os.path.join(Cnf["Show-Deferred::LinkPath"],'.status.tmp')
220         f = open(fn,"w")
221         try:
222             counts = [0]*16
223             for u in uploads:
224                 counts[u[7]] += 1
225                 print >> f, "Changes-file: %s"%u[1]
226                 fields = """Location: DEFERRED
227 Delayed-Until: %s
228 Delay-Remaining: %s
229 Fingerprint: %s"""%(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time()+u[0])),u[2], u[5])
230                 print >> f, fields
231                 encoded = unicode(u[6]).encode('utf-8')
232                 print >> f, encoded.rstrip()
233                 open(os.path.join(Cnf["Show-Deferred::LinkPath"],u[1]),"w").write(encoded+fields+'\n')
234                 print >> f
235             f.close()
236             os.rename(os.path.join(Cnf["Show-Deferred::LinkPath"],'.status.tmp'),
237                       os.path.join(Cnf["Show-Deferred::LinkPath"],'status'))
238             update_graph_database(rrd_dir, *counts)
239         except:
240             os.unlink(fn)
241             raise
242
243 def usage (exit_code=0):
244     if exit_code:
245         f = sys.stderr
246     else:
247         f = sys.stdout
248     print >> f, """Usage: dak show-deferred
249   -h, --help                    show this help and exit.
250   -p, --link-path [path]        override output directory.
251   -d, --deferred-queue [path]   path to the deferred queue
252   -r, --rrd=key                 Directory where rrd files to be updated are stored
253   """
254     sys.exit(exit_code)
255
256 def init():
257     global Cnf, Options
258     Cnf = utils.get_conf()
259     Arguments = [('h',"help","Show-Deferred::Options::Help"),
260                  ("p","link-path","Show-Deferred::LinkPath","HasArg"),
261                  ("d","deferred-queue","Show-Deferred::DeferredQueue","HasArg"),
262                  ('r',"rrd","Show-Deferred::Options::Rrd", "HasArg")]
263     args = apt_pkg.parse_commandline(Cnf,Arguments,sys.argv)
264     for i in ["help"]:
265         if not Cnf.has_key("Show-Deferred::Options::%s" % (i)):
266             Cnf["Show-Deferred::Options::%s" % (i)] = ""
267     for i,j in [("DeferredQueue","--deferred-queue")]:
268         if not Cnf.has_key("Show-Deferred::%s" % (i)):
269             print >> sys.stderr, """Show-Deferred::%s is mandatory.
270   set via config file or command-line option %s"""%(i,j)
271
272     Options = Cnf.subtree("Show-Deferred::Options")
273     if Options["help"]:
274         usage()
275
276     # Initialise database connection
277     DBConn()
278
279     return args
280
281 def main():
282     args = init()
283     if len(args)!=0:
284         usage(1)
285
286     if Cnf.has_key("Show-Deferred::Options::Rrd"):
287         rrd_dir = Cnf["Show-Deferred::Options::Rrd"]
288     elif Cnf.has_key("Dir::Rrd"):
289         rrd_dir = Cnf["Dir::Rrd"]
290     else:
291         rrd_dir = None
292
293     filelist = []
294     for r,d,f  in os.walk(Cnf["Show-Deferred::DeferredQueue"]):
295         filelist += map (lambda x: os.path.join(r,x),
296                          filter(lambda x: x.endswith('.changes'), f))
297     list_uploads(filelist, rrd_dir)
298
299     available_changes = set(map(os.path.basename,filelist))
300     if Cnf.has_key("Show-Deferred::LinkPath"):
301         # remove dead links
302         for r,d,f in os.walk(Cnf["Show-Deferred::LinkPath"]):
303             for af in f:
304                 afp = os.path.join(r,af)
305                 if (not os.path.exists(afp) or
306                     (af.endswith('.changes') and af not in available_changes)):
307                     os.unlink(afp)
308
309 if __name__ == '__main__':
310     main()