-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·404 lines (345 loc) · 13.2 KB
/
generate.py
File metadata and controls
executable file
·404 lines (345 loc) · 13.2 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python3
from pathlib import Path
from typing import List, Sequence
import argparse
import shutil
import subprocess
class YDBProtoFile:
"""
YDBProtoFile is a proto file lying within YDB directory that
we have to patch in order to generate valid GRPC for connector.
"""
src_initial: str
src_patched: str
filepath: Path
def __init__(self, filepath: Path, go_package: str):
self.filepath = filepath
# preserve original content
with open(filepath, "r") as f:
self.src_initial = f.read()
# prepare patched version
lines_initial = self.src_initial.splitlines()
if "package Ydb;" in lines_initial:
self.src_patched = self.__patch_ydb_protofile(lines_initial, go_package)
elif "package Ydb.Issue;" in lines_initial:
self.src_patched = self.__patch_ydb_protofile(lines_initial, go_package)
elif "package Ydb.Formats;" in lines_initial:
self.src_patched = self.__patch_ydb_protofile(lines_initial, go_package)
elif "package NYql.NConnector.NApi;" in lines_initial:
self.src_patched = self.__patch_connector_protofile(
filepath, lines_initial, go_package
)
elif "package NYql;" in lines_initial:
self.src_patched = self.__patch_gateways_config_protofile(
filepath, lines_initial, go_package
)
print(self.src_patched)
else:
raise ValueError(f"unknown line pattern for {filepath}")
def __patch_ydb_protofile(
self, lines_initial: Sequence[str], go_package: str
) -> str:
import_line_pos = 5
import_line = f'option go_package = "{go_package}";'
lines_patched = (
lines_initial[:import_line_pos]
+ [import_line]
+ lines_initial[import_line_pos:]
)
return "\n".join(lines_patched)
def __patch_connector_protofile(
self, filepath: Path, lines_initial: Sequence[str], go_package: str
) -> str:
import_line_pos = self.__find_line_by_prefix(
filepath, lines_initial, "option go_package"
)
import_line = f'option go_package = "{go_package}";'
lines_patched = (
lines_initial[:import_line_pos]
+ [import_line]
+ lines_initial[import_line_pos + 1 :]
)
return "\n".join(lines_patched)
def __patch_gateways_config_protofile(
self, filepath: Path, lines_initial: Sequence[str], go_package: str
) -> str:
usefull_start_pos = self.__find_line_by_prefix(
filepath,
lines_initial,
"/////////// Generic gateway for the external data sources ////////////",
)
usefull_end_pos = self.__find_line_by_prefix(
filepath, lines_initial, "message TGenericClusterConfig"
)
lines_patched = (
['syntax = "proto3";']
+ lines_initial[:1]
+ [f'option go_package = "{go_package}";']
+ lines_initial[usefull_start_pos:usefull_end_pos]
)
lines_concatenated = "\n".join(lines_patched)
# we want protofile to look like it has proto3 syntax
lines_concatenated = lines_concatenated.replace("optional", "")
return lines_concatenated
def __find_line_by_prefix(
self, filepath: Path, lines_initial: Sequence[str], prefix: str
) -> int:
import_line_pos = None
for i, line in enumerate(lines_initial):
if line.startswith(prefix):
import_line_pos = i
break
if not import_line_pos:
raise ValueError(
f"unable to find import line in file {filepath}: {lines_initial}"
)
return import_line_pos
def patch(self):
with open(self.filepath, "w") as f:
f.write(self.src_patched)
def revert(self):
with open(self.filepath, "w") as f:
f.write(self.src_initial)
# YDB's protofiles this project depends on.
# They will be temporarily patched during the code generation.
source_params = [
(
"ydb/public/api/protos/ydb_value.proto",
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb",
),
(
"ydb/public/api/protos/ydb_status_codes.proto",
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb",
),
(
"ydb/public/api/protos/ydb_formats.proto",
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Formats",
),
(
"ydb/public/api/protos/ydb_issue_message.proto",
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Issue",
),
(
"yql/essentials/providers/common/proto/gateways_config.proto",
"github.com/ydb-platform/fq-connector-go/api/common",
),
(
"ydb/library/yql/providers/generic/connector/api/service/connector.proto",
"github.com/ydb-platform/fq-connector-go/api/service",
),
(
"ydb/library/yql/providers/generic/connector/api/service/protos/connector.proto",
"github.com/ydb-platform/fq-connector-go/api/service/protos",
),
(
"ydb/library/yql/providers/generic/connector/api/service/protos/error.proto",
"github.com/ydb-platform/fq-connector-go/api/service/protos",
),
]
def __call_subprocess(cmd: List[str]):
formatted = "\n".join(map(str, cmd))
print(f"Running command:\n{formatted}")
process = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
exit_code = process.wait()
if exit_code != 0:
raise Exception(
f'Subprocess failure: exit_code={exit_code} stdout={stdout.decode("utf-8")}, stderr={stderr.decode("utf-8")}'
)
if stdout:
print(stdout.decode("utf-8"))
if stderr:
print(stderr.decode("utf-8"))
return stdout
def __find_executable(name: str) -> Path:
import os
# Check in $HOME/.local/bin first
home_dir = Path.home()
local_bin = home_dir / ".local" / "bin" / name
if local_bin.exists() and os.access(local_bin, os.X_OK):
return local_bin
# Fall back to system PATH
result = shutil.which(name)
if not result:
raise ValueError(
f'executable "{name}" was not found in path, you should install it first'
)
return Path(result)
def __check_protoc_version(protoc_binary: Path, min_version: tuple = (3, 15, 0)):
"""Check if protoc version meets minimum requirements."""
try:
result = subprocess.run(
[str(protoc_binary), "--version"],
capture_output=True,
text=True,
check=True
)
# Output format: "libprotoc 3.15.0" or similar
version_str = result.stdout.strip().split()[-1]
version_parts = tuple(int(x) for x in version_str.split('.'))
if version_parts < min_version:
min_version_str = '.'.join(str(x) for x in min_version)
raise ValueError(
f'protoc version {version_str} is too old. '
f'Minimum required version is {min_version_str}'
)
print(f"Using protoc version {version_str}")
except (subprocess.CalledProcessError, ValueError, IndexError) as e:
raise ValueError(f"Failed to check protoc version: {e}")
def run_protoc(
proto_files: Sequence[Path],
target_dir: Path,
go_module: str,
includes: Sequence[Path],
with_grpc: bool,
):
# compile protoc from Arcadia
protoc_binary = __find_executable("protoc")
protoc_gen_go_binary = __find_executable("protoc-gen-go")
protoc_gen_go_grpc_binary = __find_executable("protoc-gen-go-grpc")
# Check protoc version
__check_protoc_version(protoc_binary)
# build protoc args
cmd = [
protoc_binary,
f"--plugin=protoc-gen-go={protoc_gen_go_binary}",
f"--plugin=protoc-gen-go-grpc={protoc_gen_go_grpc_binary}",
f"--go_out={target_dir}",
f"--go_opt=module={go_module}",
]
if with_grpc:
cmd.extend(
[
f"--go-grpc_out={target_dir}",
f"--go-grpc_opt=module={go_module}",
]
)
for include in includes:
cmd.append(f"-I{include}")
cmd.extend(proto_files)
__call_subprocess(cmd)
def parse_args():
parser = argparse.ArgumentParser(
prog="generate",
description="""
Script for Go Protobuf API generation.
It takes protofiles from YDB repository and generates Go code in fq-connector-go repository.
""",
)
required_args = parser.add_argument_group("required named arguments")
required_args.add_argument(
"--ydb-repo",
type=str,
help="Path to the local copy of github.com/ydb-platform/ydb",
required=True,
)
required_args.add_argument(
"--connector-repo",
type=str,
help="Path to the local copy of github.com/ydb-platform/fq-connector-go",
required=True,
)
return parser.parse_args()
def main():
args = parse_args()
# derive Arcadia's root
ydb_github_root = Path(args.ydb_repo)
if not ydb_github_root.exists():
raise ValueError(f"path {ydb_github_root} does not exist")
connector_github_root = Path(args.connector_repo)
if not connector_github_root.exists():
raise ValueError(f"path {connector_github_root} does not exist")
protobuf_includes = ydb_github_root.joinpath(
"contrib/libs/protobuf/src/google/protobuf"
)
if not protobuf_includes.exists():
raise ValueError(f"path {protobuf_includes} does not exist")
ydb_source_files = [
YDBProtoFile(ydb_github_root.joinpath(param[0]), param[1])
for param in source_params
]
# Patch YDB sources
for f in ydb_source_files:
f.patch()
try:
# Generate YQL Generic protofiles
run_protoc(
[
ydb_github_root.joinpath(
"yql/essentials/providers/common/proto/gateways_config.proto"
),
],
connector_github_root.joinpath("api"),
"github.com/ydb-platform/fq-connector-go/api",
[ydb_github_root, protobuf_includes],
True,
)
# Generate Connector API
run_protoc(
ydb_github_root.joinpath(
"ydb/library/yql/providers/generic/connector/api"
).rglob("*.proto"),
connector_github_root.joinpath("api"),
"github.com/ydb-platform/fq-connector-go/api",
[ydb_github_root, protobuf_includes],
True,
)
# Generate config protofiles
run_protoc(
connector_github_root.joinpath("app/config").rglob("*.proto"),
connector_github_root.joinpath("app/config"),
"github.com/ydb-platform/fq-connector-go/app/config",
[ydb_github_root, connector_github_root, protobuf_includes],
False,
)
# Generate datasource-specific types (split descriptions, helpers and so on)
run_protoc(
[connector_github_root.joinpath("app/server/datasource/rdbms/ydb/split.proto")],
connector_github_root.joinpath("app/server/datasource/rdbms/ydb"),
"github.com/ydb-platform/fq-connector-go/app/server/datasource/rdbms/ydb",
[connector_github_root, protobuf_includes],
False,
)
run_protoc(
[connector_github_root.joinpath("app/server/datasource/rdbms/ydb/table_metadata_cache/value.proto")],
connector_github_root.joinpath("app/server/datasource/rdbms/ydb/table_metadata_cache"),
"github.com/ydb-platform/fq-connector-go/app/server/datasource/rdbms/ydb/table_metadata_cache",
[ydb_github_root, connector_github_root, protobuf_includes],
False,
)
run_protoc(
connector_github_root.joinpath("app/server/datasource/rdbms/postgresql").rglob(
"*.proto"
),
connector_github_root.joinpath("app/server/datasource/rdbms/postgresql"),
"github.com/ydb-platform/fq-connector-go/app/server/datasource/rdbms/postgresql",
[connector_github_root, protobuf_includes],
False,
)
# Generate Cloud Logging protofiles
run_protoc(
connector_github_root.joinpath("app/server/datasource/rdbms/logging").rglob(
"*.proto"
),
connector_github_root.joinpath("app/server/datasource/rdbms/logging"),
"github.com/ydb-platform/fq-connector-go/app/server/datasource/rdbms/logging",
[ydb_github_root, connector_github_root, protobuf_includes],
False,
)
# Generate Connector Observation API
run_protoc(
connector_github_root.joinpath(
"api/observation"
).rglob("*.proto"),
connector_github_root.joinpath("api"),
"github.com/ydb-platform/fq-connector-go/api",
[ydb_github_root, connector_github_root, protobuf_includes],
True,
)
finally:
# Revert changes in YDB sources
for f in ydb_source_files:
f.revert()
pass
if __name__ == "__main__":
main()