-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoMerger.py
108 lines (90 loc) · 3.39 KB
/
autoMerger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
from posixpath import join
# ==============
# CONFIGURATIONS
# ==============
# enter the path to your judge
# path_to_judge_dir = r"/Users/anthoni/Desktop/dsa/team40/51/main.c"
# enter the function to want to test
# 0/1/2 : expression/find/group
test_func = 0
# ==============
# MERGE
# ==============
merged_main_loc = os.path.join(os.getcwd(), "main_merged.c")
output_file = open(merged_main_loc, "w")
## write include
header_files = ["#include \"api.h\"",
"#include <stdio.h>",
"#include <string.h>",
"#include <stdlib.h>",
"#include <stdbool.h>",
"#include <limits.h>"]
for header in header_files:
output_file.write("{}\n".format(header))
## write global vars
global_vars = ["int n_mails, n_queries;\n",
"mail *mails;\n",
"query *queries;\n"]
for globle_var in global_vars:
output_file.write("{}".format(globle_var))
include_struct = ["main.c",
"hashTable.h",
"expression_match.h",
"group_analyze.h",
"find_similar.h",]
constants = list()
func_declaration = list()
func_definition = list()
mode = ""
test_func_def = True
for file_name in include_struct:
file = open(os.path.join(os.getcwd(), file_name), "r", encoding="utf-8")
mode = ""
func_definition.append("\n\n// =================================\n")
func_definition.append("// == START of {} \n".format(file_name))
func_definition.append("// =================================\n\n")
for line in file:
indented_line = line
line = line.strip() + '\n'
if line[:7] == "#define":
if line == "#define DEBUG 1\n":
constants.append("#define DEBUG 0\n") # submission-ready
else:
constants.append(indented_line)
continue
if line[:8] == "// Mark1":
mode = "function_declare"
continue
if line[:8] == "// Mark2":
mode = "function_define"
continue
if line[:9] == "// Mark3-":
if (int(line[9]) == test_func) or int(line[9]) == 4:
test_func_def = True
else:
test_func_def = False
continue
if mode == "function_declare":
func_declaration.append(indented_line)
elif (mode == "function_define") and test_func_def:
func_definition.append(indented_line)
func_definition.append("\n\n// =================================\n")
func_definition.append("// == END of {} \n".format(file_name))
func_definition.append("// =================================\n\n")
output_file.write("\n\n// =================================\n")
output_file.write("// == CONSTANTS\n")
output_file.write("// =================================\n\n")
for constant in constants:
output_file.write(constant)
output_file.write("\n\n// =================================\n")
output_file.write("// == Mark1 - Function Declarations\n")
output_file.write("// =================================\n\n")
for line in func_declaration:
output_file.write(line)
output_file.write("\n\n// =================================\n")
output_file.write("// == Mark1 - Function Definitions\n")
output_file.write("// =================================\n\n")
for line in func_definition:
output_file.write(line)
output_file.close()