|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +from pathlib import Path |
| 6 | +from typing import Tuple, TypedDict |
| 7 | + |
| 8 | +import click |
| 9 | + |
| 10 | + |
| 11 | +class ProtobufFilePathInfo(TypedDict): |
| 12 | + dir: Path |
| 13 | + path: Path |
| 14 | + rel_path: Path |
| 15 | + |
| 16 | + |
| 17 | +@click.command() |
| 18 | +@click.option("--dry", is_flag=True, show_default=True, default=False, help="Do not write out the changes to the files.") |
| 19 | +@click.argument("root_dir", type=click.Path(exists=True)) |
| 20 | +def fix_protobuf_imports(root_dir, dry): |
| 21 | + """ |
| 22 | + A script to fix relative imports (from and to nested sub-directories) within compiled `*_pb2.py` Protobuf files. |
| 23 | + """ |
| 24 | + |
| 25 | + root_dir = Path(root_dir) |
| 26 | + |
| 27 | + def generate_lookup(path: Path) -> Tuple[str, ProtobufFilePathInfo]: |
| 28 | + name = path.name.split(".")[0] |
| 29 | + rel_path = path.relative_to(root_dir) |
| 30 | + directory = path.parent.relative_to(root_dir) |
| 31 | + |
| 32 | + return (name, {"dir": directory, "path": path, "rel_path": rel_path}) |
| 33 | + |
| 34 | + py_files = list(root_dir.glob("**/*_pb2.py")) |
| 35 | + pyi_files = list(root_dir.glob("**/*_pb2.pyi")) |
| 36 | + |
| 37 | + py_files_dictionary = {} |
| 38 | + for path in py_files: |
| 39 | + name, info = generate_lookup(path) |
| 40 | + py_files_dictionary[name] = info |
| 41 | + |
| 42 | + pyi_files_dictionary = {} |
| 43 | + for path in pyi_files: |
| 44 | + name, info = generate_lookup(path) |
| 45 | + pyi_files_dictionary[name] = info |
| 46 | + |
| 47 | + def fix_protobuf_import_in_line( |
| 48 | + original_line, referencing_info: ProtobufFilePathInfo, pyi=False |
| 49 | + ) -> str: |
| 50 | + line = original_line |
| 51 | + |
| 52 | + if pyi: |
| 53 | + m = re.search(r"^import\s([^\s\.]*_pb2)$", line) |
| 54 | + else: |
| 55 | + m = re.search(r"^import\s(\S*_pb2)\sas\s(.*)$", line) |
| 56 | + |
| 57 | + if m is not None: |
| 58 | + referenced_name = m.group(1) |
| 59 | + if pyi: |
| 60 | + referenced_alias = None |
| 61 | + else: |
| 62 | + referenced_alias = m.group(2) |
| 63 | + |
| 64 | + referenced_directory = py_files_dictionary[referenced_name]["dir"] |
| 65 | + relative_path_to_referenced_module = os.path.relpath( |
| 66 | + referenced_directory, referencing_info["dir"] |
| 67 | + ) |
| 68 | + |
| 69 | + uppath_levels = relative_path_to_referenced_module.count("..") |
| 70 | + |
| 71 | + original_line = line.replace("\n", "") |
| 72 | + |
| 73 | + downpath = ( |
| 74 | + relative_path_to_referenced_module.split("..")[-1] |
| 75 | + .replace("/", ".") |
| 76 | + .replace("\\", ".") |
| 77 | + ) |
| 78 | + if referenced_alias: |
| 79 | + line = f'from .{"." * uppath_levels}{downpath if downpath != "." else ""} import {referenced_name} as {referenced_alias}\n' |
| 80 | + else: |
| 81 | + line = f'from .{"." * uppath_levels}{downpath if downpath != "." else ""} import {referenced_name}\n' |
| 82 | + |
| 83 | + new_line = line.replace("\n", "") |
| 84 | + |
| 85 | + print(f'{referencing_info["rel_path"]}: "{original_line}" -> "{new_line}"') |
| 86 | + else: |
| 87 | + m = re.search(r"^from\s([^\s\.]+[\S]*)\simport\s(.*_pb2)$", line) |
| 88 | + |
| 89 | + if m is not None: |
| 90 | + import_path = m.group(1).replace(".", "/") |
| 91 | + |
| 92 | + referenced_directory = root_dir / import_path |
| 93 | + |
| 94 | + if referenced_directory.exists(): |
| 95 | + relative_path_to_root = os.path.relpath( |
| 96 | + root_dir, referencing_info["dir"] |
| 97 | + ) |
| 98 | + |
| 99 | + uppath_levels = relative_path_to_root.count("..") |
| 100 | + |
| 101 | + original_line = line.replace("\n", "") |
| 102 | + |
| 103 | + line = ( |
| 104 | + f'from .{"." * uppath_levels}{m.group(1)} import {m.group(2)}\n' |
| 105 | + ) |
| 106 | + |
| 107 | + new_line = line.replace("\n", "") |
| 108 | + |
| 109 | + print( |
| 110 | + f'{referencing_info["rel_path"]}: "{original_line}" -> "{new_line}"' |
| 111 | + ) |
| 112 | + |
| 113 | + return line |
| 114 | + |
| 115 | + def fix_protobuf_imports_in_file(name, info: ProtobufFilePathInfo, pyi=False): |
| 116 | + with open(info["path"], "r+" if not dry else "r") as f: |
| 117 | + lines = f.readlines() |
| 118 | + if not dry: |
| 119 | + f.seek(0) |
| 120 | + |
| 121 | + for line in lines: |
| 122 | + line = fix_protobuf_import_in_line(line, info, pyi) |
| 123 | + |
| 124 | + if not dry: |
| 125 | + f.writelines([line]) |
| 126 | + |
| 127 | + if not dry: |
| 128 | + f.truncate() |
| 129 | + f.close() |
| 130 | + |
| 131 | + for (name, info) in py_files_dictionary.items(): |
| 132 | + fix_protobuf_imports_in_file(name, info) |
| 133 | + |
| 134 | + for ( |
| 135 | + name, |
| 136 | + info, |
| 137 | + ) in pyi_files_dictionary.items(): |
| 138 | + fix_protobuf_imports_in_file(name, info, pyi=True) |
| 139 | + |
| 140 | +def main(): |
| 141 | + fix_protobuf_imports() |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + main() |
0 commit comments