check_custom.py 2.1 KB

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