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