xref: /aosp_15_r20/external/grpc-grpc/tools/run_tests/sanity/check_deprecated_grpc++.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#!/usr/bin/env python3
2
3# Copyright 2018 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
17import os
18import sys
19
20os.chdir(os.path.join(os.path.dirname(sys.argv[0]), "../../.."))
21
22expected_files = [
23    "include/grpc++/create_channel_posix.h",
24    "include/grpc++/server_builder.h",
25    "include/grpc++/resource_quota.h",
26    "include/grpc++/create_channel.h",
27    "include/grpc++/alarm.h",
28    "include/grpc++/server.h",
29    "include/grpc++/server_context.h",
30    "include/grpc++/client_context.h",
31    "include/grpc++/server_posix.h",
32    "include/grpc++/grpc++.h",
33    "include/grpc++/health_check_service_interface.h",
34    "include/grpc++/completion_queue.h",
35    "include/grpc++/channel.h",
36    "include/grpc++/support/sync_stream.h",
37    "include/grpc++/support/status.h",
38    "include/grpc++/support/config.h",
39    "include/grpc++/support/status_code_enum.h",
40    "include/grpc++/support/byte_buffer.h",
41    "include/grpc++/support/error_details.h",
42    "include/grpc++/support/async_unary_call.h",
43    "include/grpc++/support/channel_arguments.h",
44    "include/grpc++/support/async_stream.h",
45    "include/grpc++/support/slice.h",
46    "include/grpc++/support/stub_options.h",
47    "include/grpc++/support/string_ref.h",
48    "include/grpc++/support/time.h",
49    "include/grpc++/security/auth_metadata_processor.h",
50    "include/grpc++/security/credentials.h",
51    "include/grpc++/security/server_credentials.h",
52    "include/grpc++/security/auth_context.h",
53    "include/grpc++/impl/rpc_method.h",
54    "include/grpc++/impl/server_builder_option.h",
55    "include/grpc++/impl/call.h",
56    "include/grpc++/impl/service_type.h",
57    "include/grpc++/impl/grpc_library.h",
58    "include/grpc++/impl/client_unary_call.h",
59    "include/grpc++/impl/channel_argument_option.h",
60    "include/grpc++/impl/rpc_service_method.h",
61    "include/grpc++/impl/method_handler_impl.h",
62    "include/grpc++/impl/server_builder_plugin.h",
63    "include/grpc++/impl/server_initializer.h",
64    "include/grpc++/impl/serialization_traits.h",
65    "include/grpc++/impl/codegen/sync_stream.h",
66    "include/grpc++/impl/codegen/channel_interface.h",
67    "include/grpc++/impl/codegen/config_protobuf.h",
68    "include/grpc++/impl/codegen/status.h",
69    "include/grpc++/impl/codegen/config.h",
70    "include/grpc++/impl/codegen/status_code_enum.h",
71    "include/grpc++/impl/codegen/metadata_map.h",
72    "include/grpc++/impl/codegen/rpc_method.h",
73    "include/grpc++/impl/codegen/server_context.h",
74    "include/grpc++/impl/codegen/byte_buffer.h",
75    "include/grpc++/impl/codegen/async_unary_call.h",
76    "include/grpc++/impl/codegen/server_interface.h",
77    "include/grpc++/impl/codegen/call.h",
78    "include/grpc++/impl/codegen/client_context.h",
79    "include/grpc++/impl/codegen/service_type.h",
80    "include/grpc++/impl/codegen/async_stream.h",
81    "include/grpc++/impl/codegen/slice.h",
82    "include/grpc++/impl/codegen/client_unary_call.h",
83    "include/grpc++/impl/codegen/proto_utils.h",
84    "include/grpc++/impl/codegen/stub_options.h",
85    "include/grpc++/impl/codegen/rpc_service_method.h",
86    "include/grpc++/impl/codegen/method_handler_impl.h",
87    "include/grpc++/impl/codegen/string_ref.h",
88    "include/grpc++/impl/codegen/completion_queue_tag.h",
89    "include/grpc++/impl/codegen/call_hook.h",
90    "include/grpc++/impl/codegen/completion_queue.h",
91    "include/grpc++/impl/codegen/serialization_traits.h",
92    "include/grpc++/impl/codegen/create_auth_context.h",
93    "include/grpc++/impl/codegen/time.h",
94    "include/grpc++/impl/codegen/security/auth_context.h",
95    "include/grpc++/ext/health_check_service_server_builder_option.h",
96    "include/grpc++/ext/proto_server_reflection_plugin.h",
97    "include/grpc++/generic/async_generic_service.h",
98    "include/grpc++/generic/generic_stub.h",
99    "include/grpc++/test/mock_stream.h",
100    "include/grpc++/test/server_context_test_spouse.h",
101]
102
103file_template = """//
104//
105// Copyright 2018 gRPC authors.
106//
107// Licensed under the Apache License, Version 2.0 (the "License");
108// you may not use this file except in compliance with the License.
109// You may obtain a copy of the License at
110//
111//     http://www.apache.org/licenses/LICENSE-2.0
112//
113// Unless required by applicable law or agreed to in writing, software
114// distributed under the License is distributed on an "AS IS" BASIS,
115// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
116// See the License for the specific language governing permissions and
117// limitations under the License.
118//
119//
120
121// DEPRECATED: The headers in include/grpc++ are deprecated. Please include the
122// headers in include/grpcpp instead. This header exists only for backwards
123// compatibility.
124
125#ifndef GRPCXX_FILE_PATH_NAME_UPPER
126#define GRPCXX_FILE_PATH_NAME_UPPER
127
128#include <grpcpp/FILE_PATH_NAME_LOWER>
129
130#endif  // GRPCXX_FILE_PATH_NAME_UPPER
131"""
132
133errors = 0
134
135path_files = []
136for root, dirs, files in os.walk("include/grpc++"):
137    for filename in files:
138        path_file = os.path.join(root, filename)
139        path_files.append(path_file)
140
141if path_files.sort() != expected_files.sort():
142    diff_plus = [file for file in path_files if file not in expected_files]
143    diff_minus = [file for file in expected_files if file not in path_files]
144    for file in diff_minus:
145        print(("- ", file))
146    for file in diff_plus:
147        print(("+ ", file))
148    errors += 1
149
150if errors > 0:
151    sys.exit(errors)
152
153for path_file in expected_files:
154    relative_path_file = path_file.split("/", 2)[2]
155
156    replace_lower = relative_path_file.replace("+", "p")
157
158    replace_upper = relative_path_file.replace("/", "_")
159    replace_upper = replace_upper.replace(".", "_")
160    replace_upper = replace_upper.upper().replace("+", "X")
161
162    expected_content = file_template.replace(
163        "FILE_PATH_NAME_LOWER", replace_lower
164    )
165    expected_content = expected_content.replace(
166        "FILE_PATH_NAME_UPPER", replace_upper
167    )
168
169    path_file_expected = path_file + ".expected"
170    with open(path_file_expected, "w") as fo:
171        fo.write(expected_content)
172
173    if 0 != os.system("diff %s %s" % (path_file_expected, path_file)):
174        print(("Difference found in file:", path_file))
175        errors += 1
176
177    os.remove(path_file_expected)
178
179check_extensions = [".h", ".cc", ".c", ".m"]
180
181for root, dirs, files in os.walk("src"):
182    for filename in files:
183        path_file = os.path.join(root, filename)
184        for ext in check_extensions:
185            if path_file.endswith(ext):
186                try:
187                    with open(path_file, "r") as fi:
188                        content = fi.read()
189                        if "#include <grpc++/" in content:
190                            print(
191                                "Failed: invalid include of deprecated headers"
192                                " in include/grpc++ in %s" % path_file
193                            )
194                            errors += 1
195                except IOError:
196                    pass
197
198sys.exit(errors)
199