|
1 | 1 | import os
|
| 2 | +import pathlib |
| 3 | +import shutil |
| 4 | +from typing import List |
| 5 | + |
2 | 6 | from grpc_tools import command
|
3 | 7 |
|
4 |
| -command.build_package_protos('.') |
5 | 8 |
|
6 |
| -init_files_to_create = [] |
| 9 | +def files_filter(dir, items: List[str]) -> List[str]: |
| 10 | + ignored_names = ['.git'] |
| 11 | + |
| 12 | + ignore = [] |
| 13 | + for item in items: |
| 14 | + fullpath = os.path.join(dir, item) |
| 15 | + if os.path.isdir(fullpath) and item not in ignored_names: |
| 16 | + continue |
| 17 | + if item.endswith(".proto"): |
| 18 | + continue |
| 19 | + ignore.append(item) |
| 20 | + |
| 21 | + return ignore |
| 22 | + |
| 23 | + |
| 24 | +def create_init_files(rootdirpath: str): |
| 25 | + for root, _dirs, _files in os.walk(rootdirpath): |
| 26 | + init_path = pathlib.Path(os.path.join(root, '__init__.py')) |
| 27 | + if not init_path.exists(): |
| 28 | + init_path.touch() |
| 29 | + |
| 30 | + |
| 31 | +def remove_protos(rootdirpath: str): |
| 32 | + for root, _dirs, files in os.walk(rootdirpath): |
| 33 | + for file in files: |
| 34 | + if file.endswith(".proto"): |
| 35 | + os.remove(os.path.join(root, file)) |
| 36 | + |
| 37 | + |
| 38 | +def fix_file_contents(rootdir: str): |
| 39 | + flake_ignore_line = "# flake8: " + "noqa" # prevent ignore the file |
| 40 | + |
| 41 | + for dirpath, _, fnames in os.walk(rootdir): |
| 42 | + for fname in fnames: |
| 43 | + if not fname.endswith(".py"): |
| 44 | + continue |
| 45 | + |
| 46 | + with open(os.path.join(dirpath, fname), 'r+t') as f: |
| 47 | + content = f.read() |
| 48 | + |
| 49 | + # Fix imports |
| 50 | + content = content.replace("from protos", "from ydb._grpc.protos") |
| 51 | + |
| 52 | + # Add ignore style check |
| 53 | + content = content.replace("# -*- coding: utf-8 -*-", "# -*- coding: utf-8 -*-\n" + flake_ignore_line) |
| 54 | + f.seek(0) |
| 55 | + f.write(content) |
| 56 | + |
| 57 | + |
| 58 | +def generate_protobuf(src_proto_dir: str, dst_dir: str): |
| 59 | + shutil.rmtree(dst_dir, ignore_errors=True) |
7 | 60 |
|
8 |
| -for root, dirs, files in os.walk('kikimr'): |
9 |
| - if '__init__.py' in files: |
10 |
| - continue |
| 61 | + shutil.copytree(src_proto_dir, dst_dir, ignore=files_filter) |
| 62 | + create_init_files(dst_dir) |
11 | 63 |
|
12 |
| - init_files_to_create.append( |
13 |
| - os.path.join( |
14 |
| - root, |
15 |
| - '__init__.py' |
16 |
| - ) |
17 |
| - ) |
| 64 | + command.build_package_protos(dst_dir) |
| 65 | + remove_protos(dst_dir) |
| 66 | + fix_file_contents(dst_dir) |
18 | 67 |
|
19 | 68 |
|
20 |
| -for filename in init_files_to_create: |
21 |
| - with open(filename, 'w') as f: |
22 |
| - pass |
| 69 | +if __name__ == '__main__': |
| 70 | + generate_protobuf("ydb-api-protos", "ydb/_grpc") |
0 commit comments