#!/usr/bin/python
#
# Copyright (C) 2007 Red Hat, Inc.
#
# Author: Phil Knirsch
#
# Process the multiple logfiles of pyrpmcheckinstall and automatically
# splits up the logfiles in per package logfiles and generates a nice overview
# html page from it.
#
# Typical usage: cd /var/tmp; ./generateresults fc-extras-devel-i386.log.* /var/tmp/results/
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; version 2 only
#
# 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library 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.
# Copyright 2004, 2005 Red Hat, Inc.
#
#


import sys, time, os, popen2

PYRPMDIR = os.path.dirname(__file__) + "/.."
if not PYRPMDIR in sys.path:
    sys.path.append(PYRPMDIR)

from pyrpm.functions import envraSplit

if len(sys.argv) < 3:
    print "Usage: "+sys.argv[0]+" LOGFILE [LOGFILE..] RESULTDIR"
    sys.exit(1)

resultdir = sys.argv[-1]
logfiles = sys.argv[1:-1]
fulllog = open(resultdir+"/full.log", "w")
resultfile = open(resultdir+"/index.html", "w")
print >>resultfile,"""
<html>
<body>
<H2>
Results for """+logfiles[0]+"""<br>
Date: """+time.ctime()+"""<br>
Complete logfile: <a href="full.log">logfile</a> WARNING! Very large file, better use "Save as" from your browers.<br>
Unique error log: <a href="error.log">errorlog</a><br></H2>
<H3><a name="owner">Results ordered by owner</a></H3>
<H4><a href="#package">Goto results ordered by package</a></H4>
<table border=1>
<tr><th>Owner</th><th>Package</th><th>Status</th></tr>
"""

resnamehash = {}
resownerhash = {}
for lfile in logfiles:
    lines = open(lfile).readlines()
    RES = None
    founderr = False
    for l in lines:
        print >>fulllog, l[:-1]
        if l.startswith("Updating package "):
            if RES != None:
                pkgname = envraSplit(pkgname)[1]
                try:
                    owner = popen2.popen2("whoowns dist-fc7 "+pkgname)[0].read()
                    owner = owner[owner.rindex(" "):-1]
                except:
                    owner = "unknown"
                resnamehash[nevra] = (founderr, owner)
                resownerhash.setdefault(owner, []).append((founderr, nevra))
                RES.close()
            nevra = l[l.index(":")+2:]
            pkgname = nevra[nevra.index("/")+1:-1]
            nevra = nevra[:nevra.index("/")]
            RES = open(resultdir+"/"+nevra, "w")
            founderr = False
            print >>RES, l[:-1]
        else:
            if RES != None:
                if l.find("ERROR") >= 0:
                    founderr = True
                print >>RES, l[:-1]

owners = resownerhash.keys()
owners.sort()
for owner in owners:
    for (founderr, nevra) in resownerhash[owner]:
        print >>resultfile, "<tr><td>"+owner+"</td><td>"+nevra+"</td>"
        pname = nevra.replace(":", "%3A")
        if founderr == True:
            print >>resultfile, "<td bgcolor=\"red\"><a href=\""+pname+"\">Failed</a></td>"
        else:
            print >>resultfile, "<td bgcolor=\"lime\"><a href=\""+pname+"\">Success</a></td>"

print >>resultfile,"""
</table>
<H3><a name="package">Results ordered by package</a></H3>
<H4><a href="#owner">Goto results ordered by owner</a></H4>
<table border=1>
<tr><th>Package</th><th>Owner</th><th>Status</th></tr>
"""

nevras = resnamehash.keys()
nevras.sort()
for nevra in nevras:
    print >>resultfile, "<tr><td>"+nevra+"</td><td>"+resnamehash[nevra][1]+"</td>"
    pname = nevra.replace(":", "%3A")
    if resnamehash[nevra][0] == True:
        print >>resultfile, "<td bgcolor=\"red\"><a href=\""+pname+"\">Failed</a></td>"
    else:
        print >>resultfile, "<td bgcolor=\"lime\"><a href=\""+pname+"\">Success</a></td>"

print >>resultfile,"""
</table>
</body>
</html>
"""

resultfile.close()
fulllog.close()

os.system("grep ERROR "+resultdir+"/full.log | sort -u >"+resultdir+"/error.log")

# vim:ts=4:sw=4:showmatch:expandtab
