1import os
2import logging
3import filecmp
4
5def compare(file_name_1, file_name_2):
6    #write logging information
7    logging.debug('Comparing: %s and %s', os.path.basename(file_name_1), os.path.basename(file_name_2))
8
9    #test if file exist
10    if os.path.exists(file_name_1) is False:
11        logging.error("** ERROR ** %s does not exist", file_name_1)
12        return False
13
14    if os.path.exists(file_name_2) is False:
15        logging.error("** ERROR ** %s does not exist", file_name_2)
16        return False
17
18    match_success = True
19
20    if file_name_1.endswith('.bin'):
21        match_success = filecmp.cmp(file_name_1, file_name_2, shallow=False)
22        if match_success == False:
23            logging.error("** ERROR ** Not match.")
24    else:
25        #find the line where "#include" is
26        start_row_1 = 0
27        file_1 = open(file_name_1,'r')
28        lines_1 = file_1.readlines()
29        for line in lines_1:
30            if "#include" in line:
31                start_row_1 = lines_1.index(line) + 1
32                if "#include" in lines_1[start_row_1] is False:
33                    break
34
35        start_row_2 = 0
36        file_2 = open(file_name_2,'r')
37        lines_2 = file_2.readlines()
38        for line in lines_2:
39            if "#include" in line:
40                start_row_2 = lines_2.index(line) + 1
41                if "#include" in lines_2[start_row_2] is False:
42                    break
43
44        #compare from "#include"
45        lines_1 = lines_1[start_row_1:]
46        lines_2 = lines_2[start_row_2:]
47        diff_count = 0
48
49        for line_1, line_2 in zip(lines_1, lines_2):
50            if line_1 != line_2:
51                print('Does not match:\n=%s\n%s' %(line_1, line_2))
52                logging.error('** ERROR ** Line #%d of %s does not match line #%d of %s\n%s\n%s' %(start_row_1, file_name_1, start_row_2, file_name_2, line_1, line_2))
53                match_success = False
54
55                diff_count += 1
56
57                if diff_count >= 10:
58                    return False
59
60            start_row_1 += 1
61            start_row_2 += 1
62
63    if match_success:
64        logging.debug("Match success.")
65        return True
66    else:
67        return False
68
69def compare_output_files(project_path, golden_file_list):
70    cmp_result = True
71    found = False
72
73    if project_path.endswith('example_internal\\folder_output_test\\'):
74        found = True
75        if compare(project_path + "main_display_resources.c", project_path + "folder_output_test_MAIN_DISPLAY_resources.c") == False:
76            logging.debug("Not Match.")
77            cmp_result = False
78
79        if compare(project_path + "main_display_resources.h", project_path + "folder_output_test_MAIN_DISPLAY_resources.h") == False:
80            logging.debug("Not Match.")
81            cmp_result = False
82
83        #compare specified folder output
84        if compare(project_path + "secondary_resources.c", project_path + "folder_output_test_SECONDARY_resources.c") == False:
85            logging.debug("Not Match.")
86            cmp_result = False
87
88        if compare(project_path + "secondary_resources.h", project_path + "folder_output_test_SECONDARY_resources.h") == False:
89            logging.debug("Not Match.")
90            cmp_result = False
91
92        if compare(project_path + "specifications.h", project_path + "folder_output_test_specifications.h") == False:
93            logging.debug("Not Match.")
94            cmp_result = False
95
96        #if compare(project_path + "specifications.c", project_path + "folder_output_test_specifications.c") == False:
97        #    logging.debug("Not Match.")
98        #    cmp_result = False
99
100        if compare(project_path + "button_screen.c", project_path + "button_screen_specifications.c") == False:
101            logging.debug("Not Match.")
102            cmp_result = False
103
104        if compare(project_path + "empty_folder.c", project_path + "empty_folder_specifications.c") == False:
105            logging.debug("Not Match.")
106            cmp_result = False
107
108        if compare(project_path + "indicator_screen_runtime_allocate.c", project_path + "indicator_screen_runtime_allocate_specifications.c") == False:
109            logging.debug("Not Match.")
110            cmp_result = False
111
112        if compare(project_path + "popup_modal.c", project_path + "popup_modal_specifications.c") == False:
113            logging.debug("Not Match.")
114            cmp_result = False
115
116        if compare(project_path + "static_screens.c", project_path + "static_screens_specifications.c") == False:
117            logging.debug("Not Match.")
118            cmp_result = False
119
120    else:
121        for file in golden_file_list:
122            test_file = ""
123
124            if file.endswith('MAIN_DISPLAY_resources.bin'):
125                test_file = 'MAIN_DISPLAY_resources.bin'
126
127            elif file.endswith('SECONDARY_resources.bin'):
128                test_file = 'SECONDARY_resources.bin'
129
130            elif file.endswith('MAIN_DISPLAY_resources.c'):
131                test_file = 'MAIN_DISPLAY_resources.c'
132
133            elif file.endswith('SECONDARY_resources.c'):
134                test_file = 'SECONDARY_resources.c'
135
136            elif file.endswith('resources.c'):
137                test_file = 'resources.c'
138
139            elif file.endswith('MAIN_DISPLAY_resources.h'):
140                test_file = 'MAIN_DISPLAY_resources.h'
141
142            elif file.endswith('SECONDARY_resources.h'):
143                test_file = 'SECONDARY_resources.h'
144
145            elif file.endswith('resources.h'):
146                test_file ='resources.h'
147
148            elif file.endswith('_specifications.c'):
149                test_file = 'specifications.c'
150
151            elif file.endswith('_specifications.h'):
152                test_file = 'specifications.h'
153
154            elif file.endswith("resources.bin"):
155                test_file = "resources.bin"
156
157            elif file.endswith("Japanese.bin"):
158                test_file = "japanese.bin"
159
160            elif file.endswith("Japanese_big_endian.bin"):
161                test_file = "japanese_big_endian.bin"
162
163            elif file.endswith("resources_big_endian.bin"):
164                test_file = "resources_big_endian.bin"
165
166            if test_file:
167                found = True
168                if compare(project_path + test_file, project_path + file) == False:
169                    cmp_result = False
170
171    if found == False:
172        logging.debug("No file been compared")
173        return False
174
175    return cmp_result
176
177def compare_output_folders(project_path, test_output_path):
178    cmp_result = True
179    file_list = os.listdir(test_output_path)
180    logging.debug("comparing output folder: %s", test_output_path)
181    for file in file_list:
182        if compare(project_path + file, test_output_path + file) == False:
183            cmp_result = False
184
185    return cmp_result