#!/usr/bin/python2.2

# Make sure tabs expand to 8 spaces in vim
# vim: expandtab

import os.path, rfc822, sys, string, re, email, common
from xml.dom import implementation, ext

from config import dir, odir, root

def striphtml(input):
    cleared = input.strip()
    while cleared.find("<") != -1 and cleared.find(">") != -1:
        cleared = cleared[:cleared.find("<")] + cleared[cleared.find(">")+1:]
    return cleared

def read_msg(file):
    f = open(file, "r")
    parser = email.Parser.Parser()
    msg = parser.parse(f)
    f.close()
    return msg

def add_resume_for_msg(file, elt, doc, kind):
    global pkg, i
    if os.path.isfile(file):
        msg = read_msg(file)
        info = common.extract_info(msg)
        sub_elt = doc.createElement("item")
        sub_elt.appendChild(doc.createTextNode(info["subject"]))
        if info.has_key("url"):
            sub_elt.setAttribute("url", info["url"])
        else:
            sub_elt.setAttribute("url", "%s/%s/%s.html" % (pkg, kind, i+1))
        if info.has_key("date"):
            sub_elt.setAttribute("date", info["date"])
        elt.appendChild(sub_elt)

# Create the source -> binaries correspondance
sources = {}
f = open(dir + "/sources", "r")
while 1:
    line = f.readline();
    if not line: break #eof
    line = line.strip()
    (binary, source) = line.split(None, 1)
    if not sources.has_key(source):
        sources[source] = []
    sources[source].append(binary)
f.close()

# Read all the bugs stats
bugs = {}
f = open(dir + "/bugs.txt")
while 1:
    line = f.readline()
    if not line: break #eof
    line = line.strip()
    (binary, stats) = line.split(None, 1)
    bugs[binary] = stats.split() 
f.close()

# Read all the PTS stats
pts = {}
f = open(dir + "/count.txt")
while 1:
    line = f.readline()
    if not line: break #eof
    line = line.strip()
    (binary, stats) = line.split(None, 1)
    pts[binary] = stats
f.close()

# Create the XML documents
while 1:
    line = sys.stdin.readline()
    if not line: break #eof
    pkg = line.strip()

    doc = implementation.createDocument(None, "other", None)
    root_elt = doc.documentElement
    
    hash = pkg[0]
    if pkg[0:3] == "lib":
        hash = pkg[0:4]

    # Get PTS stats
    elt = doc.createElement("pts")
    elt.setAttribute("count", pts.get(pkg, "0"))
    root_elt.appendChild(elt)

    # Get BTS stats
    elt = doc.createElement("bugs")
    (s_rc, s_normal, s_wishlist, s_fixed) = (0,0,0,0)
    for binary in sources.get(pkg, []):
        sub_elt = doc.createElement("item")
        sub_elt.setAttribute("name", binary)
        (rc, normal, wishlist, fixed) = bugs.get(binary, ["0","0","0","0"])
        sub_elt.setAttribute("rc", rc)
        sub_elt.setAttribute("normal", normal)
        sub_elt.setAttribute("wishlist", wishlist)
        sub_elt.setAttribute("fixed", fixed)
        rc = string.atoi(rc)
        normal = string.atoi(normal)
        wishlist = string.atoi(wishlist)
        fixed = string.atoi(fixed)
        all = rc + normal + wishlist + fixed
        sub_elt.setAttribute("all", "%d" % all)
        elt.appendChild(sub_elt)
        s_rc += rc
        s_normal += normal
        s_wishlist += wishlist
        s_fixed += fixed
        
    elt.setAttribute("rc", "%d" % s_rc)
    elt.setAttribute("normal", "%d" % s_normal)
    elt.setAttribute("wishlist", "%d" % s_wishlist)
    elt.setAttribute("fixed", "%d" % s_fixed)
    elt.setAttribute("all", "%d" % (s_fixed + s_wishlist + s_normal + s_rc))
    root_elt.appendChild(elt)
 
    # Get news information : static/news/auto
    elt = doc.createElement("static")
    for i in range(5):
        file = "%s/base/%s/%s/static/%d.txt" % (root, hash, pkg, i+1)
        add_resume_for_msg(file, elt, doc, "static")
    root_elt.appendChild(elt)
    elt = doc.createElement("news")
    for i in range(20):
        file = "%s/base/%s/%s/news/%d.txt" % (root, hash, pkg, i+1)
        add_resume_for_msg(file, elt, doc, "news")
    root_elt.appendChild(elt)
    elt = doc.createElement("auto")
    for i in range(10):
        file = "%s/base/%s/%s/auto/%d.txt" % (root, hash, pkg, i+1)
        add_resume_for_msg(file, elt, doc, "auto")
    root_elt.appendChild(elt)

    # Output the data to the XML file
    f = open("%s/%s/%s/other.xml" % (odir, hash, pkg), "w")
    ext.PrettyPrint(doc, f, "iso-8859-1")
    f.close()

