forked from joary/oai-study
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathparse_tmpl.py
81 lines (63 loc) · 1.49 KB
/
parse_tmpl.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
import subprocess, os
base_url = 'https://github.com/joary/openairinterface5g/tree/study'
def regen_line(line, info):
if(len(info) == 0):
return line
new_l = line;
for i in info:
src = i[0]
srcl = str(i[1])
func = i[2]
link = base_url+'/'+src+'#L'+srcl
info_rep = '['+func+'()]('+link+')'
new_l = new_l.replace(func+'()', info_rep)
return new_l
def cscope_search_func(fname):
result = subprocess.run(['bash', 'run_cscope.sh', fname], stdout=subprocess.PIPE)
res = result.stdout.decode('utf-8');
func_info = ''
lines = res.split('\n')
for l in lines:
n = l.count(fname)
if n == 2:
func_info = l;
break;
src = ''
src_line = 0
if(func_info != ''):
func_info = func_info.split(' ')
src = func_info[0]
src_line = int(func_info[2])
return (src, src_line, fname)
def get_function_name(f_str):
tmp = f_str.split('()')
tmp = tmp[0]
tmp = tmp.strip('*')
tmp = tmp.strip('`')
return tmp;
def parse_tmpl(fname, outf):
f = open(fname)
a = f.readlines();
f.close()
ret = []
for l in a:
info = []
for field in l.split(' '):
if '()' in field:
func = get_function_name(field)
data = cscope_search_func(func)
if(data[0] != ''):
info += [data]
new_l = regen_line(l, info)
ret += [new_l]
f2 = open(outf, 'w')
f2.writelines(ret)
f2.close()
return ret
if __name__ == '__main__':
files = os.listdir('.')
for f in files:
if('.md.tmpl' in f):
gen_file = f.replace('.tmpl','');
print('Generating:', gen_file)
parse_tmpl(f, gen_file)