|
| 1 | +""" |
| 2 | +Generate the language bindings from the proto files. |
| 3 | +""" |
| 4 | +import glob |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +from os.path import abspath, join, dirname |
| 8 | +from typing import List, Dict |
| 9 | +import pkg_resources |
| 10 | +from grpc_tools import protoc |
| 11 | + |
| 12 | + |
| 13 | +def get_language_dir(language_name: str) -> str: |
| 14 | + """ |
| 15 | + Get the directory for the given language SDK |
| 16 | + :param language_name: The language directory |
| 17 | + :return: Absolute path to the given language SDK |
| 18 | + """ |
| 19 | + return abspath(join(dirname(abspath(__file__)), '..', language_name)) |
| 20 | + |
| 21 | + |
| 22 | +def get_proto_files(dir_name: str = None) -> List[str]: |
| 23 | + dir_name = dir_name or get_language_dir('proto') |
| 24 | + proto_search_glob = join(dir_name, '**', '*.proto') |
| 25 | + return [abspath(file_path) for file_path in glob.glob(proto_search_glob, recursive=True)] |
| 26 | + |
| 27 | + |
| 28 | +def replace_lines_in_file(file_name: str, replace_lines: Dict[str, str]) -> None: |
| 29 | + with open(file_name, 'r') as fid: |
| 30 | + file_lines = fid.readlines() |
| 31 | + |
| 32 | + file_lines = [line.replace(key, value) for key, value in replace_lines.items() for line in file_lines] |
| 33 | + |
| 34 | + with open(file_name, 'w') as fid: |
| 35 | + fid.writelines(file_lines) |
| 36 | + |
| 37 | + |
| 38 | +def clean_proto_dir(language_proto_dir: str) -> None: |
| 39 | + try: |
| 40 | + shutil.rmtree(language_proto_dir) |
| 41 | + except: |
| 42 | + pass |
| 43 | + os.mkdir(language_proto_dir) |
| 44 | + |
| 45 | + |
| 46 | +def join_args(args: Dict[str, str]) -> str: |
| 47 | + return " ".join([f'--{key}="{value}"' for (key, value) in args.items()]) if args else '' |
| 48 | + |
| 49 | + |
| 50 | +def run_protoc(language_options: Dict[str, str] = None, |
| 51 | + custom_options: Dict[str, str] = None, |
| 52 | + proto_files: List[str] = None, |
| 53 | + plugin: str = None, |
| 54 | + protoc_executable: str = 'protoc') -> None: |
| 55 | + language_arg_string = join_args(language_options) |
| 56 | + proto_path_string = f'--proto_path="{get_language_dir("proto")}"' |
| 57 | + custom_options_string = join_args(custom_options) |
| 58 | + proto_files_string = " ".join(proto_files) |
| 59 | + plugin_string = f'--plugin={plugin}' if plugin else '' |
| 60 | + protoc_command = f'{protoc_executable} {plugin_string} {proto_path_string} {language_arg_string} {custom_options_string} {proto_files_string}' |
| 61 | + print(protoc_command) |
| 62 | + if os.system(protoc_command) != 0: |
| 63 | + raise Exception("Protoc failed") |
| 64 | + |
| 65 | + |
| 66 | +def update_golang(): |
| 67 | + go_path = get_language_dir('go') |
| 68 | + go_proto_path = join(go_path, 'okapiproto') |
| 69 | + clean_proto_dir(go_proto_path) |
| 70 | + run_protoc({'go_out': go_proto_path, 'go-grpc_out': go_proto_path}, |
| 71 | + {'go_opt': 'module=github.com/trinsic-id/okapiproto', |
| 72 | + 'go-grpc_opt': 'module=github.com/trinsic-id/okapiproto'}, |
| 73 | + get_proto_files()) |
| 74 | + # Remove example grpc |
| 75 | + os.remove(join(go_proto_path, 'examples_grpc.pb.go')) |
| 76 | + |
| 77 | + |
| 78 | +def update_ruby(): |
| 79 | + ruby_path = get_language_dir('ruby') |
| 80 | + ruby_proto_path = join(ruby_path, 'lib') |
| 81 | + # TODO - clean selectively |
| 82 | + run_protoc({'ruby_out': ruby_proto_path}, {}, get_proto_files()) |
| 83 | + # TODO - Ruby type specifications |
| 84 | + os.system(f"rbs prototype rb {join(ruby_proto_path, '**', '*.rb')} > {join(ruby_proto_path, 'signatures.rbs')}") |
| 85 | + # run_protoc({'rbs_out': ruby_proto_path}, {}, get_proto_files()) |
| 86 | + |
| 87 | + |
| 88 | +def update_java(): |
| 89 | + java_path = get_language_dir('java') |
| 90 | + java_proto_path = join(java_path, 'src', 'main', 'java') |
| 91 | + # TODO - clean_proto_dir(java_proto_path) |
| 92 | + run_protoc({'java_out': java_proto_path}, {}, get_proto_files(), |
| 93 | + plugin=r"C:\bin\protoc-gen-grpc-java-1.39.0-windows-x86_64.exe") |
| 94 | + # remove okapi pbmse |
| 95 | + shutil.rmtree(join(java_proto_path, 'trinsic', 'okapi')) |
| 96 | + |
| 97 | + |
| 98 | +def update_swift(): |
| 99 | + swift_path = get_language_dir('swift') |
| 100 | + swift_proto_path = join(swift_path, 'Okapi', 'Sources', 'OkapiSwift', 'proto') |
| 101 | + clean_proto_dir(swift_proto_path) |
| 102 | + run_protoc({'swift_out': swift_proto_path}, {'swift_opt': "Visibility=Public"}, get_proto_files()) |
| 103 | + |
| 104 | + |
| 105 | +def update_python(): |
| 106 | + """ |
| 107 | + Generate the protobuf interface files using the python library https://github.com/danielgtaylor/python-betterproto |
| 108 | + :return: |
| 109 | + """ |
| 110 | + # Remove everything under output directory |
| 111 | + python_proto_path = join(get_language_dir('python'), "proto") |
| 112 | + clean_proto_dir(python_proto_path) |
| 113 | + # Paths for proto compilation |
| 114 | + file_path = abspath(dirname(abspath(__file__))) |
| 115 | + base_path = abspath(join(file_path, '..', 'proto')) |
| 116 | + proto_file_path = abspath(join(base_path, "**", "*.proto")) |
| 117 | + # Come up with better locations, import google defaults from the package location (see code in protoc.main) |
| 118 | + proto_include = pkg_resources.resource_filename('grpc_tools', '_proto').replace("lib", "Lib") |
| 119 | + # Inject an empty python code file path to mimic the first argument. |
| 120 | + base_command = ['', '-I', get_language_dir('proto'), f'--python_betterproto_out={python_proto_path}'] |
| 121 | + base_command.extend(get_proto_files()) |
| 122 | + base_command.append(f'-I{proto_include}') |
| 123 | + protoc.main(base_command) |
| 124 | + |
| 125 | + |
| 126 | +def main(): |
| 127 | + update_golang() |
| 128 | + update_ruby() |
| 129 | + update_java() |
| 130 | + update_swift() |
| 131 | + update_python() |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + main() |
0 commit comments