from lxml import etree
import sys, os

def generate_DOT(xmlName):
	xsl_tree = etree.parse('./xslt/toDot.xsl')
	transform = etree.XSLT(xsl_tree)
	doc = etree.parse(xmlName)
	result = transform(doc)
	out = open(xmlName[:xmlName.rfind('.xml')] + '.dot', 'w')
	out.write(str(result))
	out.close()
	# call the DOT software now
	os.system("dot -Tpng -o" + xmlName[:xmlName.rfind('.xml')] + '.png' + " " + xmlName[:xmlName.rfind('.xml')] + '.dot')
	os.system("rm -f " + xmlName[:xmlName.rfind('.xml')] + '.dot')

def generate_AST(fname):
	xml_name = fname[:fname.rfind('.php')] + '.xml'
	os.system("php-ast.exe " + fname + " " + xml_name)
	return xml_name

if __name__ == '__main__':
	fname = sys.argv[1]
	xml_name = generate_AST(fname)
	generate_DOT(xml_name)
