check_port_platform.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python3
  2. # Copyright 2017 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import sys
  17. os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..'))
  18. def check_port_platform_inclusion(directory_root, legal_list):
  19. bad_files = []
  20. for root, dirs, files in os.walk(directory_root):
  21. for filename in files:
  22. path = os.path.join(root, filename)
  23. if os.path.splitext(path)[1] not in ['.c', '.cc', '.h']:
  24. continue
  25. if path in [
  26. os.path.join('include', 'grpc', 'support',
  27. 'port_platform.h'),
  28. os.path.join('include', 'grpc', 'impl', 'codegen',
  29. 'port_platform.h'),
  30. ]:
  31. continue
  32. if filename.endswith('.pb.h') or filename.endswith('.pb.c'):
  33. continue
  34. # Skip check for upb generated code.
  35. if (filename.endswith('.upb.h') or filename.endswith('.upb.c') or
  36. filename.endswith('.upbdefs.h') or
  37. filename.endswith('.upbdefs.c')):
  38. continue
  39. with open(path) as f:
  40. all_lines_in_file = f.readlines()
  41. for index, l in enumerate(all_lines_in_file):
  42. if '#include' in l:
  43. if l not in legal_list:
  44. bad_files.append(path)
  45. elif all_lines_in_file[index + 1] != '\n':
  46. # Require a blank line after including port_platform.h in
  47. # order to prevent the formatter from reording it's
  48. # inclusion order upon future changes.
  49. bad_files.append(path)
  50. break
  51. return bad_files
  52. all_bad_files = []
  53. all_bad_files += check_port_platform_inclusion(os.path.join('src', 'core'), [
  54. '#include <grpc/support/port_platform.h>\n',
  55. ])
  56. all_bad_files += check_port_platform_inclusion(os.path.join(
  57. 'include', 'grpc'), [
  58. '#include <grpc/support/port_platform.h>\n',
  59. '#include <grpc/impl/codegen/port_platform.h>\n',
  60. ])
  61. if sys.argv[1:] == ['--fix']:
  62. for path in all_bad_files:
  63. text = ''
  64. found = False
  65. with open(path) as f:
  66. for l in f.readlines():
  67. if not found and '#include' in l:
  68. text += '#include <grpc/support/port_platform.h>\n\n'
  69. found = True
  70. text += l
  71. with open(path, 'w') as f:
  72. f.write(text)
  73. else:
  74. if len(all_bad_files) > 0:
  75. for f in all_bad_files:
  76. print((('port_platform.h is not the first included header or there '
  77. 'is not a blank line following its inclusion in %s') % f))
  78. sys.exit(1)