-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto_updater.py
124 lines (98 loc) · 3.05 KB
/
auto_updater.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import sys, os, subprocess
from enum import Enum
from lib.getNewFunc import getNewFunc
CODE_FILE = -1
Debug = True # change to True for debug prints
if "--m2c-path" in sys.argv:
m2c_path = sys.argv[sys.argv.index("--m2c-path") + 1]
print(m2c_path)
srcRepoPath = sys.argv[CODE_FILE].split("src")[0]
def getFileName(_file):
return _file.split("/")[-1].split(".")[0]
def getGlobalAsmFile(_file):
return _file.replace('"', " ").split()[1]
def getFuncName(_str):
temp = (
_str.replace("(", " ")
.replace(":", " ")
.replace("*", " ")
.replace(")", " ")
.split()
)
return "".join([i for i in temp if "func_" in i])
process = subprocess.Popen(
"git ls-files -s mips_to_c/", shell=True, stdout=subprocess.PIPE
)
mips_to_c_version = process.communicate()
mips_to_c_version = mips_to_c_version[0].decode("ascii").split()[1]
if Debug:
print(mips_to_c_version)
# file bounds constants
START = 0
END = 1
funcBounds = {}
global_asm_lookup = {} # just in case
# Pass 1: populate data structures
fileName = os.getcwd() + "/tmp/" + getFileName(sys.argv[-1]) + ".c"
inFile = open(sys.argv[CODE_FILE], "r")
fileBuffer = inFile.readlines()
inFile.close()
inFile = open(sys.argv[CODE_FILE], "r")
ifdef_line = 1
global_asm_file = 1
end_line = 1
funcName = ""
lineNum = 0
inFunc = 0
for line in inFile:
if "//" in line:
lineNum += 1
continue
if "#ifdef" in line:
inFunc = 1
ifdef_line = lineNum
if "func_" in line and (lineNum - ifdef_line == 2):
print(line)
funcName = getFuncName(line)
if "GLOBAL_ASM" in line:
global_asm_file = getGlobalAsmFile(line)
if "#endif" in line and inFunc == 1:
inFunc = 0
funcBounds[funcName] = [ifdef_line, lineNum]
global_asm_lookup[funcName] = global_asm_file
lineNum += 1
if Debug:
print(funcBounds)
print(global_asm_lookup)
toDel = []
# Pass 2: filter out functions that are up to date
for sym in funcBounds:
temp = funcBounds[sym]
# TODO: have a way to force this
# (if one wants to update a specific function automatically, or to reset a func)
if "mips_to_c commit" in fileBuffer[temp[START] + 1]:
commit = fileBuffer[temp[START] + 1].split()[-1]
if commit == mips_to_c_version:
toDel.append(sym)
del global_asm_lookup[sym]
for i in toDel:
del funcBounds[i]
if Debug:
print(funcBounds)
print(global_asm_lookup)
# Pass 3: Write the new file and place new functions
startLine = 0
outFile = open(sys.argv[CODE_FILE], "w+")
for sym in funcBounds:
tmp = funcBounds[sym]
outFile.write("".join(fileBuffer[startLine : tmp[START]]))
startLine = tmp[END]
outFile.write("#ifdef MIPS_TO_C\n")
outFile.write("// generated by mips_to_c commit " + mips_to_c_version + "\n")
x = getNewFunc(srcRepoPath + global_asm_lookup[sym])
outFile.write(x)
outFile.write("#else\n")
outFile.write('GLOBAL_ASM("' + global_asm_lookup[sym] + '")\n')
# flush
outFile.write("".join(fileBuffer[startLine:]))
outFile.close()