#!/usr/bin/python

# This file is responsible for generating interesting stats for the specified
# user. Things like bugzilla activities, code checked in activites, etc.
#
#   - Gagan Saksena 01/28/2002

from prefs import *
from konst import *
import sys

# This only reports
def autostats(username, pref=None):
    if not pref:
        from prefs import *
        pref = Prefs('%s%s/.prefs' %(kReportsDir, username))

    output = ""
    if pref.bugstat: output = "%s%s" % (output , bugsactivity(username))
    if pref.codestat: output = "%s%s" % (output , codeactivity(username))
    if pref.revstat: output = "%s%s" % (output , revactivity(username))
    if pref.newstat: output = "%s%s" % (output ,  newsactivity(username))

    return output

def bugsactivity(username):
    output = ""
    try:
        bugs_stat = open('%s%s/bugs_stat' %(kReportsDir, username) , 'r')
        output = bugs_stat.read()
        bugs_stat.close()
    finally:
        return output

def codeactivity(username):
    return "If I was up I'd say "+ "code stuff<BR>"

def revactivity(username):
    return "If I was up I'd say "+ "review stuff<BR>"

def newsactivity(username):
    return "If I was up I'd say "+ "news stuff"

# From here onwards we actually run the scripts that update the stats info

def update_bugs_stat(username, since=None): # remember since = YYYY-MM-DD
    if not since:
        from utils import Today
        since = Today()
    bugs_stat = open('%s%s/bugs_stat' %(kReportsDir, username) , 'w')
    stdout, stderr = sys.stdout, sys.stderr
    sys.stdout, sys.stderr = bugs_stat, bugs_stat
    print "Statistics on bugzilla activity since last submitted report (%s)<BR><UL>\n" % since
    for r in ('FIXED', 'DUPLICATE','WORKSFORME/INVALID'): 
        print "<LI><A HREF=\"http://bugzilla.mozilla.org/buglist.cgi?bug_status=RESOLVED&bug_status=VERIFIED&bug_status=CLOSED&resolution=%s&email1=%s&emailtype1=substring&emailassigned_to1=1&bugidtype=include&chfield=resolution&chfieldfrom=%s&chfieldto=Now&field0-0-0=noop&type0-0-0=noop&cmdtype=doit\">Bugs resolved %s</A>\n" % (r, username, since, r)

    print "</UL>"
    sys.stdout, sys.stderr = stdout, stderr
    bugs_stat.close()
    import os
    os.chown(bugs_stat.name, 48, 48) # let apache own it

if __name__ == '__main__':
    update_bugs_stat('gagan', '2003-01-01')
