Debian Codegraph @

tools/distrib/python/make_grpcio_tools.py

raw
1 #!/usr/bin/env python3
2
3 # Copyright 2016 gRPC authors.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 from __future__ import print_function
18
19 import errno
20 import filecmp
21 import glob
22 import os
23 import os.path
24 import shutil
25 import subprocess
26 import sys
27 import traceback
28 import uuid
29
30 DEPS_FILE_CONTENT = """
31 # Copyright 2017 gRPC authors.
32 #
33 # Licensed under the Apache License, Version 2.0 (the "License");
34 # you may not use this file except in compliance with the License.
35 # You may obtain a copy of the License at
36 #
37 # http://www.apache.org/licenses/LICENSE-2.0
38 #
39 # Unless required by applicable law or agreed to in writing, software
40 # distributed under the License is distributed on an "AS IS" BASIS,
41 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42 # See the License for the specific language governing permissions and
43 # limitations under the License.
44
45 # AUTO-GENERATED BY make_grpcio_tools.py!
46 CC_FILES={cc_files}
47 PROTO_FILES={proto_files}
48
49 CC_INCLUDE={cc_include}
50 PROTO_INCLUDE={proto_include}
51
52 {commit_hash}
53 """
54
55 COMMIT_HASH_PREFIX = 'PROTOBUF_SUBMODULE_VERSION="'
56 COMMIT_HASH_SUFFIX = '"'
57
58 # Bazel query result prefix for expected source files in protobuf.
59 PROTOBUF_CC_PREFIX = '//:src/'
60 PROTOBUF_PROTO_PREFIX = '//:src/'
61
62 GRPC_ROOT = os.path.abspath(
63 os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..'))
64
65 GRPC_PYTHON_ROOT = os.path.join(GRPC_ROOT, 'tools', 'distrib', 'python',
66 'grpcio_tools')
67
68 GRPC_PYTHON_PROTOBUF_RELATIVE_ROOT = os.path.join('third_party', 'protobuf',
69 'src')
70 GRPC_PROTOBUF = os.path.join(GRPC_ROOT, GRPC_PYTHON_PROTOBUF_RELATIVE_ROOT)
71 GRPC_PROTOBUF_SUBMODULE_ROOT = os.path.join(GRPC_ROOT, 'third_party',
72 'protobuf')
73 GRPC_PROTOC_PLUGINS = os.path.join(GRPC_ROOT, 'src', 'compiler')
74 GRPC_PYTHON_PROTOBUF = os.path.join(GRPC_PYTHON_ROOT, 'third_party', 'protobuf',
75 'src')
76 GRPC_PYTHON_PROTOC_PLUGINS = os.path.join(GRPC_PYTHON_ROOT, 'grpc_root', 'src',
77 'compiler')
78 GRPC_PYTHON_PROTOC_LIB_DEPS = os.path.join(GRPC_PYTHON_ROOT,
79 'protoc_lib_deps.py')
80
81 GRPC_INCLUDE = os.path.join(GRPC_ROOT, 'include')
82 GRPC_PYTHON_INCLUDE = os.path.join(GRPC_PYTHON_ROOT, 'grpc_root', 'include')
83
84 BAZEL_DEPS = os.path.join(GRPC_ROOT, 'tools', 'distrib', 'python',
85 'bazel_deps.sh')
86 BAZEL_DEPS_PROTOC_LIB_QUERY = '//:protoc_lib'
87 BAZEL_DEPS_COMMON_PROTOS_QUERIES = [
88 '//:well_known_type_protos',
89 '//:built_in_runtime_protos',
90 ]
91
92
93 def protobuf_submodule_commit_hash():
94 """Gets the commit hash for the HEAD of the protobuf submodule currently
95 checked out."""
96 cwd = os.getcwd()
97 os.chdir(GRPC_PROTOBUF_SUBMODULE_ROOT)
98 output = subprocess.check_output(['git', 'rev-parse', 'HEAD'])
99 os.chdir(cwd)
100 return output.decode("ascii").splitlines()[0].strip()
101
102
103 def bazel_query(query):
104 print('Running "bazel query %s"' % query)
105 output = subprocess.check_output([BAZEL_DEPS, query])
106 return output.decode("ascii").splitlines()
107
108
109 def get_deps():
110 """Write the result of the bazel query `query` against protobuf to
111 `out_file`."""
112 cc_files_output = bazel_query(BAZEL_DEPS_PROTOC_LIB_QUERY)
113 cc_files = [
114 name[len(PROTOBUF_CC_PREFIX):]
115 for name in cc_files_output
116 if name.endswith('.cc') and name.startswith(PROTOBUF_CC_PREFIX)
117 ]
118 raw_proto_files = []
119 for target in BAZEL_DEPS_COMMON_PROTOS_QUERIES:
120 raw_proto_files += bazel_query(target)
121 proto_files = [
122 name[len(PROTOBUF_PROTO_PREFIX):]
123 for name in raw_proto_files
124 if name.endswith('.proto') and name.startswith(PROTOBUF_PROTO_PREFIX)
125 ]
126 commit_hash = protobuf_submodule_commit_hash()
127 deps_file_content = DEPS_FILE_CONTENT.format(
128 cc_files=cc_files,
129 proto_files=proto_files,
130 cc_include=repr(GRPC_PYTHON_PROTOBUF_RELATIVE_ROOT),
131 proto_include=repr(GRPC_PYTHON_PROTOBUF_RELATIVE_ROOT),
132 commit_hash=COMMIT_HASH_PREFIX + commit_hash + COMMIT_HASH_SUFFIX)
133 return deps_file_content
134
135
136 def long_path(path):
137 if os.name == 'nt':
138 return '\\\\?\\' + path
139 else:
140 return path
141
142
143 def main():
144 os.chdir(GRPC_ROOT)
145
146 for source, target in [(GRPC_PROTOBUF, GRPC_PYTHON_PROTOBUF),
147 (GRPC_PROTOC_PLUGINS, GRPC_PYTHON_PROTOC_PLUGINS),
148 (GRPC_INCLUDE, GRPC_PYTHON_INCLUDE)]:
149 for source_dir, _, files in os.walk(source):
150 target_dir = os.path.abspath(
151 os.path.join(target, os.path.relpath(source_dir, source)))
152 try:
153 os.makedirs(target_dir)
154 except OSError as error:
155 if error.errno != errno.EEXIST:
156 raise
157 for relative_file in files:
158 source_file = os.path.abspath(
159 os.path.join(source_dir, relative_file))
160 target_file = os.path.abspath(
161 os.path.join(target_dir, relative_file))
162 shutil.copyfile(source_file, target_file)
163
164 try:
165 print('Invoking "bazel query" to gather the protobuf dependencies.')
166 protoc_lib_deps_content = get_deps()
167 except Exception as error:
168 # We allow this script to succeed even if we couldn't get the dependencies,
169 # as then we can assume that even without a successful bazel run the
170 # dependencies currently in source control are 'good enough'.
171 sys.stderr.write("Got non-fatal error:\n")
172 traceback.print_exc(file=sys.stderr)
173 return
174 # If we successfully got the dependencies, truncate and rewrite the deps file.
175 with open(GRPC_PYTHON_PROTOC_LIB_DEPS, 'w') as deps_file:
176 deps_file.write(protoc_lib_deps_content)
177 print('File "%s" updated.' % GRPC_PYTHON_PROTOC_LIB_DEPS)
178 print('Done.')
179
180
181 if __name__ == '__main__':
182 main()