1#!/usr/bin/env python
2# -*- indent-tabs-mode:1 tab-width:4 mode:python minor-mode:whitespace -*-
3import os
4import sys
5
6# Divide the scripts into two files
7srcdir = os.path.dirname(__file__)
8sys.path.append(os.getcwd())
9# sys.path.append(srcdir)
10from helper import *
11
12# path where DWG files are present
13path_to_dwg = srcdir + "/../test-data"
14# The name of the output files folder
15outdir = "test_output"
16
17# Get all the directories
18dirs = [d for d in os.listdir(path_to_dwg)
19	if os.path.isdir(os.path.join(path_to_dwg, d)) and
20		(d[0] == 'r' or int(d) <= 2018)]
21
22for dir in dirs:
23	for file in os.listdir(os.path.join(path_to_dwg, dir)):
24		if file.endswith(".dwg"):
25			# First thing will be to make duplicate directory structure
26			if not os.path.exists(outdir + "/" + dir):
27				os.makedirs(outdir + "/" + dir)
28			pass
29
30# generate xml from txt files
31generatexml(path_to_dwg)
32
33# Now execute testsuite.c on all the DWG files found and create a separate directory structure
34for dir in dirs:
35	for file in os.listdir(os.path.join(path_to_dwg, dir)):
36		if file.endswith(".dwg"):
37			# filename of the XML File
38			dwg_xmlfile = file.rsplit(".", 1)[0] + ".xml"
39			if os.path.exists(path_to_dwg + "/" + dir + "/" + dwg_xmlfile):
40				# Start running testsuite on every DWG file
41				os.system("./testsuite " + path_to_dwg + "/"
42						  + dir + "/" + file + " " + outdir
43						  + "/" + dir + "/" + dwg_xmlfile + " 2> /dev/null")
44		pass
45
46# Now we have XML file. Next Up is Comparison
47final_output = []
48for dir in dirs:
49	for file in os.listdir(os.path.join(path_to_dwg, dir)):
50		if file.endswith(".xml"):
51
52			# Duplicate file has same directory structure
53			if os.path.exists(outdir + "/" + dir + "/" + file):
54				result = xmlprocess(path_to_dwg+ "/" + dir + "/" + file,
55									outdir + "/" + dir + "/" + file)
56			else:
57				result = [0, []]
58
59			final_output.insert(len(final_output),
60								[dir, file,result[0], result[1]])
61
62
63# Now Generate a pretty report for it
64
65#read the header
66header = open(srcdir + "/header.htm","r")
67reporthtm = open("result.htm", "w")
68reporthtm.write(header.read())
69current_format = ""
70for report in final_output:
71	if current_format != report[0]:
72		# Print the header of the File Format
73		reporthtm.write(
74			"\n<div class='heading'>\n<h3>Output for %s File Format</h3>\n</div>"
75			% report[0])
76		print (bcolors.HEADER + "\n\n****Output for %s File Format****"
77			   % report[0] + bcolors.ENDC)
78
79	if report[2] < 100 and report[2] != 0:
80		print (bcolors.OKGREEN + "%s: [%d]"
81			   % (report[1], report[2]) + bcolors.ENDC)
82		reporthtm.write("\n<div class='result_middle'><b>%s</b> was <b>%d</b> percentage matched</div>\n"
83						% (report[1], report[2]))
84	elif report[2] == 0:
85		print (bcolors.WARNING + "%s: [%d]"
86			   % (report[1], report[2]) + bcolors.ENDC)
87		reporthtm.write("\n<div class='result_bad'>%s was not read at all</div>\n"
88						% report[1])
89	reporthtm.write("\n<div class='attributedetail'><h3>Attribute Details</h3>\n")
90	for unmatched in report[3]:
91		if unmatched['duplicate'] == "":
92			reporthtm.write("\n<p><b>%s</b> wasn't found at all. It's value should be <b>%s</b></p>\n"
93							% (unmatched['attrname'], unmatched['original']))
94		else:
95			reporthtm.write("\n<p><b>%s</b> didn't match. It's value should be <b>%s</b>, and it is <b>%s</b></p>\n"
96							%(unmatched['attrname'],
97							  unmatched["original"], unmatched['duplicate']))
98	reporthtm.write("</div>")
99	current_format = report[0]
100
101# All information has been printed. Print the footer
102print (bcolors.HEADER + "****End of Report****" + bcolors.ENDC)
103reporthtm.write("<div><h1>End of Report</h1></div></body></html>");
104reporthtm.close()
105