forked from forseti-security/forseti-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_protos.py
executable file
·121 lines (99 loc) · 3.77 KB
/
build_protos.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
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A script to prepare the source tree for building."""
import argparse
import logging
import os
import subprocess
def is_grpc_service_dir(files):
"""Returns true iff the directory hosts a gRPC service."""
return ".grpc_service" in files
def clean():
"""Clean out compiled protos."""
# Start running from one directory above the directory which is found by
# this scripts's location as __file__.
logging.info("Cleaning out compiled protos.")
cwd = os.path.dirname(os.path.abspath(__file__))
# Find all the .proto files.
for (root, dirs, files) in os.walk(cwd):
if is_grpc_service_dir(files):
logging.info('Skipping grpc service directory: %s', root)
dirs[:] = []
continue
for filename in files:
full_filename = os.path.join(root, filename)
if full_filename.endswith("_pb2.py") or full_filename.endswith(
"_pb2.pyc"):
os.unlink(full_filename)
def make_proto_service(root):
"""Generate a proto service from the definition file."""
script_basename = "mkproto.sh"
script_path = os.path.join(root, script_basename)
subprocess.check_call(
[
"/bin/sh",
script_path
])
def make_proto():
"""Make sure our protos have been compiled to python libraries."""
# Start running from one directory above the directory which is found by
# this scripts's location as __file__.
cwd = os.path.dirname(os.path.abspath(__file__))
# Find all the .proto files.
protos_to_compile = []
for (root, _, files) in os.walk(cwd):
for filename in files:
full_filename = os.path.join(root, filename)
if full_filename.endswith(".proto"):
proto_stat = os.stat(full_filename)
try:
pb2_stat = os.stat(
full_filename.rsplit(
".", 1)[0] + "_pb2.py")
if pb2_stat.st_mtime >= proto_stat.st_mtime:
continue
except (OSError, IOError):
pass
protos_to_compile.append(full_filename)
if not protos_to_compile:
logging.info("No protos needed to be compiled.")
else:
for proto in protos_to_compile:
logging.info("Compiling %s", proto)
protodir, protofile = os.path.split(proto)
subprocess.check_call(
[
"python",
"-m",
"grpc_tools.protoc",
"-I.",
"--python_out=.",
"--grpc_python_out=.",
protofile,
],
cwd=protodir)
def main():
"""Generate python code from .proto files."""
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("--clean", dest="clean", action="store_true",
help="Clean out compiled protos")
arg_parser.set_defaults(feature=False)
args = arg_parser.parse_args()
if args.clean:
clean()
make_proto()
if __name__ == "__main__":
main()