-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfixupsymbols
executable file
·189 lines (165 loc) · 5.12 KB
/
fixupsymbols
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
#!/usr/bin/python3
#(C) 2017 Peter Michael Green <[email protected]>
#This software is provided 'as-is', without any express or implied warranty. In
#no event will the authors be held liable for any damages arising from the use
#of this software.
#
#Permission is granted to anyone to use this software for any purpose, including
#commercial applications, and to alter it and redistribute it freely, subject to
#the following restrictions:
#
#1. The origin of this software must not be misrepresented; you must not claim.
#that you wrote the original software. If you use this software in a product,
#an acknowledgment in the product documentation would be appreciated but is.
#not required.
#
#2. Altered source versions must be plainly marked as such, and must not be
#misrepresented as being the original software.
#
#3. This notice may not be removed or altered from any source distribution.
import sys
import re
import subprocess
import collections
import afpg_readconfig
from difflib import SequenceMatcher
from afpg_util import mergelists
aggressive = afpg_readconfig.readconfigentry('main','aggressivesymbolsfixup').upper() == 'YES'
filetofix = sys.argv[1]
f = open(filetofix,'rb')
lines = f.readlines()
f.close()
ld = []
lm = []
lu = []
mode = 0; # 0: unconflicted text 1: downstream text 2: mergehead text 3: upstream text
#for stage in range(0,2):
for line in lines:
#print(repr(line))
if (line.startswith(b'<<<<<<< ')):
if (mode == 0):
mode = 1
#print('found <<<<<<< switching to mode 1')
else:
print('broken diff3, unexpected <<<<<<<', file=sys.stderr)
sys.exit(1);
elif (line.startswith(b'||||||| ')):
if (mode == 1):
mode = 2
#print('found ||||||| switching to mode 2')
else:
print('broken diff3, unexpected |||||||', file=sys.stderr)
sys.exit(1);
elif (line == b'=======\n'):
if (mode == 2):
mode = 3
#print('found ======= switching to mode 3')
else:
print('broken diff3, unexpected =======', file=sys.stderr)
sys.exit(1);
elif (line.startswith(b'>>>>>>> ')):
if (mode == 3):
#print('found >>>>>>> switching to mode 0')
mode = 0
else:
print('broken diff3, unexpected >>>>>>>', file=sys.stderr)
sys.exit(1);
else:
if ((mode == 0) or (mode == 1)):
ld.append(line)
if ((mode == 0) or (mode == 2)):
lm.append(line)
if ((mode == 0) or (mode == 3)):
lu.append(line)
def makesymbolsdict(lines):
sd = collections.OrderedDict()
for line in lines:
#print(repr(line))
stripped = line.strip()
if stripped[0] == ord(b'#'):
#comment line, treat the whole line as the "smbol name"
name = stripped
text = line
else:
#print(line)
nobrackets = re.sub(br'\([^\)]*\)', b'', stripped)
match = re.match(br'"[^"]*',nobrackets)
#print(repr(match))
if match is None:
name = nobrackets.split(b' ')[0]
else:
name = match.group()[1:]
text = line
#print(name)
if name in sd:
print('duplicate symbol name '+repr(name))
sd[name] = text
#if (name != line):
# sys.exit(1)
return sd
dd = makesymbolsdict(ld)
dm = makesymbolsdict(lm)
du = makesymbolsdict(lu)
#extract sets from dictionaries.
sd = set(dd.keys())
sm = set(dm.keys())
su = set(du.keys())
dsadded = sd - sm
dsremoved = sm - sd
usadded = su - sm
usremoved = sm - su
#print(dsadded)
#print(dsremoved)
#print(usadded)
#print(usremoved)
finalset = (sm | usadded | dsadded) - usremoved - dsremoved
final = []
for symbol in mergelists(list(du.keys()),b = list(dd.keys())):
if symbol in finalset:
#if symbol == b'_ZTVSt15_Sp_counted_ptrIPN4Kleo21KConfigBasedKeyFilterELN9__gnu_cxx12_Lock_policyE2EE@Base':
# print(symbol)
# print(dd[symbol])
# print(dm[symbol])
# print(du[symbol])
if symbol not in dd:
final.append(du[symbol])
elif symbol not in du:
final.append(dd[symbol])
elif du[symbol] == dd[symbol]:
final.append( du[symbol])
elif (symbol in dm) and (du[symbol] == dm[symbol]):
#upstream matches mergehead, so use downstream
final.append(dd[symbol])
elif (symbol in dm) and (dd[symbol] == dm[symbol]):
#downstream matches mergehead, so use upstream
final.append(du[symbol])
else:
#print(repr(dubd[symbol]))
#print(repr(dubd[symbol][0]))
if symbol in dm:
t1 = 'Warning: upstream, downstream and mergehead have symbol '
t2 = ' with different metadata,'
t3 = ' mergehead metadata is '+repr(dm[symbol])
else:
t1 = 'Warning: upstream and downstream have symbol '
t2 = ' with different metadata and symbol is not in mergehead at all,'
t3 = ''
if (aggressive):
print(t1+symbol.decode('ascii')+t2+' using downstream metadata, upstream metadata is '+repr(du[symbol])+' downstream metadata is '+repr(dd[symbol])+t3 )
final.append(dd[symbol])
else:
print(t1+symbol.decode('ascii')+t2+' upstream metadata is '+repr(du[symbol])+' downstream metadata is '+repr(dd[symbol])+ t3)
sys.exit(1)
f = open(filetofix+'.new','wb')
#print('foo')
for line in final:
#print(type(line))
#sys.exit(1);
f.write(line)
f.close
#sys.exit(1)
command = ['mv',filetofix+'.new',filetofix]
print(command, flush=True)
if (subprocess.call(command) != 0):
print('moviing result into place failed')
sys.exit(1)