#!/usr/bin/python
# usage: check-versiondiff.py Packages1 Packages2 outfile.html
# 
# this script will check if packages in Packages1 are (not)outdated WRT ones in Packages2
# any file in rfc822 format with Package: and Version: headers is okay

# output is a space-separated file with the results (e.g. for plotting)
import sys, email.Parser, re, apt_pkg, time, os, string
from os.path import dirname, basename, exists

color0 = "#FFFFFF"
color1 = "#00CC66"
color2 = "#FFCC33"

pkgs = {}
tot1 = tot2 = 0

if len(sys.argv) < 4:
    print "not enough arguments"
    print "usage: packages1 packages2 outfile.html"
    sys.exit(1)

apt_pkg.InitSystem()

(p1, p2, out) = sys.argv[1:4]

if not exists(p1) or not exists(p2):
    sys.exit(1)

# Dirty hack to link to LP for source packages
ubuntu_release = 'hardy'
source_list = string.find(out, 'source') != -1

parser = email.Parser.HeaderParser()

# first, fill in our dictionaries
f = open(p1, "r")
m = ""
for line in f.readlines():
    if line.startswith("\n"):
        msg = parser.parsestr(m)
        if msg.has_key("Package") and msg.has_key("Version"):
            tot1+=1
            pkgs[msg["Package"]] = { 'v1' : msg["Version"].split('+')[0], 'v2' : 0, 'gt' : 1 }
        m = ""
    else:
        m = m + line
f.close()

f = open(p2, "r")
m = ""
for line in f.readlines():
    if line.startswith("\n"):
        msg = parser.parsestr(m)
        if msg.has_key("Package") and msg.has_key("Version"):
            tot2+=1
            pkg = msg["Package"]
            if pkgs.has_key(pkg): # compare versions 
               pkgs[pkg]['v2'] = msg["Version"]
               if apt_pkg.VersionCompare(pkgs[pkg]['v2'], pkgs[pkg]['v1']) > 0 :
                   pkgs[pkg]['gt'] = 2
               elif apt_pkg.VersionCompare(pkgs[pkg]['v2'], pkgs[pkg]['v1']) == 0 :
                   pkgs[pkg]['gt'] = 0
            else: # new/unknown package
               pkgs[pkg] = { 'v1' : 0, 'v2' : msg["Version"], 'gt' : 2 }
        m = ""
    else:
        m = m + line
f.close()

if not exists(out):
    f = open(out, "a+").close()

f = open(out, "rw+")

# finally do the HTML output
f.write("""
<html>
<head>
<title>Version differences between Debian and Ubuntu</title>
</head>
<body>
<p><a href="#stats">stats</a></p>

<table width="100%" border="1">
   <th>i</th>
   <th>Name</th>
   <th>debian</th>
   <th>ubuntu</th>
""")

omitted = notv1 = notv2 = gt1 = gt2 = cnt = 0

pkgs.keys().sort()

for pkg in pkgs: 
     
    if pkgs[pkg]['gt'] == 0: # both packages at the same version
        omitted+=1
        continue
    
    cnt += 1

    if pkgs[pkg]['v1'] == 0: 
        f.write("   <tr bgcolor=\"%s\">\n" % color0)
        notv2 += 1
    elif pkgs[pkg]['v2'] == 0:
        f.write("   <tr bgcolor=\"%s\">\n" % color0)
        notv1 += 1
    elif pkgs[pkg]['gt'] == 1:
        f.write("   <tr bgcolor=\"%s\">\n" % color1)
        gt1 += 1
    elif pkgs[pkg]['gt'] == 2:
        f.write("   <tr bgcolor=\"%s\">\n" % color2)
        gt2 += 1
    
    v1 = pkgs[pkg]['v1']
    v2 = pkgs[pkg]['v2']

    f.write("      <td><a name=\"%s\">%s</a></td>\n" % (pkg, cnt))
    f.write("      <td>%s</td><td>" % pkg)
    
    if v1 == 0:
        f.write("not present")
    else:
    	if source_list:
	    f.write("<a href=\"http://packages.qa.debian.org/%s\">%s</a>" % (pkg, v1))
	else:
	    f.write("<a href=\"http://packages.debian.org/%s\">%s</a>" % (pkg, v1))
    f.write("</td><td>") 
    if v2 == 0:
        f.write("not present")
    else:
        if source_list:
            f.write("<a href=\"http://launchpad.net/ubuntu/%s/+source/%s/%s\">"\
	        "%s</a>" % (ubuntu_release, pkg, v2, v2))
	else:
            f.write("<a href=\"http://packages.ubuntu.com/%s\">%s</a>" % (pkg, v2))
    f.write("</td>\n") 
    
    f.write("   </tr>\n")

f.write("""
</table>

<a name="stats">stats</a>
<table border="1">
   <tr><td>omitted packages at the same version</td><td>%s</td></tr>
   <tr><td>packages only in the first column</td><td>%s</td></tr>
   <tr><td>packages only in the second column</td><td>%s</td></tr>
   <tr bgcolor="%s"><td>packages updated in first column</td><td>%s</td></tr>
   <tr bgcolor="%s"><td>packages updated in second column</td><td>%s</td></tr>
</table>

<i>built on: %s</i>
</body>
</html>
""" % (omitted, notv1, notv2, color1, gt1, color2, gt2, time.ctime()))

# fields: date packages_1 packages_2 same_version only_first only_second updated_first updated_second
print """
%s %s %s %s %s %s %s %s
""" % (time.strftime("%m/%d"), tot1, tot2, omitted, notv1, notv2, gt1, gt2)
