forked from ethereum-optimism/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-op-dependencies.py
executable file
·101 lines (79 loc) · 2.55 KB
/
update-op-dependencies.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
#!/usr/bin/env python3
import os
import re
import argparse
import subprocess
MODULES = [
'optimism',
'simulators/optimism/l1ops',
'simulators/optimism/p2p',
'simulators/optimism/rpc',
'simulators/optimism/daisy-chain',
]
REPLACER_RE = r'replace github\.com/ethereum/go-ethereum (.*) => github.com/ethereum-optimism/op-geth'
VERSION_RE = r'github\.com/ethereum-optimism/op-geth@([va-f0-9\d\.\-]+)'
parser = argparse.ArgumentParser()
parser.add_argument('--version', help='version to upgrade to')
parser.add_argument('--geth', type=bool, help='update geth rather than op dependencies')
def main():
args = parser.parse_args()
if args.version is None:
raise Exception('Must specify a version.')
if args.geth:
update_geth(args)
else:
update_op_deps(args)
def update_geth(args):
for mod in MODULES:
should_update = False
with open(os.path.join(mod, 'go.mod')) as f:
for line in f:
if re.search(REPLACER_RE, line):
original_version = line.strip().split(' ')[2]
should_update = True
break
if not should_update:
continue
print(f'Updating {mod}')
run([
'go',
'mod',
'edit',
'-replace',
f'github.com/ethereum/go-ethereum@{original_version}=github.com/ethereum-optimism/op-geth@{args.version}'
], cwd=mod, check=True)
tidy(mod)
def update_op_deps(args):
for mod in MODULES:
needs = set()
with open(os.path.join(mod, 'go.mod')) as f:
for line in f:
if line.endswith('// indirect\n'):
continue
if not re.search(r'github.com/ethereum-optimism/optimism', line):
continue
dep = line.strip().split(' ')[0]
needs.add(dep)
print(f'Updating {mod}')
for need in needs:
go_get(mod, need, args.version)
tidy(mod)
def go_get(mod, dep, version, capture_output=False, check=True):
args = [
'go',
'get',
f'{dep}@{version}'
]
return run(args, cwd=mod, check=check, capture_output=capture_output)
def tidy(mod):
args = [
'go',
'mod',
'tidy'
]
run(args, cwd=mod, check=True)
def run(args, cwd=None, capture_output=False, check=True):
print(subprocess.list2cmdline(args))
return subprocess.run(args, cwd=cwd, check=check, capture_output=capture_output)
if __name__ == '__main__':
main()