Subversion

helios_wp3

[/] [trunk/] [python/] [npmdebbugs.py] - Rev 195 Go to most recent revision

Compare with Previous - Blame


#!/usr/bin/python

# Copyright Olivier Berger <olivier.berger@it-sudparis.eu> + Institut TELECOM, 2008-2009
# developped in the frame of the HELIOS project (http://www.helios-platform.org/)
# License : GNU LGPL V3

# Processes debian bugs to produce their RDF representation in turtle
# format, suitable to be consumed as EvoOnt bom ontology
# (http://www.ifi.uzh.ch/ddis/evoont/2008/11/bom/) and others

# reused most of npmzilla.py from nepomuk/SWIM/Mephisto

import sys
import traceback
from urllib import quote,quote_plus
import re
import email.utils
import string
import time

# import these bits to extend debbugs from btsutils
from btsutils.debbugsloc import debbugsloc

# Import bits from bts-link
from btslinkutils import BTSLConfig as Cnf
import btslinkbts
bts = btslinkbts

from btslinkremote import RemoteBts


BUG_STEP=50

bugtrackers = [
#    ("debian", "debbugs", "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s",3170,513611),
    ("debian", "debbugs", "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%s",501011,513611),
        ]

# Needs to be improved
#email_r = re.compile("([^<]*<)?([^@]*)@([a-zA-Z.\-]+).*")

TEMPLATE_DIR="TEMPLATES"
templates = {}
#for t in ["header", "bug-debian", "reporter", "package", "sys", "comment", "resolution"]:
for t in ["header-debian", "bug-debian", "reporter", "package", "comment", "resolution", "software-debian", "forwarded-debian"]:
    templates[t] = open("%s/%s" % (TEMPLATE_DIR, t)).read()


# character escaping
def escapechars(s, chars=[ '\"', '\'', '\\' ]):
    """Converts invalid characters to corresponding html"""
    return "".join(['\\%s' % c if c in chars else c for c in s])

# Main processing function to convert one bug to turtle format
def parse_bug(spoolbts, id, output=sys.stdout, output_header=False):
    """Parses bugzilla bugs and outputs valid turtle format"""

    bug = spoolbts.get(str(id))
    if not bug :
        return False

    baseurl="http://bugs.debian.org/cgi-bin/bugreport.cgi?bug="

    if output_header:
        print >>output, templates["header-debian"]
    params = {}
    bug_id=bug.getBug()
    url = "%s%s" % (baseurl, bug_id)
    params["bug_id"] = bug_id
    params["url"] = url
    params["bugzilla_url"] = url
    short_desc = bug.getSummary()
    params["short_desc"] = escapechars(short_desc.encode("utf-8"))
    params["creation_ts"] = time.strftime('%Y-%m-%dT%H:%M:%S',time.gmtime(bug.getDate()))
    params["product"] = ''
    params["sys"] = ''
    params["bug_status"] = bug.getStatus()
    params["priority"] = 5
    params["severity"] = bug.getSeverity()
    params["description"] = ''

    print >>output, templates["bug-debian"] % params

    package = bug.getPackage()
    product = package
    quoted_product = quote_plus(product)
    quoted_package = quote_plus(package)
    print >>output, templates["software-debian"] % {
                            "url": url,
                            "quoted_product": quoted_product,
                            "product": product
                            }
    print >>output, templates["package"] % {
                            "quoted_product": quoted_product,
                            "package": package,
                            "quoted_package": quoted_package
                            }

    # reporter
    reporter = bug.getSubmitter()

    name, mail = email.utils.parseaddr(reporter)

    # Maybe the reporter will contain non-standard characters making email.utils.parseaddr fail
    if string.find(mail,'@') >= 0 :
        quoted_email = quote(mail)
        quoted_email = quoted_email.replace("%40","@")
        print >>output, templates["reporter"] % {
                            "url": url,
                            "email": escapechars(mail),
                            "quoted_email": quoted_email
                        }

    forwarded = bug.getForwarded()
    if forwarded :
        rbts = RemoteBts.find(forwarded)
        if rbts:
            # add the bug to it's bugtracker's queue
            print >>output, templates["forwarded-debian"] % {
                "curbug": url,
                "newbug": forwarded
                }

    return True


# Main program starts here
if __name__ == "__main__":

    # Instantiate the bts-link interface
    RemoteBts.setup(Cnf.resources())
    btsi = bts.BtsInterface(Cnf)

    # Instantiate our btsutils.debbugs compliant interface without the SOAP fallback
    spoolbts = debbugsloc(btsi, False)

    # process all bugs in range from start to end
    for name, type, url, start, end in bugtrackers:

        # should we output the turtle prefix header
        output_header = True

        # Process bugs by batches of BUG_STEP
        for id in range(start, end, BUG_STEP):

            print >>sys.stderr, "Getting [%s] %d - %d.." % (name, id, id+BUG_STEP)

            parsed = False

            for x in range(id, id+BUG_STEP) :
                print >>sys.stderr, "Processing bug %d.." % x,
                try:
                    parsed = parse_bug(spoolbts, x, output_header=output_header)
                    print >>sys.stderr, "ok"
                except:
                    traceback.print_exc()
                    print >>sys.stderr, "error processing %d: %s" % (x, sys.exc_value)
                    sys.exit(1)

                # will only display the header once
                if parsed and output_header :
                    output_header = False
        
    sys.exit(0)

Powered by WebSVN v1.61