#!/usr/bin/python
#filter.py:  

import sys
import os
import csv
import re
import clone
import sets
import math
from random import randint
#=====================================================================#

DEBUG = 0
KEY_VAL = 0

ANNOTATE_DIFF = "./inputs/prec/annotate_net"

annotate_hash = {}
changeLog_hash = {}
repertoire_hash = {}

#=====================================================================#
def process_annotate_file():
	global ANNOTATE_DIFF
	global annotate_hash
	global KEY_VAL

	anf = open(ANNOTATE_DIFF,"r")

	an_reader = csv.reader(anf, delimiter=',')

	for row in an_reader:
		fname = row[0]
		fname = fname.partition(".csv")[0] + ".c"
		version = row[1].strip()
		diff_line_no = row[5]
#	print fname + "," + version + "," + diff_line_no

		if (KEY_VAL):
			key = (fname,version)
		else:
			key = fname

		if (annotate_hash.has_key(key) == 0):
			annotate_hash[key] = []

		annotate_hash[key].append(diff_line_no)

	anf.close()

#=====================================================================#

def process_change_log(log_file):
	global annotate_hash
	global changeLog_hash
	global KEY_VAL

	#print annotate_hash
	chf = open(log_file,"r")
	ch_reader = csv.reader(chf, delimiter=';')

	for row in ch_reader:
		fname = row[0]
		fname = fname.strip()
		version = row[1].strip()
		change_line = row[4]
		add = change_line.partition("lines: +")[2]
		add = add.partition(" -")[0]
#	print fname + "," + version + "," + change_line + "," + add
		if (KEY_VAL):
			key1 = (fname,version)
		else:
			key1 = fname
		if (annotate_hash.has_key(key1) != 0):
			changeLog_hash[key1] = annotate_hash[key1]
#	else:
#		print "!! could not find " + fname + "," + version

	chf.close()

#	for key in changeLog_hash.iterkeys():
#		diff_line_list = changeLog_hash[key]
#		for i in diff_line_list:
#			print "====" + key + "," + i

#=====================================================================#
def process_repertoire_log(rp_file):

	global repertoire_hash
	global KEY_VAL
	rpf = open(rp_file,"r")

	rp_reader = csv.reader(rpf, delimiter=',')

	for row in rp_reader:
		fname = row[0]
		if fname.startswith("====="):
			pass
		else:
			fname = fname.partition(".csv")[0] + ".c"
			fname = fname.strip()
			version = str(row[1].strip())
			change_line = row[6]
			
			add = change_line.partition(":")[2].strip()
			
			rnd = randint(2,9)
#			if((int(add) < 0) and (rnd < 6)):
			if(int(add) < 4):
				pass
			else:
				cl = change_line.partition(":")[0]
				cl = cl.partition(".")[2]
				start_line, end_line = cl.split('-')
				
#		print fname + "," + version + "," + change_line + "," + add
#		print fname + "," + version + "," + change_line + "," + start_line + "," + end_line

				start = int(start_line)
				end = int(end_line) + 1
			
				if (KEY_VAL):
					key2 = (fname,version)
				else:
					key2 = fname
				
				if (repertoire_hash.has_key(key2) == 0):
					repertoire_hash[key2] = []
				
				for i in range(start,end):
					repertoire_hash[key2].append(i)
			
	rpf.close()

#=====================================================================#
def walk_dir(directory):
	
	for f in os.listdir(directory):
#		print f
		fname = directory + "/" + f
		process_repertoire_log(fname)
#=====================================================================#
if (len(sys.argv) < 3):
	 print "Usage: filter.py changelog.csv repertoire_dir"
	 print "changelog.csv is summary of change log containing porting evidence"
	 print "this is generated by parse_log.py"
	 print "repertoire_dir contains all repertoire output"

ch_file = sys.argv[1]
rp_dir = sys.argv[2]

#print "Input : " + ch_file + "," + rp_dir

#process_annotate_file()
#process_change_log(ch_file)
#process_repertoire_log(rp_file)

walk_dir(rp_dir)

#for key in repertoire_hash.iterkeys():
#	diff_line_list = repertoire_hash[key]
#	for i in diff_line_list:
#		print str(key) + "," + str(i)
#sys.exit()

#-----------Precision & Recall Calculation -------------#
RP = set()
CH = set()

for key in repertoire_hash.iterkeys():
	
	change_list = repertoire_hash[key]
	for i in change_list:
		RP.add((key,i))

for key in changeLog_hash.iterkeys():
	change_list = changeLog_hash[key]
	for i in change_list:
		j = int(i)
		CH.add((key,j))


#print "len RP =" + str(len(RP)) 
for i in RP:
	print i[0] + "," + str(i[1])

#I = RP.intersection(CH)
#U = RP.union(CH)

#print "========= I ========"
#for i in I:
#	print i

#prec = float(len(M_E)) / len(M)

#print "len RP =" + str(len(RP)) 
#print "len CH =" + str(len(CH)) 
#print "len U =" + str(len(U)) 
#print "len I =" + str(len(I)) 

#recall = float(len(M_E)) / len(E)

#print "precision = " + str(prec)
#print "recall = " + str(recall)


