-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
144 lines (112 loc) · 4.16 KB
/
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import re
import fileinput
import setuptools
import subprocess
from pathlib import Path
package_dir = "helloworld_proto"
def clean_build_directories():
os.system("rm -rf *.egg-info")
os.system("rm -rf dist")
os.system("rm -rf build")
os.system(f"rm -rf {package_dir}")
def generate_python_classes_from_proto():
protos_dir = "api"
os.system(f"mkdir {package_dir}")
python_out = f"./{package_dir}"
python_grpc_out = f"./{package_dir}"
inputs = f"./{protos_dir}"
files = list(map(str, list(Path(f"./{protos_dir}").rglob("*.proto"))))
files = remove_symlinks_to_protos(files) # protoc is failing on symlinks to already included protos
command = [
"python3", "-m", "grpc_tools.protoc",
f"-I {inputs}",
f"--python_out={python_out}",
f"--grpc_python_out={python_grpc_out}",
f"--mypy_out={python_out}",
f"--mypy_grpc_out={python_grpc_out}",
"--experimental_allow_proto3_optional",
*files
]
command = " ".join(command)
work_dir = os.getcwd()
exit_code = subprocess.Popen(command, cwd=work_dir, shell=True).wait()
if exit_code != 0:
print("Protobuf generation failed, please check logs above")
exit(exit_code)
def remove_symlinks_to_protos(proto_files):
clean_files = []
for proto_file in proto_files:
real_path = os.path.realpath(proto_file)
relative_real = os.path.relpath(real_path)
if os.path.islink(proto_file) and relative_real in proto_files:
print(f"Excluding file {proto_file} because it is a symlink for {relative_real}")
else:
clean_files.append(proto_file)
return clean_files
def insert_init_files():
for root, dirs, files in os.walk(f"./{package_dir}"):
init_file_path = os.path.join(os.path.abspath(root), "__init__.py")
with open(init_file_path, "w"):
pass
def fix_imports():
def rewrite_py_file(file_input):
for line in file_input:
match = re.search("(?<!google\.protobuf) import .*_pb2 as", line)
if match:
line = line.replace("from ", f"from {package_dir}.")
print(line, end="")
def rewrite_pyi_file(file_input):
for line in file_input:
matches = {
match for match in re.findall(r"[\w\.]*_pb2", line)
if not match.startswith("google.protobuf")
}
for match in matches:
line = line.replace(match, f"{package_dir}.{match}")
print(line, end="")
for root, dirs, files in os.walk(f"./{package_dir}"):
for file_name in files:
file_path = os.path.join(os.path.abspath(root), file_name)
with fileinput.input(file_path, inplace=True) as f:
if file_name.endswith(".py"):
rewrite_py_file(f)
elif file_name.endswith(".pyi"):
rewrite_pyi_file(f)
def setup():
required = ["googleapis-common-protos>=1.52.0"]
setuptools.setup(
name="helloworld-proto",
use_scm_version={
"version_scheme": "post-release",
"local_scheme": "no-local-version",
"relative_to": __file__,
},
setup_requires=["wheel", "setuptools_scm"],
author="Reface",
description="Proto example package",
url="https://github.com/RefaceAI/helloworld-proto",
packages=setuptools.find_packages(),
install_requires=required,
classifiers=[
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
],
package_data={
"": ["*.pyi"],
},
)
def run():
clean_build_directories()
generate_python_classes_from_proto()
fix_imports()
insert_init_files()
setup()
if __name__ == "__main__":
run()