check_custom.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 os
  15. import re
  16. import sys
  17. YEAR_PATTERN = r"(?:20\d\d)"
  18. LICENSE_TEXT = 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. def check(file_path):
  35. with open(file_path, "r") as f:
  36. content = f.read()
  37. # Exclude shebang line
  38. if content.startswith("#!"):
  39. content = content[content.index("\n") + 1 :]
  40. if not re.match(LICENSE_TEXT, content):
  41. print(f"License header missing in {file_path}")
  42. return False
  43. if "paddlex" in file_path.split(os.sep):
  44. if "import paddle" in content or "from paddle import " in content:
  45. print(
  46. f"Please use `lazy_paddle` instead `paddle` when import in {file_path}"
  47. )
  48. return False
  49. return True
  50. def main():
  51. files = sys.argv[1:]
  52. all_files_valid = True
  53. for file in files:
  54. if not check(file):
  55. all_files_valid = False
  56. if not all_files_valid:
  57. sys.exit(1)
  58. if __name__ == "__main__":
  59. main()