forked from sparcians/atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeInstHandlers.py
64 lines (51 loc) · 2.02 KB
/
MergeInstHandlers.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
import os, sys, re, subprocess
if len(sys.argv) > 1:
os.chdir(os.path.abspath(sys.argv[1]))
result = subprocess.run(['git', 'status', '--porcelain', '|', 'grep "M "'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode == 0 and result.stdout:
raise RuntimeError('Cannot have uncommitted changes in this directory: {}'.format(os.getcwd()))
ext = os.path.basename(os.getcwd())
rv_insts_cpp = 'Rv{}Insts.cpp'.format(ext)
pattern = r"ActionGroup\* Rv{}Insts::".format(ext)
headers = set()
headers.add('#include "core/ActionGroup.hpp"\n')
headers.add('#include "include/ActionTags.hpp"\n')
for filename in os.listdir(os.getcwd()):
if filename.endswith(".cpp") and filename != rv_insts_cpp:
with open(filename, 'r') as fin:
for line in fin.readlines():
if line.find('#include') == 0:
headers.add(line)
lines = list(headers)
lines = sorted(lines, key=len, reverse=True)
lines.append('')
with open(rv_insts_cpp, 'r') as fin:
transfer = False
for line in fin.readlines():
if line.find('namespace atlas') == 0:
transfer = True
elif line.find('// namespace atlas') != -1:
break
if transfer:
lines.append(line)
lines = [''.join(lines)]
lines.append('')
for filename in os.listdir(os.getcwd()):
if filename.endswith(".cpp") and filename != rv_insts_cpp:
lines.append('')
with open(filename, 'r') as fin:
transfer = False
for line in fin.readlines():
if re.search(pattern, line):
transfer = True
elif line.find('// namespace atlas') != -1:
break
if transfer:
lines.append(line.rstrip('\n'))
lines.append('')
lines.append('} // namespace atlas')
with open(rv_insts_cpp, 'w') as fout:
fout.write('\n'.join(lines))
for filename in os.listdir(os.getcwd()):
if filename.endswith('.cpp') and filename != rv_insts_cpp:
os.remove(filename)