#!/usr/bin/python

#
#      popbugs - Find bugs in commonly used packages
#      Copyright (C) 2001  Matt Zimmerman <mdz@debian.org>
#
#      This program is free software; you can redistribute it and/or
#      modify it under the terms of the GNU General Public License as
#      published by the Free Software Foundation; either version 2 of
#      the License, or (at your option) any later version.
#
#      This program is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#      GNU General Public License for more details.
#
#      You should have received a copy of the GNU General Public
#      License along with this program; if not, write to the Free
#      Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#      MA 02111-1307 USA
#

import sys
import debianbts
import re

popconfile = '/var/log/popularity-contest'
maxbugs = maxbugs_step = 1

# Not running in interactive mode
interactive = 0

# 2 - serious
# 3 - important
# 4 - normal
# 6 - minor
# 7 - wishlist
severity_limit = 2

if len(sys.argv) > 1:
    popconfile = sys.argv[1]

packageatime = []
popcon = open(popconfile,'r')
for line in popcon.readlines():
    if len(line) == 0 or line.find(':') != -1:
        continue

    fields = line.split()
    if len(fields) != 4:
        continue
    if fields[0] == 'POPULARITY-CONTEST-0' or fields[0] == 'END-POPULARITY-CONTEST-0':
        continue
    
    (atime, ctime, package, pathname) = fields
    if pathname == '<NOFILES>' or pathname == '<RECENT-CTIME>':
        continue
    packageatime.append((package,atime))
    
popcon.close()

categoryre = re.compile('(\S+) .* - (\S+)')

packageatime.sort(lambda a,b: -cmp(a[1],b[1]))

bugs_so_far = 0
for (package,atime) in packageatime:
    #print "%s..." % package
    
    (count, title, hierarchy) = debianbts.get_reports(package, ldap_ok = 0)
    
    if hierarchy == None:
        continue
    
    bugs = []
    for (categorydesc,buglist) in hierarchy:
        
        if categorydesc == 'No reports found!':
            continue
        
        match = categoryre.match(categorydesc)
        
        if not match:
            sys.stderr.write("Warning: didn't understand: %s\n" % categorydesc)
            continue

        severity,status = match.groups()

        #print severity, status, `buglist`
        if not status in ('outstanding',
                      'forwarded to upstream software authors'):
            continue

        if severity == 'NMU':
            severity = 'fixed'

        numeric_severity = debianbts.SEVLIST.index(severity.lower())

        if numeric_severity > severity_limit:
            continue
        
        bugs.append((numeric_severity,buglist))

    if len(bugs) == 0:
        continue

    bugs.sort(lambda a,b: -cmp(a[0], b[0]))
    for (numeric_severity,buglist) in bugs:
        for bug in buglist:
            print '%s [%s]: %s' % (package,
                                   debianbts.SEVLIST[numeric_severity],
                                   bug)
        
            bugs_so_far += 1
            
            if 0 != interactive and bugs_so_far >= maxbugs:
                print "Want to see more? [Y/n] ",
                answer = sys.stdin.readline()
                if len(answer) == 0 or answer[0].lower() == 'n':
                    sys.exit(0)
                maxbugs += maxbugs_step
