-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathbomupgrader.py
More file actions
285 lines (256 loc) · 10.8 KB
/
bomupgrader.py
File metadata and controls
285 lines (256 loc) · 10.8 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import errno
import logging
import os
import re
import subprocess
import sys
"""
This Python script is used for upgrading the GCP-BOM in BeamModulePlugin.
Specifically, it
1. preprocessing BeamModulePlugin.groovy to decide the dependencies need to sync
2. generate an empty Maven project to fetch the exact target versions to change
3. Write back to BeamModulePlugin.groovy
4. Update libraries-bom version on sdks/java/container/license_scripts/dep_urls_java.yaml
There are few reasons we need to declare the version numbers:
1. Sync the dependency that not included in GCP-BOM with those included with BOM
For example, "com.google.cloud:google-cloud-spanner" does while "com.google.cloud:google-cloud-spanner:():test" doesn't
2. There are Beam artifacts not depending on GCP-BOM but used dependency managed
by GCP-BOM.
Refer to https://github.com/googleapis/java-cloud-bom/tags for the dependency
versions managed by gcp-cloud-bom
"""
# To format: yapf --style sdks/python/setup.cfg --in-place scripts/tools/bomupgrader.py
class BeamModulePluginProcessor:
# Known dependencies managed by GCP BOM and also used by Beam.
# We only need to have one dependency for each project to figure out the target version
KNOWN_DEPS = {
"arrow": "org.apache.arrow:arrow-memory-core",
"gax": "com.google.api:gax",
"google_cloud_spanner": "com.google.cloud:google-cloud-spanner",
"grpc":
"io.grpc:grpc-netty", # use "grpc-netty" to pick up proper netty version
"netty": "io.netty:netty-transport",
"opentelemetry": "io.opentelemetry:opentelemetry-sdk",
"protobuf": "com.google.protobuf:protobuf-java"
}
# dependencies managed by GCP-BOM that used the dependencies in KNOWN_DEPS
# So we need to add it to the example project to get the version to sync
OTHER_CONSTRANTS = [
"com.google.cloud:google-cloud-bigquery", # for arrow
'com.google.cloud:google-cloud-storage', # for google-api-services-storage
'com.google.cloud:google-cloud-resourcemanager', # for google-api-services-cloudresourcemanager
'com.google.cloud:google-cloud-datastore', # for google-cloud-dataflow-java-proto-library-all
]
# TODO: the logic can be generalized to support multiple BOM
ANCHOR = re.compile(
r'\s*//\s*\[bomupgrader\] determined by: (\S+), consistent with: google_cloud_platform_libraries_bom'
)
# e.g. def grpc_version = "1.61.0"
VERSION_STRING = re.compile(r'^\s*def (\w+)_version\s*=\s*[\'"](\S+)[\'"]')
SINGLE_VERSION_STRING = re.compile(
r'.+:\s*[\'"]([\w\-.]+:[\w\-.]+):(.+)[\'"],\s*//\s*\[bomupgrader\] sets version'
)
BOM_VERSION_STRING = re.compile(
r'\s*google_cloud_platform_libraries_bom\s*:\s*[\'"]com\.google\.cloud:libraries-bom:([0-9\.]+)[\'"],?'
)
BUILD_DIR = 'build/dependencyResolver'
BEAMMPLG_PATH = 'buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy'
LICENSE_SC_PATH = 'sdks/java/container/license_scripts/dep_urls_java.yaml'
GRADLE_TEMPLATE = """
plugins { id 'java' }
repositories { mavenCentral() }
dependencies {
implementation platform('com.google.cloud:libraries-bom:%s')
%s
}
configurations.implementation.canBeResolved = true
"""
def __init__(self, bom_version, project_root='.', runnable=None):
self.bom_version = bom_version
self.project_root = project_root
self.runnable = runnable or os.path.abspath('gradlew')
logging.info('-----Read BeamModulePlugin-----')
with open(os.path.join(project_root, self.BEAMMPLG_PATH), 'r') as fin:
self.original_lines = fin.readlines()
# e.g. {"io.grpc:grpc-netty", "1.61.0"}
self.dep_versions = {}
self.dep_versions_current = {}
# dependencies managed by bomupgrader. They are declared inline in BeamModulePlugin,
# different from KNOWN_DEPS which first define a version
self.set_deps = {}
self.set_deps_current = {}
@staticmethod
def resolve_actual_dep(line, id):
"""Resolve actual dependency from dependencyTree line"""
idx = line.find(id + ':')
if idx == -1: return "" # not found
dep_and_other = line[idx + len(id) + 1:].split()
try:
jdx = dep_and_other.index('->')
ver = dep_and_other[jdx + 1]
except ValueError:
# there might be multiple ':', e.g. come.group.id:some-package:test:1.2.3
ver = dep_and_other[0].split(':')[-1]
return ver
def check_dependencies(self):
"""Check dependencies in KNOWN_DEPS are found in BeamModulePlugin, and vice versa."""
logging.info("-----check dependency defs in BeamModulePlugin-----")
found_deps = {}
for idx, line in enumerate(self.original_lines):
m = self.ANCHOR.match(line)
if m:
n = self.VERSION_STRING.search(self.original_lines[idx + 1])
if not n:
raise RuntimeError(
"Version definition not found after anchor comment. Try standardize it."
)
found_deps[n.group(1)] = n.group(2)
continue
m = self.SINGLE_VERSION_STRING.match(line)
if m:
self.set_deps_current[m.group(1)] = m.group(2)
assert sorted(self.KNOWN_DEPS.keys()) == sorted(found_deps.keys()), f"expect {self.KNOWN_DEPS.keys()} == {found_deps.keys()}"
self.dep_versions_current = {
self.KNOWN_DEPS[k]: v
for k, v in found_deps.items()
}
def prepare_gradle(self, bom_version):
logging.info("-----prepare build.gradle for example project-----")
try:
os.makedirs(self.BUILD_DIR)
except OSError as e:
if e.errno != errno.EEXIST:
raise
deps = []
for dep in list(self.KNOWN_DEPS.values()) + self.OTHER_CONSTRANTS + list(
self.set_deps_current.keys()):
deps.append(f"implementation '{dep}'")
gradle_file = self.GRADLE_TEMPLATE % (bom_version, "\n".join(deps))
with open(os.path.join(self.BUILD_DIR, 'build.gradle'), 'w') as fout:
fout.write(gradle_file)
# we need a settings.gradle
with open(os.path.join(self.BUILD_DIR, 'settings.gradle'), 'w') as fout:
fout.write('\n')
def resolve(self):
logging.info("-----resolve dependency-----")
subp = subprocess.run([
self.runnable,
*(
'-q dependencies --configuration implementation --console=plain'.
split())
],
cwd=self.BUILD_DIR,
stdout=subprocess.PIPE)
result = subp.stdout.decode('utf-8')
# example line: | +--- com.google.guava:guava:32.1.3-android -> 32.1.3-jre (*)
logging.debug(result)
get_dep_line = re.compile('\s+([\w\-.]+:[\w\-.]+):(.+)')
for line in result.splitlines():
# search self.set_deps version
m = get_dep_line.search(line)
if m and m.group(1) in self.set_deps_current:
ver = self.resolve_actual_dep(line, m.group(1))
self.set_deps[m.group(1)] = ver
continue
# search KNOWN_DEPS version
for id in self.KNOWN_DEPS.values():
if id in self.dep_versions: continue
ver = self.resolve_actual_dep(line, id)
if ver:
self.dep_versions[id] = ver
break
if len(self.dep_versions) < len(self.KNOWN_DEPS):
logging.warning(
"Warning: not all dependencies are resolved: %s", self.dep_versions)
logging.info(result)
if len(self.set_deps) < len(self.set_deps_current):
logging.warning(
"Warning: not all dependencies are resolved: %s", self.set_deps)
logging.info(result)
def write_back(self):
logging.info("-----Update BeamModulePlugin-----")
# make a shallow copy
self.target_lines = list(self.original_lines)
found_bom = False
for idx, line in enumerate(self.original_lines):
m = self.ANCHOR.match(line)
if m:
n = self.VERSION_STRING.search(self.original_lines[idx + 1])
if not n:
raise RuntimeError(
"Version definition not found after anchor comment. Try standardize it."
)
id = self.KNOWN_DEPS[n.group(1)]
new_v = self.dep_versions[id]
old_v = self.dep_versions_current[id]
if new_v != old_v:
self.target_lines[idx + 1] = self.original_lines[idx + 1].replace(
old_v, new_v)
logging.info('Changed %s: %s -> %s', id, old_v, new_v)
else:
logging.info('Unchanged: %s:%s', id, new_v)
continue
# single_ver replace
m = self.SINGLE_VERSION_STRING.match(line)
if m:
id = m.group(1)
old_v = m.group(2)
new_v = self.set_deps[id]
if new_v != old_v:
self.target_lines[idx] = self.original_lines[idx].replace(
old_v, new_v)
logging.info('Changed %s: %s -> %s', id, old_v, new_v)
else:
logging.info('Unchanged: %s:%s', id, new_v)
# replace GCP BOM version
n = self.BOM_VERSION_STRING.match(line)
if n:
self.target_lines[idx] = self.original_lines[idx].replace(
n.group(1), self.bom_version)
found_bom = True
if not found_bom:
logging.warning(
'GCP_BOM version declaration not found in BeamModulePlugin')
with open(os.path.join(self.project_root, self.BEAMMPLG_PATH), 'w') as fout:
for line in self.target_lines:
fout.write(line)
def write_license_script(self):
logging.info("-----Update dep_urls_java.yaml-----")
with open(os.path.join(self.project_root, self.LICENSE_SC_PATH),
'r') as fin:
lines = fin.readlines()
with open(os.path.join(self.project_root, self.LICENSE_SC_PATH),
'w') as fout:
for idx, line in enumerate(lines):
if line.strip() == 'libraries-bom:':
lines[idx + 1] = re.sub(
r'[\'"]\d[\.\d]+[\'"]', f"'{self.bom_version}'", lines[idx + 1])
fout.write(line)
def run(self):
self.check_dependencies()
self.prepare_gradle(self.bom_version)
self.resolve()
self.write_back()
self.write_license_script()
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
if len(sys.argv) < 2:
print("Usage: python scripts/tools/bomupgrader.py target_version")
exit(1)
processor = BeamModulePluginProcessor(sys.argv[1])
processor.run()