forked from slizb/min-tfs-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
137 lines (111 loc) · 4.21 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
import os
from contextlib import contextmanager
from distutils.cmd import Command
from itertools import chain
from pathlib import Path
from shutil import copy2, rmtree
from subprocess import check_output
from typing import Any, List
from setuptools import find_namespace_packages, setup
from setuptools.command.build_py import build_py
ROOT_DIR = Path(os.path.dirname(os.path.realpath(__file__)))
PROTOBUF_INCLUDE_PATH = ROOT_DIR / "protobuf_srcs/"
OUTPUT_PATH = ROOT_DIR / "tensor_serving_client/"
PROTO_PATHS = [str(p) for p in PROTOBUF_INCLUDE_PATH.rglob("*.proto")]
@contextmanager
def cd(newdir):
prevdir = os.getcwd()
os.chdir(os.path.expanduser(newdir))
try:
yield
finally:
os.chdir(prevdir)
class CompileProtobufs(Command):
description = """
Compile .proto files in Tensorflow and Tensorflow Serving into python
libraries.
"""
user_options: List[Any] = []
def initialize_options(self):
...
def finalize_options(self):
...
def run(self) -> None:
pb_compile_command = [
"protoc",
f"-I={str(PROTOBUF_INCLUDE_PATH)}",
f"--python_out={str(OUTPUT_PATH)}",
]
with cd(str(PROTOBUF_INCLUDE_PATH)):
for proto_path in PROTO_PATHS:
check_output(pb_compile_command + [proto_path])
class CopyGRPCStubs(Command):
description = """
Copy GRPC Stubs from Tensorflow Serving APIs to the minified Tensorflow Serving APIs library
"""
user_options: List[Any] = []
def initialize_options(self):
...
def finalize_options(self):
...
def run(self):
predict_service_stub = (
PROTOBUF_INCLUDE_PATH / "tensorflow_serving/apis/prediction_service_pb2_grpc.py"
)
model_service_stub = (
PROTOBUF_INCLUDE_PATH / "tensorflow_serving/apis/model_service_pb2_grpc.py"
)
destination = ROOT_DIR / "tensor_serving_client/tensorflow_serving/apis/"
copy2(predict_service_stub, destination)
copy2(model_service_stub, destination)
class RestructureProtos(Command):
description = '''
Rearrange the repository and modify protobuf imports so that protobufs nested inside min_tfs_client namespace
'''
user_options: List[Any] = []
def initialize_options(self):
...
def finalize_options(self):
...
def run(self):
with cd(str(OUTPUT_PATH)):
# Move the protos inside of the min_tfs_client namespace
if os.path.isdir('min_tfs_client/tensorflow'):
rmtree('min_tfs_client/tensorflow')
os.rename('tensorflow', 'min_tfs_client/tensorflow')
if os.path.isdir('min_tfs_client/tensorflow_serving'):
rmtree('min_tfs_client/tensorflow_serving')
os.rename('tensorflow_serving', 'min_tfs_client/tensorflow_serving')
for file_path in chain((OUTPUT_PATH / 'min_tfs_client/tensorflow').rglob('*.py'), (OUTPUT_PATH / 'min_tfs_client/tensorflow_serving').rglob('*.py')):
filename = str(file_path)
with open(filename, 'r') as f:
new_text = f.read().replace('from tensorflow', 'from min_tfs_client.tensorflow')
with open(filename, 'w') as f:
f.write(new_text)
class BuildPyCommand(build_py):
"""Custom build command."""
def run(self):
self.run_command("compile_pb")
self.run_command("copy_grpc")
self.run_command("restructure")
build_py.run(self)
with open("README.md", "r") as fh:
long_description = fh.read()
setup(
name="min_tfs_client",
version="1.0.2.post1",
description="A minified Tensor Serving Client for Python",
long_description=long_description,
long_description_content_type="text/markdown",
cmdclass={
"compile_pb": CompileProtobufs,
"copy_grpc": CopyGRPCStubs,
"restructure": RestructureProtos,
"build_py": BuildPyCommand,
},
package_dir={"": "tensor_serving_client"},
packages=find_namespace_packages(where="tensor_serving_client"),
install_requires=["grpcio>=1.21", "protobuf>=3.8", "numpy>=1.16.4"],
tests_require=["pytest"],
extras_require={"dev": ["black==19.10b0", "flake8", "mypy", "pytest"]},
)