|
| 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() |
0 commit comments