#! /usr/bin/env python
"""whattoremove [arguments]

Flags:
    -i file     File of commits to include
    -h          This help

Outputs CSV list of the commits that people who rejected
the license were involved in.  The exclude file can be
generated by a command like this:
    git log --pretty=fomat:%h
"""

import mysql.connector
import datetime, os, re, subprocess, sys, string, random
import getopt

# Parse JCL
include = None
opts, args = getopt.getopt(sys.argv[1:], "hi:")
for o,a in opts:
    if o == '-i':
        f = open(a)
        include = [ c.strip() for c in f ]
        f.close()
    else:
        print __doc__
        raise SystemExit

dbconfig = {
        'user': 'licensereader',
        'password': open('ropass.txt').read().strip(),
        'database': 'license'
        }
conn = mysql.connector.connect(**dbconfig)
cursor = conn.cursor()

# Get those who said no and all the commits the did
q = ( 'SELECT log.cid,users.email FROM log'
        ' LEFT JOIN users ON log.uid = users.uid'
        ' WHERE users.reply = "n" ORDER BY log.cid' )
cursor.execute(q)
cids = []
emails = {}
for row in cursor:
    cid,email = row
    cids.append(cid)
    emails[cid] = str(email)

q = 'SELECT commit,date,descrip FROM commits WHERE cid=%s'
for cid in cids:
    cursor.execute(q, (cid,))
    for row in cursor:
        commit,date,descrip = row
        commit = commit[0:7]
        descrip = descrip.replace('"', '\'').replace(',', '')
        if include == None or commit in include:
            print '%s, %s, "%s"' % (commit, emails[cid], descrip)
