import vhdltools
import win32com.client


def find_entity_source(ent,map):
    if map.has_key(ent):
        return map[ent]
    else:
        return "entity %s not in other source files" % ent
            
def main(path):
    print path
    files = vhdltools.get_vhd_files(path)
    result = vhdltools.analyze_files(files,vhdltools.entity_tuple)
    Excel = win32com.client.Dispatch("Excel.Application")
    Excel.Visible = True
    
    wb = Excel.Workbooks.Add()
    sh = wb.Sheets[0]
    sh.Range("A1:D1").Value = ["File Paths","Entity Declarations","Entity Instantiations","File Dependency"]
    nr = len(result)
    entity_to_file = {}
    for r in result:
        for ent in r[1][0]:
            entity_to_file[ent] = r[0]
   
    sh.Range(sh.Cells(2,1),sh.Cells(1+nr,4)).Value = [(str(r[0]),str(r[1][0]),str(r[1][1]),str([find_entity_source(x,entity_to_file) for x in r[1][1]])) for r in result]
    wb.SaveAs("DependencyReport")
    
import sys
if __name__ == "__main__":
    if len(sys.argv) > 1:
        files = main(sys.argv[1])
    else:
        files = main('.')

