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