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