forked from Liquifact/Liquifact-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_init.py
More file actions
53 lines (53 loc) · 1.48 KB
/
Copy pathpatch_init.py
File metadata and controls
53 lines (53 loc) · 1.48 KB
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
from pathlib import Path
root = Path('escrow/src')
patches = 0
for path in root.rglob('*.rs'):
text = path.read_text()
out = []
i = 0
while True:
idx = text.find('client.init(', i)
if idx == -1:
out.append(text[i:])
break
out.append(text[i:idx])
j = idx + len('client.init(')
depth = 1
in_string = False
escape = False
while j < len(text) and depth > 0:
c = text[j]
if in_string:
if escape:
escape = False
elif c == '\\':
escape = True
elif c == '"':
in_string = False
else:
if c == '"':
in_string = True
elif c == '(':
depth += 1
elif c == ')':
depth -= 1
j += 1
if depth != 0:
out.append(text[idx:])
break
call_body = text[idx:j]
if call_body.endswith(');'):
inner = call_body[:-2]
if ', &None' not in inner[-20:]:
out.append(inner)
out.append(', &None);')
patches += 1
else:
out.append(call_body)
else:
out.append(call_body)
i = j
new_text = ''.join(out)
if new_text != text:
path.write_text(new_text)
print(f'Patched {patches} client.init call(s)')