]> git.decadent.org.uk Git - dak.git/blob - dak/queue_report.py
Globally remove trailing semi-colon damage.
[dak.git] / dak / queue_report.py
1 #!/usr/bin/env python
2
3 # Produces a report on NEW and BYHAND packages
4 # Copyright (C) 2001, 2002, 2003, 2005  James Troup <james@nocrew.org>
5 # $Id: helena,v 1.6 2005-11-15 09:50:32 ajt Exp $
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 # <o-o> XP runs GCC, XFREE86, SSH etc etc,.,, I feel almost like linux....
24 # <o-o> I am very confident that I can replicate any Linux application on XP
25 # <willy> o-o: *boggle*
26 # <o-o> building from source.
27 # <o-o> Viiru: I already run GIMP under XP
28 # <willy> o-o: why do you capitalise the names of all pieces of software?
29 # <o-o> willy: because I want the EMPHASIZE them....
30 # <o-o> grr s/the/to/
31 # <willy> o-o: it makes you look like ZIPPY the PINHEAD
32 # <o-o> willy: no idea what you are talking about.
33 # <willy> o-o: do some research
34 # <o-o> willy: for what reason?
35
36 ################################################################################
37
38 import copy, glob, os, stat, sys, time
39 import apt_pkg
40 import katie, utils
41 import encodings.utf_8, encodings.latin_1, string
42
43 Cnf = None
44 Katie = None
45 direction = []
46 row_number = 0
47
48 ################################################################################
49
50 def usage(exit_code=0):
51     print """Usage: helena
52 Prints a report of packages in queue directories (usually new and byhand).
53
54   -h, --help                show this help and exit.
55   -n, --new                 produce html-output
56   -s, --sort=key            sort output according to key, see below.
57   -a, --age=key             if using sort by age, how should time be treated?
58                             If not given a default of hours will be used.
59
60      Sorting Keys: ao=age,   oldest first.   an=age,   newest first.
61                    na=name,  ascending       nd=name,  descending
62                    nf=notes, first           nl=notes, last
63
64      Age Keys: m=minutes, h=hours, d=days, w=weeks, o=months, y=years
65      
66 """
67     sys.exit(exit_code)
68
69 ################################################################################
70
71 def plural(x):
72     if x > 1:
73         return "s"
74     else:
75         return ""
76
77 ################################################################################
78
79 def time_pp(x):
80     if x < 60:
81         unit="second"
82     elif x < 3600:
83         x /= 60
84         unit="minute"
85     elif x < 86400:
86         x /= 3600
87         unit="hour"
88     elif x < 604800:
89         x /= 86400
90         unit="day"
91     elif x < 2419200:
92         x /= 604800
93         unit="week"
94     elif x < 29030400:
95         x /= 2419200
96         unit="month"
97     else:
98         x /= 29030400
99         unit="year"
100     x = int(x)
101     return "%s %s%s" % (x, unit, plural(x))
102
103 ################################################################################
104
105 def sg_compare (a, b):
106     a = a[1]
107     b = b[1]
108     """Sort by have note, time of oldest upload."""
109     # Sort by have note
110     a_note_state = a["note_state"]
111     b_note_state = b["note_state"]
112     if a_note_state < b_note_state:
113         return -1
114     elif a_note_state > b_note_state:
115         return 1
116
117     # Sort by time of oldest upload
118     return cmp(a["oldest"], b["oldest"])
119
120 ############################################################
121
122 def sortfunc(a,b):
123      for sorting in direction:
124          (sortkey, way, time) = sorting
125          ret = 0
126          if time == "m":
127              x=int(a[sortkey]/60)
128              y=int(b[sortkey]/60)
129          elif time == "h":
130              x=int(a[sortkey]/3600)
131              y=int(b[sortkey]/3600)
132          elif time == "d":
133              x=int(a[sortkey]/86400)
134              y=int(b[sortkey]/86400)
135          elif time == "w":
136              x=int(a[sortkey]/604800)
137              y=int(b[sortkey]/604800)
138          elif time == "o":
139              x=int(a[sortkey]/2419200)
140              y=int(b[sortkey]/2419200)
141          elif time == "y":
142              x=int(a[sortkey]/29030400)
143              y=int(b[sortkey]/29030400)
144          else:
145              x=a[sortkey]
146              y=b[sortkey]
147          if x < y:
148              ret = -1
149          elif x > y:
150              ret = 1
151          if ret != 0:
152              if way < 0:
153                  ret = ret*-1
154              return ret
155      return 0
156
157 ############################################################
158
159 def header():
160     print """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
161         <html><head><meta http-equiv="Content-Type" content="text/html; charset=iso8859-1">
162         <title>Debian NEW and BYHAND Packages</title>
163         <link type="text/css" rel="stylesheet" href="style.css">
164         <link rel="shortcut icon" href="http://www.debian.org/favicon.ico">
165         </head>
166         <body>
167         <div align="center">
168         <a href="http://www.debian.org/">
169      <img src="http://www.debian.org/logos/openlogo-nd-50.png" border="0" hspace="0" vspace="0" alt=""></a>
170         <a href="http://www.debian.org/">
171      <img src="http://www.debian.org/Pics/debian.png" border="0" hspace="0" vspace="0" alt="Debian Project"></a>
172         </div>
173         <br />
174         <table class="reddy" width="100%">
175         <tr>
176         <td class="reddy">
177     <img src="http://www.debian.org/Pics/red-upperleft.png" align="left" border="0" hspace="0" vspace="0"
178      alt="" width="15" height="16"></td>
179         <td rowspan="2" class="reddy">Debian NEW and BYHAND Packages</td>
180         <td class="reddy">
181     <img src="http://www.debian.org/Pics/red-upperright.png" align="right" border="0" hspace="0" vspace="0"
182      alt="" width="16" height="16"></td>
183         </tr>
184         <tr>
185         <td class="reddy">
186     <img src="http://www.debian.org/Pics/red-lowerleft.png" align="left" border="0" hspace="0" vspace="0"
187      alt="" width="16" height="16"></td>
188         <td class="reddy">
189     <img src="http://www.debian.org/Pics/red-lowerright.png" align="right" border="0" hspace="0" vspace="0"
190      alt="" width="15" height="16"></td>
191         </tr>
192         </table>
193         """
194
195 def footer():
196     print "<p class=\"validate\">Timestamp: %s (UTC)</p>" % (time.strftime("%d.%m.%Y / %H:%M:%S", time.gmtime()))
197     print "<hr><p>Hint: Age is the youngest upload of the package, if there is more than one version.</p>"
198     print "<p>You may want to look at <a href=\"http://ftp-master.debian.org/REJECT-FAQ.html\">the REJECT-FAQ</a> for possible reasons why one of the above packages may get rejected.</p>"
199     print """<a href="http://validator.w3.org/check?uri=referer">
200     <img border="0" src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" height="31" width="88"></a>
201         <a href="http://jigsaw.w3.org/css-validator/check/referer">
202     <img border="0" src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"
203      height="31" width="88"></a>
204     """
205     print "</body></html>"
206
207 def table_header(type):
208     print "<h1>Summary for: %s</h1>" % (type)
209     print """<center><table border="0">
210         <tr>
211           <th align="center">Package</th>
212           <th align="center">Version</th>
213           <th align="center">Arch</th>
214           <th align="center">Distribution</th>
215           <th align="center">Age</th>
216           <th align="center">Maintainer</th>
217           <th align="center">Closes</th>
218         </tr>
219         """
220
221 def table_footer(type, source_count, total_count):
222     print "</table></center><br>\n"
223     print "<p class=\"validate\">Package count in <b>%s</b>: <i>%s</i>\n" % (type, source_count)
224     print "<br>Total Package count: <i>%s</i></p>\n" % (total_count)
225
226 def force_to_latin(s):
227     """Forces a string to Latin-1."""
228     latin1_s = unicode(s,'utf-8')
229     return latin1_s.encode('iso8859-1', 'replace')
230
231
232 def table_row(source, version, arch, last_mod, maint, distribution, closes):
233
234     global row_number
235
236     if row_number % 2 != 0:
237         print "<tr class=\"even\">"
238     else:
239         print "<tr class=\"odd\">"
240
241     tdclass = "sid"
242     for dist in distribution:
243         if dist == "experimental":
244             tdclass = "exp"
245     print "<td valign=\"top\" class=\"%s\">%s</td>" % (tdclass, source)
246     print "<td valign=\"top\" class=\"%s\">" % (tdclass)
247     for vers in version.split():
248         print "%s<br>" % (vers)
249     print "</td><td valign=\"top\" class=\"%s\">%s</td><td valign=\"top\" class=\"%s\">" % (tdclass, arch, tdclass)
250     for dist in distribution:
251         print "%s<br>" % (dist)
252     print "</td><td valign=\"top\" class=\"%s\">%s</td>" % (tdclass, last_mod)
253     (name, mail) = maint.split(":")
254     name = force_to_latin(name)
255
256     print "<td valign=\"top\" class=\"%s\"><a href=\"http://qa.debian.org/developer.php?login=%s\">%s</a></td>" % (tdclass, mail, name)
257     print "<td valign=\"top\" class=\"%s\">" % (tdclass)
258     for close in closes:
259         print "<a href=\"http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s\">#%s</a><br>" % (close, close)
260     print "</td></tr>"
261     row_number+=1
262     
263 ############################################################
264
265 def process_changes_files(changes_files, type):
266     msg = ""
267     cache = {}
268     # Read in all the .changes files
269     for filename in changes_files:
270         try:
271             Katie.pkg.changes_file = filename
272             Katie.init_vars()
273             Katie.update_vars()
274             cache[filename] = copy.copy(Katie.pkg.changes)
275             cache[filename]["filename"] = filename
276         except:
277             break
278     # Divide the .changes into per-source groups
279     per_source = {}
280     for filename in cache.keys():
281         source = cache[filename]["source"]
282         if not per_source.has_key(source):
283             per_source[source] = {}
284             per_source[source]["list"] = []
285         per_source[source]["list"].append(cache[filename])
286     # Determine oldest time and have note status for each source group
287     for source in per_source.keys():
288         source_list = per_source[source]["list"]
289         first = source_list[0]
290         oldest = os.stat(first["filename"])[stat.ST_MTIME]
291         have_note = 0
292         for d in per_source[source]["list"]:
293             mtime = os.stat(d["filename"])[stat.ST_MTIME]
294             if Cnf.has_key("Helena::Options::New"):
295                 if mtime > oldest:
296                     oldest = mtime
297             else:
298                 if mtime < oldest:
299                     oldest = mtime
300             have_note += (d.has_key("lisa note"))
301         per_source[source]["oldest"] = oldest
302         if not have_note:
303             per_source[source]["note_state"] = 0; # none
304         elif have_note < len(source_list):
305             per_source[source]["note_state"] = 1; # some
306         else:
307             per_source[source]["note_state"] = 2; # all
308     per_source_items = per_source.items()
309     per_source_items.sort(sg_compare)
310
311     entries = []
312     max_source_len = 0
313     max_version_len = 0
314     max_arch_len = 0
315     maintainer = {}
316     maint=""
317     distribution=""
318     closes=""
319     source_exists=""
320     for i in per_source_items:
321         last_modified = time.time()-i[1]["oldest"]
322         source = i[1]["list"][0]["source"]
323         if len(source) > max_source_len:
324             max_source_len = len(source)
325         arches = {}
326         versions = {}
327         for j in i[1]["list"]:
328             if Cnf.has_key("Helena::Options::New"):
329                 try:
330                     (maintainer["maintainer822"], maintainer["maintainer2047"],
331                     maintainer["maintainername"], maintainer["maintaineremail"]) = \
332                     utils.fix_maintainer (j["maintainer"])
333                 except utils.ParseMaintError, msg:
334                     print "Problems while parsing maintainer address\n"
335                     maintainer["maintainername"] = "Unknown"
336                     maintainer["maintaineremail"] = "Unknown"
337                 maint="%s:%s" % (maintainer["maintainername"], maintainer["maintaineremail"])
338                 distribution=j["distribution"].keys()
339                 closes=j["closes"].keys()
340             for arch in j["architecture"].keys():
341                 arches[arch] = ""
342             version = j["version"]
343             versions[version] = ""
344         arches_list = arches.keys()
345         arches_list.sort(utils.arch_compare_sw)
346         arch_list = " ".join(arches_list)
347         version_list = " ".join(versions.keys())
348         if len(version_list) > max_version_len:
349             max_version_len = len(version_list)
350         if len(arch_list) > max_arch_len:
351             max_arch_len = len(arch_list)
352         if i[1]["note_state"]:
353             note = " | [N]"
354         else:
355             note = ""
356         entries.append([source, version_list, arch_list, note, last_modified, maint, distribution, closes])
357
358     # direction entry consists of "Which field, which direction, time-consider" where
359     # time-consider says how we should treat last_modified. Thats all.
360
361     # Look for the options for sort and then do the sort.
362     age = "h"
363     if Cnf.has_key("Helena::Options::Age"):
364         age =  Cnf["Helena::Options::Age"]
365     if Cnf.has_key("Helena::Options::New"):
366     # If we produce html we always have oldest first.
367         direction.append([4,-1,"ao"])
368     else:
369                 if Cnf.has_key("Helena::Options::Sort"):
370                         for i in Cnf["Helena::Options::Sort"].split(","):
371                           if i == "ao":
372                                   # Age, oldest first.
373                                   direction.append([4,-1,age])
374                           elif i == "an":
375                                   # Age, newest first.
376                                   direction.append([4,1,age])
377                           elif i == "na":
378                                   # Name, Ascending.
379                                   direction.append([0,1,0])
380                           elif i == "nd":
381                                   # Name, Descending.
382                                   direction.append([0,-1,0])
383                           elif i == "nl":
384                                   # Notes last.
385                                   direction.append([3,1,0])
386                           elif i == "nf":
387                                   # Notes first.
388                                   direction.append([3,-1,0])
389     entries.sort(lambda x, y: sortfunc(x, y))
390     # Yes, in theory you can add several sort options at the commandline with. But my mind is to small
391     # at the moment to come up with a real good sorting function that considers all the sidesteps you
392     # have with it. (If you combine options it will simply take the last one at the moment).
393     # Will be enhanced in the future.
394
395     if Cnf.has_key("Helena::Options::New"):
396         direction.append([4,1,"ao"])
397         entries.sort(lambda x, y: sortfunc(x, y))
398     # Output for a html file. First table header. then table_footer.
399     # Any line between them is then a <tr> printed from subroutine table_row.
400         if len(entries) > 0:
401             table_header(type.upper())
402             for entry in entries:
403                 (source, version_list, arch_list, note, last_modified, maint, distribution, closes) = entry
404                 table_row(source, version_list, arch_list, time_pp(last_modified), maint, distribution, closes)
405             total_count = len(changes_files)
406             source_count = len(per_source_items)
407             table_footer(type.upper(), source_count, total_count)
408     else:
409     # The "normal" output without any formatting.
410         format="%%-%ds | %%-%ds | %%-%ds%%s | %%s old\n" % (max_source_len, max_version_len, max_arch_len)
411
412         msg = ""
413         for entry in entries:
414             (source, version_list, arch_list, note, last_modified, undef, undef, undef) = entry
415             msg += format % (source, version_list, arch_list, note, time_pp(last_modified))
416
417         if msg:
418             total_count = len(changes_files)
419             source_count = len(per_source_items)
420             print type.upper()
421             print "-"*len(type)
422             print
423             print msg
424             print "%s %s source package%s / %s %s package%s in total." % (source_count, type, plural(source_count), total_count, type, plural(total_count))
425             print
426
427
428 ################################################################################
429
430 def main():
431     global Cnf, Katie
432
433     Cnf = utils.get_conf()
434     Arguments = [('h',"help","Helena::Options::Help"),
435                                  ('n',"new","Helena::Options::New"),
436                  ('s',"sort","Helena::Options::Sort", "HasArg"),
437                  ('a',"age","Helena::Options::Age", "HasArg")]
438     for i in [ "help" ]:
439         if not Cnf.has_key("Helena::Options::%s" % (i)):
440             Cnf["Helena::Options::%s" % (i)] = ""
441
442     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
443
444     Options = Cnf.SubTree("Helena::Options")
445     if Options["Help"]:
446         usage()
447
448     Katie = katie.Katie(Cnf)
449
450     if Cnf.has_key("Helena::Options::New"):
451         header()
452
453     directories = Cnf.ValueList("Helena::Directories")
454     if not directories:
455         directories = [ "byhand", "new" ]
456
457     for directory in directories:
458         changes_files = glob.glob("%s/*.changes" % (Cnf["Dir::Queue::%s" % (directory)]))
459         process_changes_files(changes_files, directory)
460
461     if Cnf.has_key("Helena::Options::New"):
462         footer()
463
464 ################################################################################
465
466 if __name__ == '__main__':
467     main()