#!/usr/bin/python
################################################################
#
#              Find Dependencies of A Proof 
#
################################################################
#
# Command Line Arguments:
#
#   1. The name of the file containing the proof whose 
#      dependencies are to be traced. (without .tex extension)
#   2. The number of the theorem 
#
################################################################
#
import pickle,os,re,argparse
#
import pattern, synt, prop
from getpath import getpath
#
parser = argparse.ArgumentParser()

parser.add_argument("file",
		help = "name of the file with proof(s) to be audited")

parser.add_argument("num",
		help = "number of theorem to be audited") 

args = parser.parse_args()

if args.file.endswith('.tex'):
	Arg_1 = args.file[-4:]
else:
	Arg_1 = args.file

filename = Arg_1 + '.tex'

try:
	f = open(Arg_1 + ".tex" ,"r")
	f.close()
except:
	raise SystemExit("File " + Arg_1 + ".tex not found")

try:
	f = open(Arg_1 + ".pfs" ,"r")
	f.close()
except:
	print("No .pfs file found: Run parse " + Arg_1)
	raise SystemExit
	

f = open(Arg_1 + ".pfs" ,"rb")
doc = pickle.load(f)

Theorem_ref = args.num

if Theorem_ref not in doc.propdict:
	print(Theorem_ref, " not found.")
	raise SystemExit	

#Find what theorems depend on the given theorem:
n = 0
newlist = [Theorem_ref]
oldlist = []
while newlist:
#	print len(oldlist),len(newlist)
	combined_list = oldlist + newlist
	t = []
	t = set()
	for x in doc.proofdict:
		for p in newlist:
			if p in doc.proofdict[x].prop_refnums and x not in combined_list:
				t.add(x)
				n = n + 1
	oldlist = oldlist + newlist
	newlist = list(t)
del oldlist[0]
consequences = oldlist
n_consequences = len(consequences)

callers = []
for x in doc.proofdict:
	if Theorem_ref in doc.proofdict[x].prop_refnums:
		callers.append(x)

#Find the given theorem needs:
n = 0
newlist = [Theorem_ref]
oldlist = []
while newlist:
#	print len(oldlist),len(newlist)
	combined_list = oldlist + newlist
	t = set()
	for p in newlist:
#		for q in exrefs[p]:
		y = doc.proofdict.get(p, -1)
		if y != -1:
			for q in y.prop_refnums: 
				if q not in combined_list:
					if q not in doc.propdict:
						print("Non-existent reference in proof of",p, ":",q )
						continue
					t.add(q)
					n = n + 1
	oldlist = oldlist + newlist
	newlist = list(t)
#del oldlist[0]
basicpreds = set()
for p in oldlist:
	y = doc.proofdict.get(p, -1)
	if y != -1:
		basicpreds = basicpreds| y.basic_refnums
#	for q in  basicrefs[p]:
#		if q not in basicpredecessors:
#			basicpredecessors.append(q)
basicpredecessors = list(basicpreds)
basicpredecessors.sort(key=lambda x: x.split("."))

predecessors = oldlist[1:]
n_predecessors = len(predecessors)

print()
#print("called by ",len(callers),":")
#for p in callers:
#	print(p, end = ', ')
#print()
#print()

print("used by", n_consequences,":")
consequences.sort(key=lambda x: x.split("."))
for p in consequences:
	print(p, end = ', ')
print()
print()

print("needs ", n_predecessors,":")
predecessors.sort(key=lambda x: x.split("."))
for p in predecessors:
	print(p, end = ', ')
print()
print()
print("uses ",len(basicpredecessors)," zero plus references :")
for p in basicpredecessors:
	print(p, end = ', ')
print()
print()
#f = open(Arg_1 + ".dep","w")
#for x in predecessors:
#	f.write(x + "\n")
#f.close()
