forked from yangminz/bcst_csapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.py
280 lines (258 loc) · 9.63 KB
/
cmd.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/python3
# usage:
# ./cmd.py argv[1] argv[2], ...
# /usr/bin/python3 ./cmd.py argv[1] argv[2], ...
import sys, os, subprocess, argparse, math, json
from pathlib import Path
from functools import reduce
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--build", help="Build the target module.")
parser.add_argument("-t", "--test", help="Test the target module.")
parser.add_argument("-d", "--debug", help="Debug the target module.")
parser.add_argument("-m", "--memcheck", help="Check the memory of the target module.")
parser.add_argument("--hex", help="Convert ascii word to hex 64 numbers.")
parser.add_argument("--clean", action="store_true", help="Clean the unused files.")
parser.add_argument("--count", action="store_true", help="Count the lines of source code.")
parser.add_argument("--format", action="store_true", help="Format the lines of source code.")
parser.add_argument("--copyright", action="store_true", help="Add copyright info to source code.")
parser.add_argument("--csim", action="store_true", help="Check and verify the cache module.")
args = parser.parse_args()
# print arguments
i = 0
for argv in sys.argv:
print("[", i, "] ", argv)
i += 1
print("================")
def load_config():
filename = "./config.json"
content = open(filename, "r", encoding="utf-8").read()
js = json.loads(content)
return js
# file headers contains copyright information
def make_build_directory():
if not os.path.isdir("./bin/"):
os.mkdir("./bin/")
if not os.path.isdir("./files/swap/"):
os.mkdir("./files/swap/")
def add_copyright_header():
# get files with paths
filelist = list(Path(".").rglob("*.[ch]"))
notification = load_config()["copyright"]
# recursively add lines to every .c and .h file
print("recursively add lines to every .c and .h file")
for filename in filelist:
try:
with open(filename, "r", encoding = 'ascii') as fr:
content = fr.read()
if (content.startswith("/* BCST - Introduction to Computer Systems")):
print("\tskip\t%s" % filename)
fr.close()
continue
else:
fr.close()
# reopen and write data: this is a safer approach
# try to not open in r+ mode
print("\tprepend\t%s" % filename)
with open(filename, "w", encoding = 'ascii') as fw:
fw.write(notification + content)
fw.close()
except UnicodeDecodeError:
print(filename)
def format_include(s, line_index):
a = "#include<headers/"
b = "#include<"
update = False
old = s
# check include
if s.startswith(a):
s = "#include \"headers/" + s[len(a):]
for j in range(len(s)):
if s[j] == '>':
l = list(s)
l[j] = "\""
s = "".join(l)
update = True
elif s.startswith(b):
s = "#include <" + s[len(b):]
update = True
if update:
print("\tline [%d] #include rule: \"%s\" ==> \"%s\"" % (line_index, old, s))
return s
def format_marco(s, line_index):
a = s.strip("\n")
a = a.strip(" ")
if (len(a) >= 1 and a[len(a) - 1] == ";"):
return s
if (a.startswith("#if") or a.startswith("#endif") or a.startswith("#else") or a.startswith("#define")):
a = a + "\n"
print("\tline [%d] marco rule: \"%s\" ==> \"%s\"" % (line_index, s, a))
return a
return s
def format_tag(s, line_index):
a = s.strip("\n")
a = a.strip(" ")
if (len(a) >= 1 and a[len(a) - 1] == ";"):
return s
charset = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "_" }
for i in range(len(a) - 1):
if not a[i] in charset:
return s
# are all in char set
if len(a) >= 1 and a[len(a) - 1] == ":":
# this is a tag
a = a + "\n"
print("\tline [%d] tag rule: \"%s\" ==> \"%s\"" % (line_index, s, a))
return a
return s
def format_whiteline(s, line_index):
space = 0
for c in s:
if c == ' ':
space += 1
if space == len(s) - 1 and s[-1] == '\n':
s = "\n"
# print("\tline [%d] white line rule: delete" % (line_index))
return s
def format_code():
# get files with paths
filelist = list(Path(".").rglob("*.[ch]"))
# recursively add lines to every .c and .h file
print("recursively check every .c and file")
for filename in filelist:
try:
with open(filename, "r", encoding = 'ascii') as fr:
content = fr.readlines()
print(filename, ":")
for i in range(len(content)):
content[i] = format_include(content[i], i)
content[i] = format_whiteline(content[i], i)
content[i] = format_marco(content[i], i)
content[i] = format_tag(content[i], i)
fr.close()
# reopen and write data: this is a safer approach
# try to not open in r+ mode
with open(filename, "w", encoding = 'ascii') as fw:
fw.writelines(content)
fw.close()
except UnicodeDecodeError:
print(filename)
def count_lines():
# get files with paths
filelist = list(Path(".").rglob("*.[ch]"))
name_count = []
total_count = 0
maxfilename = 0
for filename in filelist:
count = 0
for index, line in enumerate(open(filename, 'r')):
count += 1
# skip the test files
if "tests" in str(filename):
continue
name_count += [[str(filename), count]]
total_count += count
if len(str(filename)) > maxfilename:
maxfilename = len(str(filename))
# print result
print("count .c and .h file lines:")
sortedlist = sorted(name_count, key = lambda x: x[1], reverse=True)
for [filename, count] in sortedlist:
print(filename, end="")
n = (int(maxfilename / 4) + 1) * 4
for i in range(n - len(filename)):
print(" ", end="")
print(count)
print("\nTotal:", total_count)
def run_recursive(obj):
if isinstance(obj, list):
if len(obj) > 0:
all_childs_string = reduce(
lambda x, y: x and y,
list(map(lambda x: isinstance(x, str), obj)))
list_childs = list(filter(lambda x: isinstance(x, list), obj))
if all_childs_string:
# a list of strings -- command
print(" ".join(obj))
subprocess.run(obj)
elif len(list_childs) > 0:
for child in list_childs:
run_recursive(child)
def execute(module, component):
make_build_directory()
config = load_config();
if not module in config:
print("input the correct build module:", config.keys())
exit()
if not component in config[module]:
print("target module '" + module + "' does not have the '" + component + "' component.\nplease check.")
exit()
# find all list of strings and execute
assert(component in {"build", "test", "debug", "memcheck"})
run_recursive(config[module][component])
def cache_verify():
make_build_directory()
csim_ref_file = "/mnt/e/Ubuntu/cache/csim-ref"
trace_dir = "/mnt/e/Ubuntu/cache/traces/"
assert(os.path.isfile(csim_ref_file))
assert(os.path.isdir(trace_dir))
test_cases = [
# s E b
[ 2, 1, 2, "wide.trace" ],
[ 3, 2, 2, "load.trace" ],
[ 1, 1, 1, "yi2.trace" ],
[ 4, 2, 4, "yi.trace" ],
[ 2, 1, 4, "dave.trace" ],
[ 2, 1, 3, "trans.trace" ],
[ 2, 2, 3, "trans.trace" ],
[ 14, 1024, 3, "trans.trace" ],
[ 5, 1, 5, "trans.trace" ],
[ 5, 1, 5, "long.trace" ],
]
for [s, E, b, file] in test_cases:
# need to reload shared library for each test run
# thus we start a new process
a = [
"/usr/bin/python3",
"./src/tests/test_cache.py",
"/mnt/e/Ubuntu/cache/csim-ref",
"/mnt/e/Ubuntu/cache/traces/" + file,
str(s), str(E), str(b),
]
print(" ".join(a))
subprocess.run(a)
def printHex():
strVal = args.hex
print(strVal)
length = len(strVal)
buffer = ""
for i in range(math.ceil(length/8)):
line = ""
for j in range(8):
index = j + i * 8
if index < len(strVal):
line = str(hex(ord(strVal[index])))[2:] + line
else:
line = "00" + line
line = "[" + str(i * 8) + "]\t" + line
line += "\t" + strVal[i * 8: i * 8 + 8] + "\n"
buffer += line
print(buffer)
# main
if args.build:
execute(args.build, "build")
elif args.test:
execute(args.test, "test")
elif args.debug:
execute(args.debug, "debug")
elif args.memcheck:
execute(args.memcheck, "memcheck")
elif args.hex:
printHex()
elif args.count == True:
count_lines()
elif args.copyright == True:
add_copyright_header()
elif args.format == True:
format_code()
elif args.csim == True:
cache_verify()