1 #!/usr/bin/env python 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): 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']: continue 29 if path in [ 30 os.path.join('include', 'grpc', 'support', 31 'port_platform.h'), 32 os.path.join('include', 'grpc', 'impl', 'codegen', 33 'port_platform.h'), 34 ]: 35 continue 36 if filename.endswith('.pb.h') or filename.endswith('.pb.c'): 37 continue 38 with open(path) as f: 39 all_lines_in_file = f.readlines() 40 for index, l in enumerate(all_lines_in_file): 41 if '#include' in l: 42 if l not in [ 43 '#include <grpc/support/port_platform.h>\n', 44 '#include <grpc/impl/codegen/port_platform.h>\n' 45 ]: 46 bad_files.append(path) 47 elif all_lines_in_file[index + 1] != '\n': 48 # Require a blank line after including port_platform.h in 49 # order to prevent the formatter from reording it's 50 # inclusion order upon future changes. 51 bad_files.append(path) 52 break 53 return bad_files 54 55 56 all_bad_files = [] 57 all_bad_files += check_port_platform_inclusion(os.path.join('src', 'core')) 58 all_bad_files += check_port_platform_inclusion(os.path.join('include', 'grpc')) 59 60 if len(all_bad_files) > 0: 61 for f in all_bad_files: 62 print(('port_platform.h is not the first included header or there ' 63 'is not a blank line following its inclusion in %s') % f) 64 sys.exit(1) 65