]> git.decadent.org.uk Git - dak.git/blob - dak/show_deferred.py
Implement NEW/BYHAND/DEFERRED/OSPU/SPU graphs
[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
26 try:
27     # starting with squeeze
28     from debian import deb822
29 except:
30     # up to lenny
31     from debian_bundle import deb822
32
33 from daklib.dbconn import *
34 from daklib import utils
35 from daklib.regexes import re_html_escaping, html_escaping
36
37 ################################################################################
38 ### work around bug #487902 in debian-python 0.1.10
39 deb822.Changes._multivalued_fields = {
40             "files": [ "md5sum", "size", "section", "priority", "name" ],
41             "checksums-sha1": ["sha1", "size", "name"],
42             "checksums-sha256": ["sha256", "size", "name"],
43           }
44
45 ################################################################################
46
47 row_number = 1
48
49 def html_escape(s):
50     return re_html_escaping.sub(lambda x: html_escaping.get(x.group(0)), s)
51
52 ################################################################################
53
54 def header():
55   return  """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
56         <html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
57         <title>Deferred uploads to Debian</title>
58         <link type="text/css" rel="stylesheet" href="style.css">
59         <link rel="shortcut icon" href="http://www.debian.org/favicon.ico">
60         </head>
61         <body>
62         <div align="center">
63         <a href="http://www.debian.org/">
64      <img src="http://www.debian.org/logos/openlogo-nd-50.png" border="0" hspace="0" vspace="0" alt=""></a>
65         <a href="http://www.debian.org/">
66      <img src="http://www.debian.org/Pics/debian.png" border="0" hspace="0" vspace="0" alt="Debian Project"></a>
67         </div>
68         <br />
69         <table class="reddy" width="100%">
70         <tr>
71         <td class="reddy">
72     <img src="http://www.debian.org/Pics/red-upperleft.png" align="left" border="0" hspace="0" vspace="0"
73      alt="" width="15" height="16"></td>
74         <td rowspan="2" class="reddy">Deferred uploads to Debian</td>
75         <td class="reddy">
76     <img src="http://www.debian.org/Pics/red-upperright.png" align="right" border="0" hspace="0" vspace="0"
77      alt="" width="16" height="16"></td>
78         </tr>
79         <tr>
80         <td class="reddy">
81     <img src="http://www.debian.org/Pics/red-lowerleft.png" align="left" border="0" hspace="0" vspace="0"
82      alt="" width="16" height="16"></td>
83         <td class="reddy">
84     <img src="http://www.debian.org/Pics/red-lowerright.png" align="right" border="0" hspace="0" vspace="0"
85      alt="" width="15" height="16"></td>
86         </tr>
87         </table>
88         """
89
90 def footer():
91     res = """
92 <p><img src="deferred-day.png" alt="deferred, last day"></p>
93 <p><img src="deferred-week.png" alt="deferred, last week"></p>
94 <p><img src="deferred-month.png" alt="deferred, last month"></p>
95 <p><img src="deferred-year.png" alt="deferred, last year"></p>
96 <p><img src="deferred-5years.png" alt="deferred, last 5 years"></p>
97 <p><img src="deferred-10years.png" alt="deferred, last 10 years"></p>
98 """
99     res += "<p class=\"validate\">Timestamp: %s (UTC)</p>" % (time.strftime("%d.%m.%Y / %H:%M:%S", time.gmtime()))
100     res += """<a href="http://validator.w3.org/check?uri=referer">
101     <img border="0" src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" height="31" width="88"></a>
102         <a href="http://jigsaw.w3.org/css-validator/check/referer">
103     <img border="0" src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"
104      height="31" width="88"></a>
105     """
106     res += "</body></html>"
107     return res
108
109 def table_header():
110     return """<h1>Deferred uploads</h1>
111       <center><table border="0">
112         <tr>
113           <th align="center">Change</th>
114           <th align="center">Time remaining</th>
115           <th align="center">Uploader</th>
116           <th align="center">Closes</th>
117         </tr>
118         """
119
120 def table_footer():
121     return '</table><br/><p>non-NEW uploads are <a href="/deferred/">available</a>, see the <a href="ftp://ftp-master.debian.org/pub/UploadQueue/README">UploadQueue-README</a> for more information.</p></center><br/>\n'
122
123 def table_row(changesname, delay, changed_by, closes):
124     global row_number
125
126     res = '<tr class="%s">'%((row_number%2) and 'odd' or 'even')
127     res += (3*'<td valign="top">%s</td>')%tuple(map(html_escape,(changesname,delay,changed_by)))
128     res += ('<td valign="top">%s</td>' %
129              ''.join(map(lambda close:  '<a href="http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s">#%s</a><br>' % (close, close),closes)))
130     res += '</tr>\n'
131     row_number+=1
132     return res
133
134 def get_upload_data(changesfn):
135     achanges = deb822.Changes(file(changesfn))
136     changesname = os.path.basename(changesfn)
137     delay = os.path.basename(os.path.dirname(changesfn))
138     m = re.match(r'([0-9]+)-day', delay)
139     if m:
140         delaydays = int(m.group(1))
141         remainingtime = (delaydays>0)*max(0,24*60*60+os.stat(changesfn).st_mtime-time.time())
142         delay = "%d days %02d:%02d" %(max(delaydays-1,0), int(remainingtime/3600),int(remainingtime/60)%60)
143     else:
144         remainingtime = 0
145
146     uploader = achanges.get('changed-by')
147     uploader = re.sub(r'^\s*(\S.*)\s+<.*>',r'\1',uploader)
148     if Cnf.has_key("Show-Deferred::LinkPath"):
149         isnew = 0
150         suites = get_suites_source_in(achanges['source'])
151         if 'unstable' not in suites and 'experimental' not in suites:
152             isnew = 1
153
154         for b in achanges['binary'].split():
155             suites = get_suites_binary_in(b)
156             if 'unstable' not in suites and 'experimental' not in suites:
157                 isnew = 1
158
159         if not isnew:
160             # we don't link .changes because we don't want other people to
161             # upload it with the existing signature.
162             for afn in map(lambda x: x['name'],achanges['files']):
163                 lfn = os.path.join(Cnf["Show-Deferred::LinkPath"],afn)
164                 qfn = os.path.join(os.path.dirname(changesfn),afn)
165                 if os.path.islink(lfn):
166                     os.unlink(lfn)
167                 if os.path.exists(qfn):
168                     os.symlink(qfn,lfn)
169                     os.chmod(qfn, 0644)
170     return (max(delaydays-1,0)*24*60*60+remainingtime, changesname, delay, uploader, achanges.get('closes','').split(),achanges)
171
172 def list_uploads(filelist):
173     uploads = map(get_upload_data, filelist)
174     uploads.sort()
175     # print the summary page
176     print header()
177     if uploads:
178         print table_header()
179         print ''.join(map(lambda x: table_row(*x[1:5]), uploads))
180         print table_footer()
181     else:
182         print '<h1>Currently no deferred uploads to Debian</h1>'
183     print footer()
184     # machine readable summary
185     if Cnf.has_key("Show-Deferred::LinkPath"):
186         fn = os.path.join(Cnf["Show-Deferred::LinkPath"],'.status.tmp')
187         f = open(fn,"w")
188         try:
189             for u in uploads:
190                 print >> f, "Changes-file: %s"%u[1]
191                 fields = """Location: DEFERRED
192 Delayed-Until: %s
193 Delay-Remaining: %s"""%(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time()+u[0])),u[2])
194                 print >> f, fields
195                 print >> f, str(u[5]).rstrip()
196                 open(os.path.join(Cnf["Show-Deferred::LinkPath"],u[1]),"w").write(str(u[5])+fields+'\n')
197                 print >> f
198             f.close()
199             os.rename(os.path.join(Cnf["Show-Deferred::LinkPath"],'.status.tmp'),
200                       os.path.join(Cnf["Show-Deferred::LinkPath"],'status'))
201         except:
202             os.unlink(fn)
203             raise
204
205 def usage (exit_code=0):
206     if exit_code:
207         f = sys.stderr
208     else:
209         f = sys.stdout
210     print >> f, """Usage: dak show-deferred
211   -h, --help                    show this help and exit.
212   -p, --link-path [path]        override output directory.
213   -d, --deferred-queue [path]   path to the deferred queue
214   """
215     sys.exit(exit_code)
216
217 def init():
218     global Cnf, Options
219     Cnf = utils.get_conf()
220     Arguments = [('h',"help","Show-Deferred::Options::Help"),
221                  ("p","link-path","Show-Deferred::LinkPath","HasArg"),
222                  ("d","deferred-queue","Show-Deferred::DeferredQueue","HasArg")]
223     args = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
224     for i in ["help"]:
225         if not Cnf.has_key("Show-Deferred::Options::%s" % (i)):
226             Cnf["Show-Deferred::Options::%s" % (i)] = ""
227     for i,j in [("DeferredQueue","--deferred-queue")]:
228         if not Cnf.has_key("Show-Deferred::%s" % (i)):
229             print >> sys.stderr, """Show-Deferred::%s is mandatory.
230   set via config file or command-line option %s"""%(i,j)
231
232     Options = Cnf.SubTree("Show-Deferred::Options")
233     if Options["help"]:
234         usage()
235
236     # Initialise database connection
237     DBConn()
238
239     return args
240
241 def main():
242     args = init()
243     if len(args)!=0:
244         usage(1)
245
246     filelist = []
247     for r,d,f  in os.walk(Cnf["Show-Deferred::DeferredQueue"]):
248         filelist += map (lambda x: os.path.join(r,x),
249                          filter(lambda x: x.endswith('.changes'), f))
250     list_uploads(filelist)
251
252     available_changes = set(map(os.path.basename,filelist))
253     if Cnf.has_key("Show-Deferred::LinkPath"):
254         # remove dead links
255         for r,d,f in os.walk(Cnf["Show-Deferred::LinkPath"]):
256             for af in f:
257                 afp = os.path.join(r,af)
258                 if (not os.path.exists(afp) or
259                     (af.endswith('.changes') and af not in available_changes)):
260                     os.unlink(afp)