|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +from typing import Generator |
| 4 | + |
| 5 | +os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python" |
| 6 | + |
| 7 | +root_path = os.path.dirname(os.path.realpath(__file__)) |
| 8 | +inputs_path = os.path.join(root_path, 'inputs') |
| 9 | + |
| 10 | +if os.name == 'nt': |
| 11 | + plugin_path = os.path.join(root_path, '..', 'plugin.bat') |
| 12 | +else: |
| 13 | + plugin_path = os.path.join(root_path, '..', 'plugin.py') |
| 14 | + |
| 15 | + |
| 16 | +def get_files(path, end: str) -> Generator[str, None, None]: |
| 17 | + for r, dirs, files in os.walk(path): |
| 18 | + for filename in [f for f in files if f.endswith(end)]: |
| 19 | + yield os.path.join(r, filename) |
| 20 | + |
| 21 | + |
| 22 | +def get_directories(path): |
| 23 | + for root, directories, files in os.walk(path): |
| 24 | + for directory in directories: |
| 25 | + yield directory |
| 26 | + |
| 27 | + |
| 28 | +def relative(file: str, path: str): |
| 29 | + return os.path.join(os.path.dirname(file), path) |
| 30 | + |
| 31 | + |
| 32 | +def read_relative(file: str, path: str): |
| 33 | + with open(relative(file, path)) as fh: |
| 34 | + return fh.read() |
| 35 | + |
| 36 | + |
| 37 | +def protoc_plugin(path: str, output_dir: str): |
| 38 | + subprocess.run( |
| 39 | + f"protoc --plugin=protoc-gen-custom={plugin_path} --custom_out={output_dir} --proto_path={path} {path}/*.proto", |
| 40 | + shell=True, |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def protoc_reference(path: str, output_dir: str): |
| 45 | + subprocess.run(f"protoc --python_out={output_dir} --proto_path={path} {path}/*.proto", shell=True) |
| 46 | + |
0 commit comments