#!/usr/bin/python

#
#      listwnpp - Check if packages are listed in as WNPP in Debian BTS
#      Copyright (C) 2003  Petter Reinholdtsen <pere@hungry.com>
#
#      Based on popbug by 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
#
#      Might use LDAP instead to access the bugs.
#      <URL:http://people.debian.org/~aba/bts2ldap/>

import sys
import debianbts
import re

use_ldap = 0
debug = 0

if len(sys.argv) > 1:
    pkglistfile = sys.argv[1]
else:
    sys.stderr.write("Error: Must supply file as argument\n")
    sys.exit(1)
    

packageinfo = {}
popcon = open(pkglistfile,'r')
for pkgname in popcon.readlines():
    pkgname = pkgname.strip()
    if len(pkgname) == 0:
        continue
    if debug:
        print "Loading pkg '%s'" % pkgname
    packageinfo[pkgname] = -1

popcon.close()

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

# The entries should look like this:
#  '#178611: RFP: xspringies -- A mass and spring simulation game'
#  '#176096: ITP: amrita -- HTML/XML template library for Ruby'
#  '#154092: O: ubit -- Ubiquitous Brick Interactive Toolkit'
#  '#144327: ITP: libversit -- vCard/vCalendar C interface'
#  '#174071: RFA: cadaver'
wnpp_entry_re = re.compile('#(\S+): (\S+): *(.+) --? *(.+)')

(count, title, hierarchy) = debianbts.get_reports("wnpp", ldap_ok = use_ldap)

totalcount,badcount = 0, 0
if hierarchy is not None:
    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()

        if not status in ('outstanding',
                          'forwarded to upstream software authors'):
            continue

        #print severity, status, `buglist`

        for bugentry in buglist:
            totalcount += 1
            match = wnpp_entry_re.match(bugentry)
            if not match:
                sys.stderr.write("Warning: didn't understand entry: %s\n" % bugentry)
                badcount += 1
                pass
            else:
                bug, type, pkgname, description = match.groups()
                bug = int(bug)
                if debug:
                    print "Testing wnpp entry '%s'" % pkgname
                if packageinfo.has_key(pkgname):
                    bugs.append((bug, type, pkgname, description))
                    if (debug):
                        print "#%s: %s: %s -- %s" % (bug, type,
                                                    pkgname, description)

    print "WNTPP entries: %d (%d with format problem)" % (totalcount, badcount)

    if len(bugs) > 0:
        bugs.sort(lambda a,b: cmp(a[0], b[0]))
        for (bug, type, pkgname, description) in bugs:
            print "#%s: %s: %s -- %s" % (bug, type, pkgname, description)
