]> git.decadent.org.uk Git - dak.git/blob - dak/show_new.py
Merge remote branch 'drkranz/show-new' into merge
[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.dbconn import *
34 from daklib.queue import determine_new, check_valid, Upload, get_policy_queue
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
50
51 ################################################################################
52 ################################################################################
53 ################################################################################
54
55 def html_header(name, filestoexamine):
56     if name.endswith('.changes'):
57         name = ' '.join(name.split('_')[:2])
58     result = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
59 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
60   <head>
61     <meta http-equiv="content-type" content="text/xhtml+xml; charset=utf-8"
62     />
63     <title>%(name)s - Debian NEW package overview</title>
64     <link type="text/css" rel="stylesheet" href="/style.css" />
65     <link rel="shortcut icon" href="http://www.debian.org/favicon.ico" />
66     <script type="text/javascript">
67       //<![CDATA[
68       <!--
69       function toggle(id, initial, display) {
70         var o = document.getElementById(id);
71         toggleObj(o, initial, display);
72       }
73       function show(id, display) {
74         var o = document.getElementById(id);
75         o.style.display = 'table-row-group';
76       }
77       function toggleObj(o, initial, display) {
78         if(! o.style.display)
79           o.style.display = initial;
80         if(o.style.display == display) {
81           o.style.display = "none";
82         } else {
83           o.style.display = display;
84         }
85       }
86       //-->
87       //]]>
88     </script>
89   </head>
90   <body id="NEW-details-page">
91     <div id="logo">
92       <a href="http://www.debian.org/">
93         <img src="http://www.debian.org/logos/openlogo-nd-50.png"
94         alt="debian logo" /></a>
95       <a href="http://www.debian.org/">
96         <img src="http://www.debian.org/Pics/debian.png"
97         alt="Debian Project" /></a>
98     </div>
99     <div id="titleblock">
100       <img src="http://www.debian.org/Pics/red-upperleft.png"
101       id="red-upperleft" alt="corner image"/>
102       <img src="http://www.debian.org/Pics/red-lowerleft.png"
103       id="red-lowerleft" alt="corner image"/>
104       <img src="http://www.debian.org/Pics/red-upperright.png"
105       id="red-upperright" alt="corner image"/>
106       <img src="http://www.debian.org/Pics/red-lowerright.png"
107       id="red-lowerright" alt="corner image"/>
108       <span class="title">
109         Debian NEW package overview for %(name)s
110       </span>
111     </div>
112
113     """%{"name":name}
114
115     # we assume only one source (.dsc) per changes here
116     result += """
117     <div id="menu">
118       <p class="title">Navigation</p>
119       <p><a href="#changes" onclick="show('changes-body')">.changes</a></p>
120       <p><a href="#dsc" onclick="show('dsc-body')">.dsc</a></p>
121       <p><a href="#source-lintian" onclick="show('source-lintian-body')">source lintian</a></p>
122
123 """
124     for fn in filter(lambda x: x.endswith('.deb') or x.endswith('.udeb'),filestoexamine):
125         packagename = fn.split('_')[0]
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(changes_file):
157     changes_file = utils.validate_changes_file_arg(changes_file, 0)
158     if not changes_file:
159         return
160     print "\n" + changes_file
161
162     session = DBConn().session()
163     u = Upload()
164     u.pkg.changes_file = changes_file
165     # We can afoord not to check the signature before loading the changes file
166     # as we've validated it already (otherwise it couldn't be in new)
167     # and we can more quickly skip over already processed files this way
168     u.load_changes(changes_file)
169
170     origchanges = os.path.abspath(u.pkg.changes_file)
171
172     # Still be cautious in case paring the changes file went badly
173     if u.pkg.changes.has_key('source') and u.pkg.changes.has_key('version'):
174         htmlname = u.pkg.changes["source"] + "_" + u.pkg.changes["version"] + ".html"
175         htmlfile = os.path.join(cnf["Show-New::HTMLPath"], htmlname)
176         htmlfiles_to_process.append(htmlfile)
177     else:
178         # Changes file was bad
179         print "Changes file %s missing source or version field" % changes_file
180         session.close()
181         return
182
183     # Have we already processed this?
184     if os.path.exists(htmlfile) and \
185         os.stat(htmlfile).st_mtime > os.stat(origchanges).st_mtime:
186             sources.append(htmlname)
187             session.close()
188             return (PROC_STATUS_SUCCESS, '%s already up-to-date' % htmlfile)
189
190     # Now we'll load the fingerprint
191     (u.pkg.changes["fingerprint"], rejects) = utils.check_signature(changes_file, session=session)
192     new_queue = get_policy_queue('new', session );
193     u.pkg.directory = new_queue.path
194     u.update_subst()
195     files = u.pkg.files
196     changes = u.pkg.changes
197     sources.append(htmlname)
198
199     for deb_filename, f in files.items():
200         if deb_filename.endswith(".udeb") or deb_filename.endswith(".deb"):
201             u.binary_file_checks(deb_filename, session)
202             u.check_binary_against_db(deb_filename, session)
203         else:
204             u.source_file_checks(deb_filename, session)
205             u.check_source_against_db(deb_filename, session)
206     u.pkg.changes["suite"] = u.pkg.changes["distribution"]
207
208     new, byhand = determine_new(u.pkg.changes_file, u.pkg.changes, files, 0, dsc=u.pkg.dsc, session=session)
209
210     outfile = open(os.path.join(cnf["Show-New::HTMLPath"],htmlname),"w")
211
212     filestoexamine = []
213     for pkg in new.keys():
214         for fn in new[pkg]["files"]:
215             filestoexamine.append(fn)
216
217     print >> outfile, html_header(changes["source"], filestoexamine)
218
219     check_valid(new, session)
220     distribution = changes["distribution"].keys()[0]
221     print >> outfile, examine_package.display_changes(distribution, changes_file)
222
223     for fn in filter(lambda fn: fn.endswith(".dsc"), filestoexamine):
224         print >> outfile, examine_package.check_dsc(distribution, fn, session)
225     for fn in filter(lambda fn: fn.endswith(".deb") or fn.endswith(".udeb"), filestoexamine):
226         print >> outfile, examine_package.check_deb(distribution, fn, session)
227
228     print >> outfile, html_footer()
229
230     outfile.close()
231     session.close()
232
233     htmlfiles_to_process.remove(htmlfile)
234     return (PROC_STATUS_SUCCESS, '%s already updated' % htmlfile)
235
236 ################################################################################
237
238 def usage (exit_code=0):
239     print """Usage: dak show-new [OPTION]... [CHANGES]...
240   -h, --help                show this help and exit.
241   -p, --html-path [path]    override output directory.
242   """
243     sys.exit(exit_code)
244
245 ################################################################################
246
247 def init(session):
248     global cnf, Options
249
250     cnf = Config()
251
252     Arguments = [('h',"help","Show-New::Options::Help"),
253                  ("p","html-path","Show-New::HTMLPath","HasArg")]
254
255     for i in ["help"]:
256         if not cnf.has_key("Show-New::Options::%s" % (i)):
257             cnf["Show-New::Options::%s" % (i)] = ""
258
259     changes_files = apt_pkg.ParseCommandLine(cnf.Cnf,Arguments,sys.argv)
260     if len(changes_files) == 0:
261         new_queue = get_policy_queue('new', session );
262         changes_files = utils.get_changes_files(new_queue.path)
263
264     Options = cnf.SubTree("Show-New::Options")
265
266     if Options["help"]:
267         usage()
268
269     return changes_files
270
271
272 ################################################################################
273 ################################################################################
274
275 def main():
276     session = DBConn().session()
277     changes_files = init(session)
278
279     examine_package.use_html=1
280
281     pool = DakProcessPool()
282     p = pool.map_async(do_pkg, changes_files)
283     pool.close()
284     try:
285         p.get(timeout=600)
286     except TimeoutError:
287         for htmlfile in htmlfiles_to_process:
288             with open(htmlfile, "w") as fd:
289                 fd.write("Timed out while processing")
290
291     files = set(os.listdir(cnf["Show-New::HTMLPath"]))
292     to_delete = filter(lambda x: x.endswith(".html"), files.difference(set(sources)))
293     for f in to_delete:
294         os.remove(os.path.join(cnf["Show-New::HTMLPath"],f))
295
296 ################################################################################
297
298 if __name__ == '__main__':
299     main()