forked from infiniflow/infinity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_rpc_code.py
53 lines (41 loc) · 1.74 KB
/
generate_rpc_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import subprocess
def get_path(executable):
try:
complete_path = subprocess.check_output(['which', executable])
return complete_path.decode('utf-8').strip()
except subprocess.CalledProcessError:
return None
def create_dir(paths: list):
for path in paths:
if not os.path.exists(path):
os.makedirs(path)
def fix_python_import_path(filename: str):
# from import infinity_brpc_pb2 as infinity__brpc__pb2
# to from . import infinity_brpc_pb2 as infinity__brpc__pb2
# replace with your actual file path
# Read the file
with open(filename, 'r') as file:
lines = file.readlines()
for i in range(7 if len(lines) >= 7 else len(lines)):
line = lines[i]
words = line.split()
if len(words) == 4 and words[0] == "import":
import_module, infinity_brpc_pb2, alias, infinity__brpc__pb2 = words
# Construct the new import line.
new_line = f"from . {import_module} {infinity_brpc_pb2} {alias} {infinity__brpc__pb2}\n"
lines[i] = new_line # replace the line
# Write back to the file
with open(filename, 'w') as file:
file.writelines(lines)
def generate_thrift():
parent_dir = os.path.dirname(os.getcwd())
python_dir = parent_dir + "/python/infinity/remote_thrift"
cpp_dir = parent_dir + "/src/network/infinity_thrift"
create_dir([python_dir, cpp_dir])
infinity_thrift_file = python_dir + "/infinity_thrift_rpc/infinity.thrift"
os.system("thrift --version")
os.system(f"thrift --out {python_dir} --gen py {infinity_thrift_file}")
os.system(f"thrift -r --out {cpp_dir} --gen cpp:no_skeleton {infinity_thrift_file}")
if __name__ == '__main__':
generate_thrift()