1#!/usr/bin/env python3 2 3# Copyright 2021 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 re 19import sys 20 21os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..')) 22 23BAD_REGEXES = [ 24 (r'\n#include "include/(.*)"', r'\n#include <\1>'), 25 (r'\n#include "grpc(.*)"', r'\n#include <grpc\1>'), 26] 27 28fix = sys.argv[1:] == ['--fix'] 29if fix: 30 print("FIXING!") 31 32 33def check_include_style(directory_root): 34 bad_files = [] 35 for root, dirs, files in os.walk(directory_root): 36 for filename in files: 37 path = os.path.join(root, filename) 38 if os.path.splitext(path)[1] not in ['.c', '.cc', '.h']: 39 continue 40 if filename.endswith('.pb.h') or filename.endswith('.pb.c'): 41 continue 42 # Skip check for upb generated code. 43 if (filename.endswith('.upb.h') or filename.endswith('.upb.c') or 44 filename.endswith('.upbdefs.h') or 45 filename.endswith('.upbdefs.c')): 46 continue 47 with open(path) as f: 48 text = f.read() 49 original = text 50 for regex, replace in BAD_REGEXES: 51 text = re.sub(regex, replace, text) 52 if text != original: 53 bad_files.append(path) 54 if fix: 55 with open(path, 'w') as f: 56 f.write(text) 57 return bad_files 58 59 60all_bad_files = [] 61all_bad_files += check_include_style(os.path.join('src', 'core')) 62all_bad_files += check_include_style(os.path.join('src', 'cpp')) 63all_bad_files += check_include_style(os.path.join('test', 'core')) 64all_bad_files += check_include_style(os.path.join('test', 'cpp')) 65all_bad_files += check_include_style(os.path.join('include', 'grpc')) 66all_bad_files += check_include_style(os.path.join('include', 'grpcpp')) 67 68if all_bad_files: 69 for f in all_bad_files: 70 print("%s has badly formed grpc system header files" % f) 71 sys.exit(1) 72