• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2
3# Copyright 2024 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
18# Explicitly ban select functions from being used in gRPC.
19# Any new instance of a deprecated function being used in the code will be
20# flagged by the script. If there is a new instance of a deprecated function in
21# a Pull Request, then the Sanity tests will fail for the Pull Request.
22# The allow list has a list of files where clean up of deprecated functions is
23# pending.
24
25import os
26import sys
27
28os.chdir(os.path.join(os.path.dirname(sys.argv[0]), "../../.."))
29
30# More files may be added to the RUBY_PHP_ALLOW_LIST
31# if they belong to the PHP or RUBY folder.
32RUBY_PHP_ALLOW_LIST = [
33    "./include/grpc/support/log.h",
34    "./src/core/util/log.cc",
35    "./src/php/ext/grpc/call_credentials.c",
36    "./src/php/ext/grpc/channel.c",
37    "./src/ruby/ext/grpc/rb_call.c",
38    "./src/ruby/ext/grpc/rb_call_credentials.c",
39    "./src/ruby/ext/grpc/rb_channel.c",
40    "./src/ruby/ext/grpc/rb_event_thread.c",
41    "./src/ruby/ext/grpc/rb_grpc.c",
42    "./src/ruby/ext/grpc/rb_server.c",
43]
44
45#  Map of deprecated functions to allowlist files
46DEPRECATED_FUNCTION_TEMP_ALLOW_LIST = {
47    # These experimental logging functions are only for php and ruby.
48    "grpc_absl_log(": RUBY_PHP_ALLOW_LIST,
49    "grpc_absl_log_int(": RUBY_PHP_ALLOW_LIST,
50    "grpc_absl_log_str(": RUBY_PHP_ALLOW_LIST,
51    # These have been deprecated.
52    # Most of these have been deleted.
53    # Putting this check here just to prevent people from
54    # submitting PRs with any of these commented out.
55    "gpr_assertion_failed": [],  # Safe to delete this entry after Nov 2024.
56    "gpr_log(": [],  # Safe to delete this entry after Nov 2024.
57    "gpr_log_func_args": [],  # Safe to delete this entry after Nov 2024.
58    "gpr_log_message": [],  # Safe to delete this entry after Nov 2024.
59    "gpr_log_severity_string": [],  # Safe to delete this entry after Nov 2024.
60    "gpr_set_log_function": [],  # Safe to delete this entry after Nov 2024.
61    "gpr_set_log_verbosity": [],  # Safe to delete this entry after Nov 2024.
62    "gpr_should_log": [],  # Safe to delete this entry after Nov 2024.
63    "GPR_ASSERT": [],  # Safe to delete this entry after Nov 2024.
64    "GPR_DEBUG_ASSERT": [],  # Safe to delete this entry after Nov 2024.
65}
66
67errors = 0
68num_files = 0
69for root, dirs, files in os.walk("."):
70    for filename in files:
71        num_files += 1
72        path = os.path.join(root, filename)
73        if os.path.splitext(path)[1] not in (".h", ".cc", ".c"):
74            continue
75        with open(path) as f:
76            text = f.read()
77        for deprecated, allowlist in list(
78            DEPRECATED_FUNCTION_TEMP_ALLOW_LIST.items()
79        ):
80            if path in allowlist:
81                continue
82            if deprecated in text:
83                print(
84                    (
85                        'Illegal use of "%s" in %s. Use absl functions instead.'
86                        % (deprecated, path)
87                    )
88                )
89                errors += 1
90
91assert errors == 0
92if errors > 0:
93    print(("Number of errors : %d " % (errors)))
94
95# This check comes about from this issue:
96# https://github.com/grpc/grpc/issues/15381
97# Basically, a change rendered this script useless and we did not realize it.
98# This check ensures that this type of issue doesn't occur again.
99assert num_files > 18000
100print("Number of files checked : %d " % (num_files))
101