#!/usr/bin/env python
#
# Search the Debian bug tracking system for bugs about a package
# containing a particular string.
#
# Documentation regarding the Debian bug tracker SOAP inteface is at:
# http://wiki.debian.org/DebbugsSoapInterface
#
# Copyright 2008 Canonical, Ltd
# Author: Brian Murray <brian@ubuntu.com>
# Licensed under the GNU General Public License, version 3.

import SOAPpy
import re
import sys

url = 'http://bugs.debian.org/cgi-bin/soap.cgi'
namespace = 'Debbugs/SOAP'
server = SOAPpy.SOAPProxy(url, namespace)

# Check if the amount of arguments is correct
if len(sys.argv) is not 3 or sys.argv[1] in ('help', '-h', '--help'):
    print 'Usage: %s <package name> <search_string>' % sys.argv[0]
    sys.exit(1)
	
package = sys.argv[1]
string = sys.argv[2]

def getBugs(*args):
        result = server.get_bugs(*args)
        return result

def getBugLog(*args):
        result = server.get_bug_log(*args)
        return result

buglist = getBugs("package", package)

for bug in range(len(buglist)):
    buglog = getBugLog(buglist[bug])
    for bodies in range(len(buglog)):
        if re.search(string, buglog[bodies]['body'], re.MULTILINE): 
            print 'http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s contains the string %s' % (buglist[bug], string) 
            # only return each bug report one time
            break
