#!/usr/bin/env python

import pysvn
import getopt
import sys
import string
import time
import cPickle as pickle

# Options by default
path = "." # Current directory
possible_period = "week"
period = 86400 * 7
start_date = None
end_date = None
options_file = "./svncommits.pickle"

# Do not change
time_format = "%Y-%m-%d %H:%M"

def time2string(thetime):
    return time.strftime ("%Y-%B-%d %H:%M", time.localtime(thetime))

try:
    optlist, args = getopt.getopt (sys.argv[1:], "p:e:s:d:",
                                   ["path=", "period=", "start=", "end="])
    for option, value in optlist:
        if option == "--path" or option == "-p":
            path = value
        elif option == "--period" or option == "-e":
            possible_period = string.lower(value)
            if possible_period == "year":
                period = 86400 * 365.25
            elif possible_period == "month":
                period = 86400 * 30.5
            elif possible_period == "week":
                period = 86400 * 7
            elif possible_period == "day":
                period = 86400
            else:
                raise Exception("Unknown period %s" % possible_period)
        elif option == "--start" or option == "-s":
            start_date = time.mktime(time.strptime(value, time_format))
        elif option == "--end" or option == "-d":
            end_date = time.mktime(time.strptime(value, time_format))
        else:
            raise Exception ("Unknown option " + str(option))
except getopt.error, reason:
    raise Exception ("Usage: " + sys.argv[0] + ": " + str(reason))
if len(args) > 0:
    raise Exception("Usage: " + sys.argv[0] + "[-p path]")

client = pysvn.Client()
log = client.log(path)
print "# %s from the limit        Commits" % string.capitalize(possible_period)
slot = 1
commits = 0
if end_date is None:
    limit = time.time()
else:
    limit = end_date
for message in log:
    if start_date is not None and message['date'] < start_date:
        print "# Breaking at %s" % time2string(message['date'])
        # The last slot
        print "%i      %i" % (1 - slot, commits)
        break
    if end_date is not None and message['date'] > end_date:
        print "# Ignoring %s" % time2string(message['date'])
        continue
    interval = (limit - message['date']) / period
    new_slot = int(interval)
    if new_slot > slot:
        print "# From %s (%i -> %i)" % (time2string(message['date']), slot, new_slot)
        while slot < new_slot:
            print "%i      %i" % (1 - slot, commits)
            slot = slot + 1
            commits = 0
    commits = commits + 1
    print "# Commit %i at %s (r%i)" % (commits, time2string(message['date']),
                                       message['revision'].number)
if start_date is None:
    # The last slot
    print "%i      %i" % (1 - slot, commits)
config = {}
config["period"] = string.capitalize(possible_period)
config["limit"] = time2string(limit)
pickle.dump(config,open(options_file,'w'))

