check_custom.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 content.startswith("\n"):
  41. content = content[1:]
  42. if not re.match(LICENSE_TEXT, content):
  43. print(f"License header missing in {file_path}")
  44. return False
  45. if "paddlex" in file_path.split(os.sep):
  46. if "import paddle" in content or "from paddle import " in content:
  47. print(
  48. f"Please use `lazy_paddle` instead `paddle` when import in {file_path}"
  49. )
  50. return False
  51. return True
  52. def main():
  53. files = sys.argv[1:]
  54. all_files_valid = True
  55. for file in files:
  56. if not check(file):
  57. all_files_valid = False
  58. if not all_files_valid:
  59. sys.exit(1)
  60. if __name__ == "__main__":
  61. main()