Skip to content

Commit 738fd67

Browse files
SDK-147: use python for compiling proto files, golang context support (#155)
1 parent 839b227 commit 738fd67

File tree

6 files changed

+161
-124
lines changed

6 files changed

+161
-124
lines changed

devops/generate_proto_files.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import glob
2+
import os
3+
import shutil
4+
from os.path import abspath, join, dirname
5+
from typing import List, Dict
6+
7+
8+
def get_language_dir(language_name: str) -> str:
9+
return abspath(join(dirname(abspath(__file__)), '..', language_name))
10+
11+
12+
def get_proto_files(dir_name: str = None) -> List[str]:
13+
dir_name = dir_name or get_language_dir('proto')
14+
proto_search_glob = join(dir_name, '**', '*.proto')
15+
return [abspath(file_path) for file_path in glob.glob(proto_search_glob, recursive=True)]
16+
17+
18+
def replace_lines_in_file(file_name: str, replace_lines: Dict[str, str]) -> None:
19+
with open(file_name, 'r') as fid:
20+
file_lines = fid.readlines()
21+
22+
file_lines = [line.replace(key, value) for key, value in replace_lines.items() for line in file_lines]
23+
24+
with open(file_name, 'w') as fid:
25+
fid.writelines(file_lines)
26+
27+
28+
def clean_proto_dir(language_proto_dir: str) -> None:
29+
try:
30+
shutil.rmtree(language_proto_dir)
31+
except:
32+
pass
33+
os.mkdir(language_proto_dir)
34+
35+
36+
def join_args(args: Dict[str, str]) -> str:
37+
return " ".join([f'--{key}="{value}"' for (key, value) in args.items()]) if args else ''
38+
39+
40+
def run_protoc(language_options: Dict[str, str] = None,
41+
custom_options: Dict[str, str] = None,
42+
proto_files: List[str] = None,
43+
plugin: str = None,
44+
protoc_executable: str = 'protoc') -> None:
45+
language_arg_string = join_args(language_options)
46+
proto_path_string = f'--proto_path="{get_language_dir("proto")}"'
47+
custom_options_string = join_args(custom_options)
48+
proto_files_string = " ".join(proto_files)
49+
plugin_string = f'--plugin={plugin}' if plugin else ''
50+
protoc_command = f'{protoc_executable} {plugin_string} {proto_path_string} {language_arg_string} {custom_options_string} {proto_files_string}'
51+
print(protoc_command)
52+
if os.system(protoc_command) != 0:
53+
raise Exception("Protoc failed")
54+
55+
56+
def update_golang():
57+
go_path = get_language_dir('go')
58+
go_proto_path = join(go_path, 'proto')
59+
clean_proto_dir(go_proto_path)
60+
run_protoc({'go_out': go_proto_path, 'go-grpc_out': go_proto_path},
61+
{'go_opt': 'module=github.com/trinsic-id/sdk', 'go-grpc_opt': 'module=github.com/trinsic-id/sdk'},
62+
get_proto_files())
63+
# Remove okapi proto folder
64+
shutil.rmtree(join(go_proto_path, 'go'))
65+
# find and replace the sdk proto with okapi proto
66+
replace_pairs = {'okapiproto "github.com/trinsic-id/sdk/go/okapiproto"':
67+
'okapiproto "github.com/trinsic-id/okapi/go/okapiproto"'}
68+
for file_name in glob.glob(join(go_proto_path, '*.go')):
69+
replace_lines_in_file(file_name, replace_pairs)
70+
71+
72+
def update_ruby():
73+
ruby_path = get_language_dir('ruby')
74+
ruby_proto_path = join(ruby_path, 'lib')
75+
# TODO - clean selectively
76+
run_protoc({'ruby_out': ruby_proto_path, 'grpc_out': ruby_proto_path}, {}, get_proto_files(),
77+
protoc_executable='grpc_tools_ruby_protoc')
78+
# TODO - Ruby type specifications
79+
80+
81+
def update_java():
82+
java_path = get_language_dir('java')
83+
java_proto_path = join(java_path, 'src', 'main', 'java')
84+
# TODO - clean_proto_dir(java_proto_path)
85+
run_protoc({'java_out': java_proto_path}, {}, get_proto_files(),
86+
plugin=r"C:\bin\protoc-gen-grpc-java-1.39.0-windows-x86_64.exe")
87+
# remove okapi pbmse
88+
shutil.rmtree(join(java_proto_path, 'trinsic', 'okapi'))
89+
90+
91+
def main():
92+
update_golang()
93+
update_ruby()
94+
update_java()
95+
96+
97+
if __name__ == "__main__":
98+
main()

go/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ go 1.16
44

55
require (
66
github.com/davecgh/go-spew v1.1.1 // indirect
7-
github.com/golang/protobuf v1.5.2
87
github.com/stretchr/testify v1.7.0
9-
github.com/trinsic-id/okapi/go v0.0.0-20211022163741-691fe8721335
8+
github.com/trinsic-id/okapi/go v0.0.0-20211022175546-f12ecb2812cb
109
google.golang.org/grpc v1.41.0
1110
google.golang.org/protobuf v1.27.1
1211
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect

go/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
7878
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
7979
github.com/trinsic-id/okapi/go v0.0.0-20211022163741-691fe8721335 h1:rK/tJLP611yLpYgnoxeVHVLUoSXncym90WMnv1y/qfg=
8080
github.com/trinsic-id/okapi/go v0.0.0-20211022163741-691fe8721335/go.mod h1:y2FOcdxdwphJl2D206kVPvKdskMm/Ro6WrnQv/nOCos=
81+
github.com/trinsic-id/okapi/go v0.0.0-20211022175546-f12ecb2812cb h1:c/t8LnGU7DCI65WE3Vt5QHP3wXaGwp2ikQLa6fvkSdI=
82+
github.com/trinsic-id/okapi/go v0.0.0-20211022175546-f12ecb2812cb/go.mod h1:y2FOcdxdwphJl2D206kVPvKdskMm/Ro6WrnQv/nOCos=
8183
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
8284
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
8385
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

0 commit comments

Comments
 (0)