]> git.decadent.org.uk Git - dak.git/blob - dak/show_new.py
dak/show_new.py: update for multi-archive changes
[dak.git] / dak / show_new.py
1 #!/usr/bin/env python
2
3 """ Output html for packages in NEW """
4 # Copyright (C) 2007, 2009 Joerg Jaspert <joerg@debian.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21
22 # <elmo> I'm James Troup, long term source of all evil in Debian. you may
23 #        know me from such debian-devel-announce gems as "Serious
24 #        Problems With ...."
25
26 ################################################################################
27
28 from copy import copy
29 import os, sys, time
30 import apt_pkg
31 import examine_package
32
33 from daklib import policy
34 from daklib.dbconn import *
35 from daklib import utils
36 from daklib.regexes import re_source_ext
37 from daklib.config import Config
38 from daklib import daklog
39 from daklib.changesutils import *
40 from daklib.dakmultiprocessing import DakProcessPool, PROC_STATUS_SUCCESS, PROC_STATUS_SIGNALRAISED
41 from multiprocessing import Manager, TimeoutError
42
43 # Globals
44 Cnf = None
45 Options = None
46 manager = Manager()
47 sources = manager.list()
48 htmlfiles_to_process = manager.list()
49 timeout_str = "Timed out while processing"
50
51
52 ################################################################################
53 ################################################################################
54 ################################################################################
55
56 def html_header(name, missing):
57     if name.endswith('.changes'):
58         name = ' '.join(name.split('_')[:2])
59     result = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
60 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
61   <head>
62     <meta http-equiv="content-type" content="text/xhtml+xml; charset=utf-8"
63     />
64     <title>%(name)s - Debian NEW package overview</title>
65     <link type="text/css" rel="stylesheet" href="/style.css" />
66     <link rel="shortcut icon" href="http://www.debian.org/favicon.ico" />
67     <script type="text/javascript">
68       //<![CDATA[
69       <!--
70       function toggle(id, initial, display) {
71         var o = document.getElementById(id);
72         toggleObj(o, initial, display);
73       }
74       function show(id, display) {
75         var o = document.getElementById(id);
76         o.style.display = 'table-row-group';
77       }
78       function toggleObj(o, initial, display) {
79         if(! o.style.display)
80           o.style.display = initial;
81         if(o.style.display == display) {
82           o.style.display = "none";
83         } else {
84           o.style.display = display;
85         }
86       }
87       //-->
88       //]]>
89     </script>
90   </head>
91   <body id="NEW-details-page">
92     <div id="logo">
93       <a href="http://www.debian.org/">
94         <img src="http://www.debian.org/logos/openlogo-nd-50.png"
95         alt="debian logo" /></a>
96       <a href="http://www.debian.org/">
97         <img src="http://www.debian.org/Pics/debian.png"
98         alt="Debian Project" /></a>
99     </div>
100     <div id="titleblock">
101       <img src="http://www.debian.org/Pics/red-upperleft.png"
102       id="red-upperleft" alt="corner image"/>
103       <img src="http://www.debian.org/Pics/red-lowerleft.png"
104       id="red-lowerleft" alt="corner image"/>
105       <img src="http://www.debian.org/Pics/red-upperright.png"
106       id="red-upperright" alt="corner image"/>
107       <img src="http://www.debian.org/Pics/red-lowerright.png"
108       id="red-lowerright" alt="corner image"/>
109       <span class="title">
110         Debian NEW package overview for %(name)s
111       </span>
112     </div>
113
114     """%{"name":name}
115
116     # we assume only one source (.dsc) per changes here
117     result += """
118     <div id="menu">
119       <p class="title">Navigation</p>
120       <p><a href="#changes" onclick="show('changes-body')">.changes</a></p>
121       <p><a href="#dsc" onclick="show('dsc-body')">.dsc</a></p>
122       <p><a href="#source-lintian" onclick="show('source-lintian-body')">source lintian</a></p>
123
124 """
125     for binarytype, packagename in filter(lambda m: m[0] in ('deb', 'udeb'), missing):
126         result += """
127         <p class="subtitle">%(pkg)s</p>
128         <p><a href="#binary-%(pkg)s-control" onclick="show('binary-%(pkg)s-control-body')">control file</a></p>
129         <p><a href="#binary-%(pkg)s-lintian" onclick="show('binary-%(pkg)s-lintian-body')">binary lintian</a></p>
130         <p><a href="#binary-%(pkg)s-contents" onclick="show('binary-%(pkg)s-contents-body')">.deb contents</a></p>
131         <p><a href="#binary-%(pkg)s-copyright" onclick="show('binary-%(pkg)s-copyright-body')">copyright</a></p>
132         <p><a href="#binary-%(pkg)s-file-listing" onclick="show('binary-%(pkg)s-file-listing-body')">file listing</a></p>
133
134 """%{"pkg":packagename}
135     result += "    </div>"
136     return result
137
138 def html_footer():
139     result = """    <p class="validate">Timestamp: %s (UTC)</p>
140 """% (time.strftime("%d.%m.%Y / %H:%M:%S", time.gmtime()))
141     result += """    <p><a href="http://validator.w3.org/check?uri=referer">
142       <img src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"
143       style="border: none; height: 31px; width: 88px" /></a>
144     <a href="http://jigsaw.w3.org/css-validator/check/referer">
145       <img src="http://jigsaw.w3.org/css-validator/images/vcss"
146       alt="Valid CSS!" style="border: none; height: 31px; width: 88px" /></a>
147     </p>
148   </body>
149 </html>
150 """
151     return result
152
153 ################################################################################
154
155
156 def do_pkg(upload_id):
157     session = DBConn().session()
158     upload = session.query(PolicyQueueUpload).filter_by(id=upload_id).one()
159
160     queue = upload.policy_queue
161     changes = upload.changes
162
163     origchanges = os.path.join(queue.path, changes.changesname)
164     print origchanges
165
166     htmlname = "{0}_{1}.html".format(changes.source, changes.version)
167     htmlfile = os.path.join(cnf['Show-New::HTMLPath'], htmlname)
168
169     # Have we already processed this?
170     if False and os.path.exists(htmlfile) and \
171         os.stat(htmlfile).st_mtime > time.mktime(changes.created.timetuple()):
172             with open(htmlfile, "r") as fd:
173                 if fd.read() != timeout_str:
174                     sources.append(htmlname)
175                     return (PROC_STATUS_SUCCESS,
176                             '%s already up-to-date' % htmlfile)
177
178     # Go, process it... Now!
179     htmlfiles_to_process.append(htmlfile)
180     sources.append(htmlname)
181
182     with open(htmlfile, 'w') as outfile:
183       with policy.UploadCopy(upload) as upload_copy:
184         handler = policy.PolicyQueueUploadHandler(upload, session)
185         missing = [ (o['type'], o['package']) for o in handler.missing_overrides() ]
186         distribution = changes.distribution
187
188         print >>outfile, html_header(changes.source, missing)
189         print >>outfile, examine_package.display_changes(distribution, origchanges)
190
191         if upload.source is not None and ('dsc', upload.source.source) in missing:
192             fn = os.path.join(upload_copy.directory, upload.source.poolfile.basename)
193             print >>outfile, examine_package.check_dsc(distribution, fn, session)
194         for binary in upload.binaries:
195             if (binary.binarytype, binary.package) not in missing:
196                 continue
197             fn = os.path.join(upload_copy.directory, binary.poolfile.basename)
198             print >>outfile, examine_package.check_deb(distribution, fn, session)
199
200         print >>outfile, html_footer()
201
202     session.close()
203     htmlfiles_to_process.remove(htmlfile)
204     return (PROC_STATUS_SUCCESS, '{0} already updated'.format(htmlfile))
205
206 ################################################################################
207
208 def usage (exit_code=0):
209     print """Usage: dak show-new [OPTION]... [CHANGES]...
210   -h, --help                show this help and exit.
211   -p, --html-path [path]    override output directory.
212   """
213     sys.exit(exit_code)
214
215 ################################################################################
216
217 def init(session):
218     global cnf, Options
219
220     cnf = Config()
221
222     Arguments = [('h',"help","Show-New::Options::Help"),
223                  ("p","html-path","Show-New::HTMLPath","HasArg")]
224
225     for i in ["help"]:
226         if not cnf.has_key("Show-New::Options::%s" % (i)):
227             cnf["Show-New::Options::%s" % (i)] = ""
228
229     changesnames = apt_pkg.parse_commandline(cnf.Cnf,Arguments,sys.argv)
230     Options = cnf.subtree("Show-New::Options")
231
232     if Options["help"]:
233         usage()
234
235     uploads = session.query(PolicyQueueUpload) \
236         .join(PolicyQueueUpload.policy_queue).filter(PolicyQueue.queue_name == 'new') \
237         .join(PolicyQueueUpload.changes).order_by(DBChange.source)
238
239     if len(changesnames) > 0:
240         uploads = uploads.filter(DBChange.changesname.in_(changesnames))
241
242     return uploads
243
244
245 ################################################################################
246 ################################################################################
247
248 def main():
249     session = DBConn().session()
250     upload_ids = [ u.id for u in init(session) ]
251     session.close()
252
253     examine_package.use_html=1
254
255     pool = DakProcessPool(processes=5)
256     p = pool.map_async(do_pkg, upload_ids)
257     pool.close()
258
259     p.wait(timeout=600)
260     for htmlfile in htmlfiles_to_process:
261         with open(htmlfile, "w") as fd:
262             fd.write(timeout_str)
263
264     files = set(os.listdir(cnf["Show-New::HTMLPath"]))
265     to_delete = filter(lambda x: x.endswith(".html"), files.difference(set(sources)))
266     for f in to_delete:
267         os.remove(os.path.join(cnf["Show-New::HTMLPath"],f))
268
269 ################################################################################
270
271 if __name__ == '__main__':
272     main()