check_license_headers.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import re
  15. import sys
  16. YEAR_PATTERN = r"(?:20\d\d)"
  17. LICENSE_HEADER_PATTERN = re.compile(
  18. re.escape(
  19. """# Copyright (c) <YEAR_PATTERN> PaddlePaddle Authors. All Rights Reserved.
  20. #
  21. # Licensed under the Apache License, Version 2.0 (the "License");
  22. # you may not use this file except in compliance with the License.
  23. # You may obtain a copy of the License at
  24. #
  25. # http://www.apache.org/licenses/LICENSE-2.0
  26. #
  27. # Unless required by applicable law or agreed to in writing, software
  28. # distributed under the License is distributed on an "AS IS" BASIS,
  29. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  30. # See the License for the specific language governing permissions and
  31. # limitations under the License.
  32. """
  33. ).replace("<YEAR_PATTERN>", YEAR_PATTERN)
  34. )
  35. def check(file_path):
  36. with open(file_path, "r", encoding="utf-8") as f:
  37. contents = f.read()
  38. # Exclude shebang line
  39. if contents.startswith("#!"):
  40. contents = contents[contents.index("\n") + 1 :]
  41. if contents.startswith("\n"):
  42. contents = contents[1:]
  43. if not LICENSE_HEADER_PATTERN.match(contents):
  44. print(f"License header missing in `{file_path}`")
  45. return False
  46. return True
  47. def main():
  48. files = sys.argv[1:]
  49. all_files_valid = True
  50. for file in files:
  51. if not check(file):
  52. all_files_valid = False
  53. if not all_files_valid:
  54. sys.exit(1)
  55. if __name__ == "__main__":
  56. main()