]> git.decadent.org.uk Git - dak.git/blob - dak/show_new.py
af1b8a6480b0b58ea6c1af99a90ea94fffa732b6
[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.config import Config
37 from daklib import daklog
38 from daklib.dakmultiprocessing import DakProcessPool, PROC_STATUS_SUCCESS, PROC_STATUS_SIGNALRAISED
39 from multiprocessing import Manager, TimeoutError
40
41 # Globals
42 Cnf = None
43 Options = None
44 manager = Manager()
45 sources = manager.list()
46 htmlfiles_to_process = manager.list()
47 timeout_str = "Timed out while processing"
48
49
50 ################################################################################
51 ################################################################################
52 ################################################################################
53
54 def html_header(name, missing):
55     if name.endswith('.changes'):
56         name = ' '.join(name.split('_')[:2])
57     result = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
58 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
59   <head>
60     <meta http-equiv="content-type" content="text/xhtml+xml; charset=utf-8"
61     />
62     <title>%(name)s - Debian NEW package overview</title>
63     <link type="text/css" rel="stylesheet" href="/style.css" />
64     <link rel="shortcut icon" href="https://www.debian.org/favicon.ico" />
65     <script type="text/javascript">
66       //<![CDATA[
67       <!--
68       function toggle(id, initial, display) {
69         var o = document.getElementById(id);
70         toggleObj(o, initial, display);
71       }
72       function show(id, display) {
73         var o = document.getElementById(id);
74         o.style.display = 'table-row-group';
75       }
76       function toggleObj(o, initial, display) {
77         if(! o.style.display)
78           o.style.display = initial;
79         if(o.style.display == display) {
80           o.style.display = "none";
81         } else {
82           o.style.display = display;
83         }
84       }
85       //-->
86       //]]>
87     </script>
88   </head>
89   <body id="NEW-details-page">
90     <div id="logo">
91       <a href="https://www.debian.org/">
92         <img src="https://www.debian.org/logos/openlogo-nd-50.png"
93         alt="debian logo" /></a>
94       <a href="https://www.debian.org/">
95         <img src="https://www.debian.org/Pics/debian.png"
96         alt="Debian Project" /></a>
97     </div>
98     <div id="titleblock">
99       <img src="https://www.debian.org/Pics/red-upperleft.png"
100       id="red-upperleft" alt="corner image"/>
101       <img src="https://www.debian.org/Pics/red-lowerleft.png"
102       id="red-lowerleft" alt="corner image"/>
103       <img src="https://www.debian.org/Pics/red-upperright.png"
104       id="red-upperright" alt="corner image"/>
105       <img src="https://www.debian.org/Pics/red-lowerright.png"
106       id="red-lowerright" alt="corner image"/>
107       <span class="title">
108         Debian NEW package overview for %(name)s
109       </span>
110     </div>
111
112     """%{"name":name}
113
114     # we assume only one source (.dsc) per changes here
115     result += """
116     <div id="menu">
117       <p class="title">Navigation</p>
118       <p><a href="#changes" onclick="show('changes-body')">.changes</a></p>
119       <p><a href="#dsc" onclick="show('dsc-body')">.dsc</a></p>
120       <p><a href="#source-lintian" onclick="show('source-lintian-body')">source lintian</a></p>
121
122 """
123     for binarytype, packagename in filter(lambda m: m[0] in ('deb', 'udeb'), missing):
124         result += """
125         <p class="subtitle">%(pkg)s</p>
126         <p><a href="#binary-%(pkg)s-control" onclick="show('binary-%(pkg)s-control-body')">control file</a></p>
127         <p><a href="#binary-%(pkg)s-lintian" onclick="show('binary-%(pkg)s-lintian-body')">binary lintian</a></p>
128         <p><a href="#binary-%(pkg)s-contents" onclick="show('binary-%(pkg)s-contents-body')">.deb contents</a></p>
129         <p><a href="#binary-%(pkg)s-copyright" onclick="show('binary-%(pkg)s-copyright-body')">copyright</a></p>
130         <p><a href="#binary-%(pkg)s-file-listing" onclick="show('binary-%(pkg)s-file-listing-body')">file listing</a></p>
131
132 """%{"pkg":packagename}
133     result += "    </div>"
134     return result
135
136 def html_footer():
137     result = """    <p class="validate">Timestamp: %s (UTC)</p>
138 """% (time.strftime("%d.%m.%Y / %H:%M:%S", time.gmtime()))
139     result += "</body></html>"
140     return result
141
142 ################################################################################
143
144
145 def do_pkg(upload_id):
146     cnf = Config()
147
148     session = DBConn().session()
149     upload = session.query(PolicyQueueUpload).filter_by(id=upload_id).one()
150
151     queue = upload.policy_queue
152     changes = upload.changes
153
154     origchanges = os.path.join(queue.path, changes.changesname)
155     print origchanges
156
157     htmlname = "{0}_{1}.html".format(changes.source, changes.version)
158     htmlfile = os.path.join(cnf['Show-New::HTMLPath'], htmlname)
159
160     # Have we already processed this?
161     if os.path.exists(htmlfile) and \
162         os.stat(htmlfile).st_mtime > time.mktime(changes.created.timetuple()):
163             with open(htmlfile, "r") as fd:
164                 if fd.read() != timeout_str:
165                     sources.append(htmlname)
166                     return (PROC_STATUS_SUCCESS,
167                             '%s already up-to-date' % htmlfile)
168
169     # Go, process it... Now!
170     htmlfiles_to_process.append(htmlfile)
171     sources.append(htmlname)
172
173     group = cnf.get('Dinstall::UnprivGroup') or None
174
175     with open(htmlfile, 'w') as outfile:
176       with policy.UploadCopy(upload, group=group) as upload_copy:
177         handler = policy.PolicyQueueUploadHandler(upload, session)
178         missing = [ (o['type'], o['package']) for o in handler.missing_overrides() ]
179         distribution = changes.distribution
180
181         print >>outfile, html_header(changes.source, missing)
182         print >>outfile, examine_package.display_changes(distribution, origchanges)
183
184         if upload.source is not None and ('dsc', upload.source.source) in missing:
185             fn = os.path.join(upload_copy.directory, upload.source.poolfile.basename)
186             print >>outfile, examine_package.check_dsc(distribution, fn, session)
187         for binary in upload.binaries:
188             if (binary.binarytype, binary.package) not in missing:
189                 continue
190             fn = os.path.join(upload_copy.directory, binary.poolfile.basename)
191             print >>outfile, examine_package.check_deb(distribution, fn, session)
192
193         print >>outfile, html_footer()
194
195     session.close()
196     htmlfiles_to_process.remove(htmlfile)
197     return (PROC_STATUS_SUCCESS, '{0} already updated'.format(htmlfile))
198
199 ################################################################################
200
201 def usage (exit_code=0):
202     print """Usage: dak show-new [OPTION]... [CHANGES]...
203   -h, --help                show this help and exit.
204   -p, --html-path [path]    override output directory.
205   """
206     sys.exit(exit_code)
207
208 ################################################################################
209
210 def init(session):
211     global cnf, Options
212
213     cnf = Config()
214
215     Arguments = [('h',"help","Show-New::Options::Help"),
216                  ("p","html-path","Show-New::HTMLPath","HasArg"),
217                  ('q','queue','Show-New::Options::Queue','HasArg')]
218
219     for i in ["help"]:
220         if not cnf.has_key("Show-New::Options::%s" % (i)):
221             cnf["Show-New::Options::%s" % (i)] = ""
222
223     changesnames = apt_pkg.parse_commandline(cnf.Cnf,Arguments,sys.argv)
224     Options = cnf.subtree("Show-New::Options")
225
226     if Options["help"]:
227         usage()
228
229     queue_names = Options.find('Queue', 'new').split(',')
230     uploads = session.query(PolicyQueueUpload) \
231         .join(PolicyQueueUpload.policy_queue).filter(PolicyQueue.queue_name.in_(queue_names)) \
232         .join(PolicyQueueUpload.changes).order_by(DBChange.source)
233
234     if len(changesnames) > 0:
235         uploads = uploads.filter(DBChange.changesname.in_(changesnames))
236
237     return uploads
238
239
240 ################################################################################
241 ################################################################################
242
243 def main():
244     examine_package.use_html = True
245     pool = DakProcessPool(processes=5)
246
247     session = DBConn().session()
248     upload_ids = [ u.id for u in init(session) ]
249     session.close()
250
251     for upload_id in upload_ids:
252         pool.apply_async(do_pkg, [upload_id])
253     pool.close()
254
255     #p.wait(timeout=600)
256     pool.join()
257     for htmlfile in htmlfiles_to_process:
258         with open(htmlfile, "w") as fd:
259             fd.write(timeout_str)
260
261     files = set(os.listdir(cnf["Show-New::HTMLPath"]))
262     to_delete = filter(lambda x: x.endswith(".html"), files.difference(set(sources)))
263     for f in to_delete:
264         os.remove(os.path.join(cnf["Show-New::HTMLPath"],f))
265
266 ################################################################################
267
268 if __name__ == '__main__':
269     main()