-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenerate.py
executable file
·313 lines (261 loc) · 9.35 KB
/
generate.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
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
#!/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 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_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",
),
]
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:
result = shutil.which(name)
if not result:
raise ValueError(
f'executable "{name}" was not found in path, you should install it first'
)
return result
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")
# 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,
)
finally:
# Revert changes in YDB sources
for f in ydb_source_files:
f.revert()
pass
if __name__ == "__main__":
main()