-
Notifications
You must be signed in to change notification settings - Fork 218
/
update_geth.py
198 lines (175 loc) · 7.46 KB
/
update_geth.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
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
190
191
192
193
194
195
196
197
198
"""
A script to automate adding support for new geth versions.
To add support for a geth version, run the following line from the py-geth directory,
substituting the version for the one you wish to add support for. Note that the 'v' in
the versioning is optional.
.. code-block:: shell
$ python update_geth.py v1_10_9
To introduce support for more than one version, pass in the versions in increasing
order, ending with the latest version.
.. code-block:: shell
$ python update_geth.py v1_10_7 v1_10_8 v1_10_9
Note: Always review your changes before committing as something may cause this existing
pattern to change at some point.
"""
import fileinput
import re
import sys
GETH_VERSION_REGEX = re.compile(r"v\d*_\d+") # v0_0_0 pattern
currently_supported_geth_versions = []
with open("tox.ini") as tox_ini:
for line_number, line in enumerate(tox_ini, start=1):
if line_number == 15:
# supported versions are near the beginning of the tox.ini file
break
if "install-geth" in line:
line.replace(" ", "")
circleci_python_versions = line[
line.find("py{") + 3 : line.find("}")
].split(",")
if GETH_VERSION_REGEX.search(line):
line = line.replace(" ", "") # clean space
line = line.replace("\n", "") # remove trailing indent
line = line.replace("\\", "") # remove the multiline backslash
line = line if line[-1] != "," else line[:-1]
for version in line.split(","):
currently_supported_geth_versions.append(version.strip())
LATEST_SUPPORTED_GETH_VERSION = currently_supported_geth_versions[-1]
LATEST_PYTHON_VERSION = circleci_python_versions[-1]
# .circleci/config.yml pattern
CIRCLE_CI_PATTERN = {
"jobs": "",
"workflow_test_jobs": "",
}
# geth/install.py pattern
GETH_INSTALL_PATTERN = {
"versions": "",
"installs": "",
"version<->install": "",
}
user_provided_versions = sys.argv[1:]
normalized_user_versions = []
for index, user_provided_version in enumerate(user_provided_versions):
if "v" not in user_provided_version:
user_provided_version = f"v{user_provided_version}"
normalized_user_versions.append(user_provided_version)
if (
not GETH_VERSION_REGEX.match(user_provided_version)
or len(user_provided_versions) == 0
):
raise ValueError("missing or improper format for provided geth versions")
if user_provided_version in currently_supported_geth_versions:
raise ValueError(
f"provided version is already supported: {user_provided_version}"
)
latest_user_provided_version = normalized_user_versions[-1]
# set up .circleci/config.yml pattern
if index > 0:
CIRCLE_CI_PATTERN["workflow_test_jobs"] += "\n"
for py_version in circleci_python_versions:
py_version_decimal = f"{py_version[0]}.{py_version[1:]}"
CIRCLE_CI_PATTERN["jobs"] += (
f" py{py_version}-install-geth-{user_provided_version}:\n"
f" <<: *common_go_steps\n"
" docker:\n"
f" - image: cimg/python:{py_version_decimal}\n"
" environment:\n"
f" GETH_VERSION: {user_provided_version.replace('_', '.')}\n"
f" TOXENV: py{py_version}-install-geth-{user_provided_version}\n"
)
CIRCLE_CI_PATTERN[
"workflow_test_jobs"
] += f"\n - py{py_version}-install-geth-{user_provided_version}"
# set up geth/install.py pattern
user_version_upper = user_provided_version.upper()
user_version_period = user_provided_version.replace("_", ".")
GETH_INSTALL_PATTERN[
"versions"
] += f'{user_version_upper} = "{user_version_period}"\n'
user_version_install = f"install_v{user_version_upper[1:]}"
GETH_INSTALL_PATTERN["installs"] += (
f"{user_version_install} = functools.partial("
f"install_from_source_code_release, {user_version_upper})\n"
)
GETH_INSTALL_PATTERN[
"version<->install"
] += f" {user_version_upper}: {user_version_install},\n"
# update .circleci/config.yml versions
with fileinput.FileInput(".circleci/config.yml", inplace=True) as cci_config:
for line in cci_config:
if (
f"TOXENV: py{LATEST_PYTHON_VERSION}-install-geth-{LATEST_SUPPORTED_GETH_VERSION}" # noqa: E501
) in line:
print(
f" TOXENV: py{LATEST_PYTHON_VERSION}-install-geth-"
f"{LATEST_SUPPORTED_GETH_VERSION}\n" + CIRCLE_CI_PATTERN["jobs"],
end="",
)
elif (
f"- py{LATEST_PYTHON_VERSION}-install-geth-{LATEST_SUPPORTED_GETH_VERSION}"
) in line:
print(
f" - py{LATEST_PYTHON_VERSION}-install-geth-{LATEST_SUPPORTED_GETH_VERSION}\n" # noqa: E501
+ CIRCLE_CI_PATTERN["workflow_test_jobs"]
)
else:
print(line, end="")
# update geth/install.py versions
with fileinput.FileInput("geth/install.py", inplace=True) as geth_install:
latest_supported_upper = LATEST_SUPPORTED_GETH_VERSION.upper()
latest_supported_period = LATEST_SUPPORTED_GETH_VERSION.replace("_", ".")
latest_version_install = f"install_v{latest_supported_upper[1:]}"
for line in geth_install:
if f'{latest_supported_upper} = "{latest_supported_period}"' in line:
print(
f'{latest_supported_upper} = "{latest_supported_period}"\n'
+ GETH_INSTALL_PATTERN["versions"],
end="",
)
elif f"{latest_version_install} = functools.partial" in line:
print(
f"{latest_version_install} = functools.partial("
f"install_from_source_code_release, {latest_supported_upper})\n"
+ GETH_INSTALL_PATTERN["installs"],
end="",
)
elif (f"{latest_supported_upper}: {latest_version_install}") in line:
print(
f" {latest_supported_upper}: {latest_version_install},\n"
+ GETH_INSTALL_PATTERN["version<->install"],
end="",
)
else:
print(line, end="")
# update versions in readme to the latest supported version
with fileinput.FileInput("README.md", inplace=True) as readme:
latest_supported_period = LATEST_SUPPORTED_GETH_VERSION.replace("_", ".")
latest_user_provided_period = latest_user_provided_version.replace("_", ".")
for line in readme:
print(
line.replace(latest_supported_period, latest_user_provided_period),
end="",
)
# update tox.ini versions
with fileinput.FileInput("tox.ini", inplace=True) as tox_ini:
all_versions = currently_supported_geth_versions + normalized_user_versions
write_versions = False
for line in tox_ini:
if write_versions:
print(" ", end="")
for num, v in enumerate(all_versions, start=1):
if num == len(all_versions):
print(f"{v} \\")
elif not num % 7:
print(f"{v}, \\\n ", end="")
else:
print(v, end=", ")
write_versions = False
else:
if "install-geth-{" in line:
write_versions = True
if GETH_VERSION_REGEX.search(line):
# clean up the older version lines
print(end="")
else:
print(line, end="")