check_custom.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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. LICENSE_TEXT = """# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  17. #
  18. # Licensed under the Apache License, Version 2.0 (the "License");
  19. # you may not use this file except in compliance with the License.
  20. # You may obtain a copy of the License at
  21. #
  22. # http://www.apache.org/licenses/LICENSE-2.0
  23. #
  24. # Unless required by applicable law or agreed to in writing, software
  25. # distributed under the License is distributed on an "AS IS" BASIS,
  26. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. # See the License for the specific language governing permissions and
  28. # limitations under the License.
  29. """
  30. def check(file_path):
  31. with open(file_path, "r") as f:
  32. content = f.read()
  33. # Exclude shebang line
  34. if content.startswith("#!"):
  35. content = content[content.index("\n") + 1 :]
  36. if not content.startswith(LICENSE_TEXT):
  37. print(f"License header missing in {file_path}")
  38. return False
  39. if "import paddle" in content or "from paddle import " in content:
  40. print(f"Please use `lazy_paddle` instead `paddle` when import in {file_path}")
  41. return False
  42. return True
  43. def main():
  44. files = sys.argv[1:]
  45. all_files_valid = True
  46. for file in files:
  47. if not check(file):
  48. all_files_valid = False
  49. if not all_files_valid:
  50. sys.exit(1)
  51. if __name__ == "__main__":
  52. main()