#!/usr/bin/python
# Copyright 2007-2008  Canonical, Ltd
# Author: Brian Murray <brian@ubuntu.com>
# License: GPLv2
# ---------------------------------------
# Parses the package data csv file and then does some math on the quantity of
# changes for every bug status and writes it to a csv file.  The csv file is
# overwritten since the data is dynamic 

import csv
import sys

sourcepackage = sys.argv[1]
sourcepackagedata = sys.argv[1] + '/' + sys.argv[1] + '.data' 

try:
    reader = csv.reader(open(sourcepackagedata, "rb"))
except IOError:
    print '''%s data file doesn't exist''' % sourcepackagedata
reader = csv.reader(open(sourcepackagedata, "rb"))
# name: column - base 0
statinfo = {
    'total': { 'title':'Total', 'column':1 },
    'new': { 'title':'New', 'column':2 },
    'incomplete': { 'title':'Incomplete', 'column':3 },
    'confirmed': { 'title':'Confirmed', 'column':4 },
    'triaged': { 'title':'Triaged', 'column':5 },
    'inprogress': { 'title':'In Progress', 'column':6 },
    'fixcommitted': { 'title':'Fix Committed', 'column':7 },
    'fixreleased': { 'title':'Fix Released', 'column':8 },
    'invalid': { 'title':'Invalid', 'column':9 },
    'wontfix': { 'title':'''Won't Fix''', 'column':10 },
#    'undecided': { 'title':'Undecided', 'column':12 },
#    'wishlist': { 'title':'Wishlist', 'column':13 },
#    'low': { 'title':'Low', 'column':14 },
#    'medium': { 'title':'Medium', 'column':15 },
#    'high': { 'title':'High', 'column':16 },
#    'critical': { 'title':'Critical', 'column':17 },
            }
statuses = [ 'total', 'new', 'incomplete', 'confirmed', 'triaged', 'inprogress', 'fixcommitted', 'fixreleased', 'invalid', 'wontfix' ]
# length of time in days: number of rows from end (using hours) where -1 is the last row
periods = { 1: '-25',
            7: '-169',
            30: '-721'
          }

quantities = {}
#for stat in statinfo.keys():
#    quantities[stat] = {}
for status in statuses:
    quantities[status] = {}

# just colum zero from the file the date information was gathered
time = []
for row in reader:
    if row[0].startswith('#'):
        continue
    time.append(row[0])
#    for stat in statinfo.keys():
#        column = statinfo[stat]['column']
    for status in statuses:
        column = statinfo[status]['column']
        # set the date equal to the count for the status
        quantities[status][row[0]] = int(row[column])
# every row is +1 because -1 is the last row

#writer = csv.writer(open("%s-changes.csv" % datafile, "wb"))
writeto = sourcepackage + '/' + sourcepackage + '-changes.csv'
changes = open('%s' % writeto, 'w')
for period in sorted(periods.keys()):
    data = []
    data.append('%s' % period)
#    for stat in sorted(statinfo.keys()):
    for status in statuses:
        try: 
            quantities[status][time[int(periods[period])]]
        except IndexError:
            data.append('-')
            continue
        period_change=int(quantities[status][time[-1]] - quantities[status][time[int(periods[period])]])
        data.append('%s' % (period_change))
    changes.write(','.join(data))
    changes.write("\n")
    changes.close
