-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththrift_setup.py
94 lines (63 loc) · 2.5 KB
/
thrift_setup.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from typing import List
import sys
import os
import shutil
from loguru import logger
def setup_logger():
log_format = ("<blue>{time:%s}:{process}</blue> <cyan>{name}:{line}</cyan> <level>{level: <8} | {message}</level>" %
("YYYY-MM-DD-HH:mm:ss.SSS"))
logger.remove()
logger.add(sys.stdout, level="INFO", colorize=True, format=log_format)
# logger.add("logs/linelib.log", level="DEBUG", format=log_format, rotation="1 MB", retention=10)
def mkdir(name: str):
if os.path.isdir(name):
shutil.rmtree(name)
logger.info("[!] Deleted old files")
os.mkdir(name)
logger.info(f"[*] Created dir: '{name}'")
def execute(dir_name: str, lang: str):
os.system("ls service | xargs -I {} thrift --out %s --gen %s service/{}" % (dir_name, lang))
logger.info("[*] Thrift executed successfully")
def get_line_services(name: str) -> List[str]:
services = [name + "/" + service for service in os.listdir(name) if service != "__init__.py"]
logger.info(f"[*] Services: {len(services)}")
return services
def get_service_files(path: str) -> List[str]:
return [path + "/" + file for file in os.listdir(path)]
def edit_ttypes(name: str, file: str):
with open(file, "r") as reader:
code = reader.read()
lines = code.split("\n")
imported_service = ""
for i in range(len(lines)):
line = lines[i]
if "import " in line and ".ttypes" in line:
imported_service = line[len("import "):line.find(".ttypes")]
lines[i] = line.replace(line, f"from {name} import {imported_service}")
with open(file, "w") as writer:
writer.write("\n".join(lines))
def edit_files(dir_name: str, service_files: List[str]):
for file in service_files:
name = file.rpartition("/")[-1]
if name in ["constants.py", "__init__.py"] or name.endswith("-remote"):
os.remove(file)
logger.info(f"[!] Removed: {file}")
elif name == "ttypes.py":
edit_ttypes(dir_name, file)
logger.info(f"[*] Edited: {file}")
def edit_services(name: str, services: List[str]):
for service in services:
edit_files(name, get_service_files(service))
def main():
name = "line_service"
# Deletes files if exists old files
# Creates 'line_service'
mkdir(name)
# Executes Thrift
execute(name, "py")
# Gets service dirs
services = get_line_services(name)
# Edits services
edit_services(name, services)
if __name__ == "__main__":
main()