#!/usr/bin/python
# Pau Freixes, pfreixes@milnou.net
#
# Create a histogram of svn revisions


import popen2
import re
import datetime


def main():

    first_day = None
    last_day = None

    d_stats = {}

    reg = re.compile("^(r.*) \| (.*) \| (.*) \| (.*)$")
    s_stdout, s_stdin = popen2.popen2("svn log")
    buffer = s_stdout.readlines()

    for line in buffer:
        m = reg.match(line)

        if (  m is not None):
            revision = int(m.group(1)[1:])
            #print m.group(3)
            #print "%s %s %s" % (m.group(3)[0:4], m.group(3)[5:7], m.group(3)[8:10])
            day = datetime.date(int(m.group(3)[0:4]), int(m.group(3)[5:7]), int(m.group(3)[8:10] ))
            if ( last_day is None ):
                last_day = day

            if d_stats.has_key(day.strftime("%Y/%m/%d")) is False:
                #print "Processing new day %s" % day.strftime("%Y/%m/%d")
                d_stats[day.strftime("%Y/%m/%d")] = 0
                first_day = day


            s_stdout, s_stdin = popen2.popen2("svn -c %d diff" % revision)
            buffer = s_stdout.readlines()


            d_stats[day.strftime("%Y/%m/%d")] = d_stats[day.strftime("%Y/%m/%d")] + len(buffer)




    # iterate for all days
    #print "Creating table from %s to %s" % ( first_day.strftime("%Y/%m/%d"), last_day.strftime("%Y/%m/%d"))

    check_day = first_day + datetime.timedelta(days=1)
    week = 0
    while ( check_day <= last_day ) :
        count = 0
        for i in range(1, 7):
            if d_stats.has_key(check_day.strftime("%Y/%m/%d")) is False:
                count = count + 0
               # print "%s 0" % check_day.strftime("%Y/%m/%d")
            else:
                count = count + d_stats[check_day.strftime("%Y/%m/%d")]
                #print "%s %d" % (check_day.strftime("%Y/%m/%d"), d_stats[check_day.strftime("%Y/%m/%d")])

            check_day = check_day + datetime.timedelta(days=1)

        print "%d %d" % (week, count)


        week = week + 1






    


if __name__ == "__main__":
    main()

