17 from __future__
import print_function
30 DEPS_FILE_CONTENT =
"""
31 # Copyright 2017 gRPC authors.
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
37 # http://www.apache.org/licenses/LICENSE-2.0
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.
45 # AUTO-GENERATED BY make_grpcio_tools.py!
47 PROTO_FILES={proto_files}
49 CC_INCLUDE={cc_include}
50 PROTO_INCLUDE={proto_include}
55 COMMIT_HASH_PREFIX =
'PROTOBUF_SUBMODULE_VERSION="'
56 COMMIT_HASH_SUFFIX =
'"'
59 PROTOBUF_CC_PREFIX =
'//:src/'
60 PROTOBUF_PROTO_PREFIX =
'//:src/'
62 GRPC_ROOT = os.path.abspath(
63 os.path.join(os.path.dirname(os.path.abspath(__file__)),
'..',
'..',
'..'))
65 GRPC_PYTHON_ROOT = os.path.join(GRPC_ROOT,
'tools',
'distrib',
'python',
68 GRPC_PYTHON_PROTOBUF_RELATIVE_ROOT = os.path.join(
'third_party',
'protobuf',
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',
73 GRPC_PROTOC_PLUGINS = os.path.join(GRPC_ROOT,
'src',
'compiler')
74 GRPC_PYTHON_PROTOBUF = os.path.join(GRPC_PYTHON_ROOT,
'third_party',
'protobuf',
76 GRPC_PYTHON_PROTOC_PLUGINS = os.path.join(GRPC_PYTHON_ROOT,
'grpc_root',
'src',
78 GRPC_PYTHON_PROTOC_LIB_DEPS = os.path.join(GRPC_PYTHON_ROOT,
81 GRPC_INCLUDE = os.path.join(GRPC_ROOT,
'include')
82 GRPC_PYTHON_INCLUDE = os.path.join(GRPC_PYTHON_ROOT,
'grpc_root',
'include')
84 BAZEL_DEPS = os.path.join(GRPC_ROOT,
'tools',
'distrib',
'python',
86 BAZEL_DEPS_PROTOC_LIB_QUERY =
'//:protoc_lib'
87 BAZEL_DEPS_COMMON_PROTOS_QUERY =
'//:well_known_protos'
91 """Gets the commit hash for the HEAD of the protobuf submodule currently
94 os.chdir(GRPC_PROTOBUF_SUBMODULE_ROOT)
95 output = subprocess.check_output([
'git',
'rev-parse',
'HEAD'])
97 return output.decode(
"ascii").splitlines()[0].strip()
101 print(
'Running "bazel query %s"' % query)
102 output = subprocess.check_output([BAZEL_DEPS, query])
103 return output.decode(
"ascii").splitlines()
107 """Write the result of the bazel query `query` against protobuf to
109 cc_files_output =
bazel_query(BAZEL_DEPS_PROTOC_LIB_QUERY)
111 name[
len(PROTOBUF_CC_PREFIX):]
112 for name
in cc_files_output
113 if name.endswith(
'.cc')
and name.startswith(PROTOBUF_CC_PREFIX)
115 proto_files_output =
bazel_query(BAZEL_DEPS_COMMON_PROTOS_QUERY)
117 name[
len(PROTOBUF_PROTO_PREFIX):]
118 for name
in proto_files_output
119 if name.endswith(
'.proto')
and name.startswith(PROTOBUF_PROTO_PREFIX)
122 deps_file_content = DEPS_FILE_CONTENT.format(
124 proto_files=proto_files,
125 cc_include=repr(GRPC_PYTHON_PROTOBUF_RELATIVE_ROOT),
126 proto_include=repr(GRPC_PYTHON_PROTOBUF_RELATIVE_ROOT),
127 commit_hash=COMMIT_HASH_PREFIX + commit_hash + COMMIT_HASH_SUFFIX)
128 return deps_file_content
133 return '\\\\?\\' + path
141 for source, target
in [(GRPC_PROTOBUF, GRPC_PYTHON_PROTOBUF),
142 (GRPC_PROTOC_PLUGINS, GRPC_PYTHON_PROTOC_PLUGINS),
143 (GRPC_INCLUDE, GRPC_PYTHON_INCLUDE)]:
144 for source_dir, _, files
in os.walk(source):
145 target_dir = os.path.abspath(
146 os.path.join(target, os.path.relpath(source_dir, source)))
148 os.makedirs(target_dir)
149 except OSError
as error:
150 if error.errno != errno.EEXIST:
152 for relative_file
in files:
153 source_file = os.path.abspath(
154 os.path.join(source_dir, relative_file))
155 target_file = os.path.abspath(
156 os.path.join(target_dir, relative_file))
157 shutil.copyfile(source_file, target_file)
160 print(
'Invoking "bazel query" to gather the protobuf dependencies.')
161 protoc_lib_deps_content =
get_deps()
162 except Exception
as error:
166 sys.stderr.write(
"Got non-fatal error:\n")
167 traceback.print_exc(file=sys.stderr)
170 with open(GRPC_PYTHON_PROTOC_LIB_DEPS,
'w')
as deps_file:
171 deps_file.write(protoc_lib_deps_content)
172 print(
'File "%s" updated.' % GRPC_PYTHON_PROTOC_LIB_DEPS)
176 if __name__ ==
'__main__':