check_port_platform.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright 2017 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 import os
18 import sys
19 
20 os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..'))
21 
22 
23 def check_port_platform_inclusion(directory_root, legal_list):
24  bad_files = []
25  for root, dirs, files in os.walk(directory_root):
26  for filename in files:
27  path = os.path.join(root, filename)
28  if os.path.splitext(path)[1] not in ['.c', '.cc', '.h']:
29  continue
30  if path in [
31  os.path.join('include', 'grpc', 'support',
32  'port_platform.h'),
33  os.path.join('include', 'grpc', 'impl', 'codegen',
34  'port_platform.h'),
35  ]:
36  continue
37  if filename.endswith('.pb.h') or filename.endswith('.pb.c'):
38  continue
39  # Skip check for upb generated code.
40  if (filename.endswith('.upb.h') or filename.endswith('.upb.c') or
41  filename.endswith('.upbdefs.h') or
42  filename.endswith('.upbdefs.c')):
43  continue
44  with open(path) as f:
45  all_lines_in_file = f.readlines()
46  for index, l in enumerate(all_lines_in_file):
47  if '#include' in l:
48  if l not in legal_list:
49  bad_files.append(path)
50  elif all_lines_in_file[index + 1] != '\n':
51  # Require a blank line after including port_platform.h in
52  # order to prevent the formatter from reording it's
53  # inclusion order upon future changes.
54  bad_files.append(path)
55  break
56  return bad_files
57 
58 
59 all_bad_files = []
60 all_bad_files += check_port_platform_inclusion(os.path.join('src', 'core'), [
61  '#include <grpc/support/port_platform.h>\n',
62 ])
63 all_bad_files += check_port_platform_inclusion(os.path.join(
64  'include', 'grpc'), [
65  '#include <grpc/support/port_platform.h>\n',
66  '#include <grpc/impl/codegen/port_platform.h>\n',
67  ])
68 
69 if sys.argv[1:] == ['--fix']:
70  for path in all_bad_files:
71  text = ''
72  found = False
73  with open(path) as f:
74  for l in f.readlines():
75  if not found and '#include' in l:
76  text += '#include <grpc/support/port_platform.h>\n\n'
77  found = True
78  text += l
79  with open(path, 'w') as f:
80  f.write(text)
81 else:
82  if len(all_bad_files) > 0:
83  for f in all_bad_files:
84  print((('port_platform.h is not the first included header or there '
85  'is not a blank line following its inclusion in %s') % f))
86  sys.exit(1)
check_port_platform.check_port_platform_inclusion
def check_port_platform_inclusion(directory_root, legal_list)
Definition: check_port_platform.py:23
open
#define open
Definition: test-fs.c:46
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:45