check-license.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. # Copyright (c) Microsoft Corporation.
  3. # SPDX-License-Identifier: Apache-2.0
  4. # DeepSpeed Team
  5. from __future__ import annotations
  6. '''Copyright The Microsoft DeepSpeed Team'''
  7. """
  8. Modified from https://github.com/jlebar/pre-commit-hooks/blob/master/check_do_not_submit.py
  9. """
  10. import subprocess
  11. import sys
  12. def err(s: str) -> None:
  13. print(s, file=sys.stderr)
  14. COPYRIGHT = [
  15. r"^\(\/\/\|#\) Copyright (c) Microsoft Corporation.$", r"^\(\/\/\|#\) SPDX-License-Identifier: Apache-2.0$",
  16. r"^\(\/\/\|#\) DeepSpeed Team$"
  17. ]
  18. success = True
  19. failures = []
  20. for f in sys.argv[1:]:
  21. for copyright_line in COPYRIGHT:
  22. if not success:
  23. break
  24. res = subprocess.run(["git", "grep", "--quiet", "-e", copyright_line, f], capture_output=True)
  25. if res.returncode == 1:
  26. success = False
  27. failures.append(f)
  28. elif res.returncode == 2:
  29. err(f"Error invoking grep on {', '.join(sys.argv[1:])}:")
  30. err(res.stderr.decode("utf-8"))
  31. sys.exit(2)
  32. if not success:
  33. err(f'{failures}: Missing license at top of file')
  34. err(res.stdout.decode("utf-8"))
  35. sys.exit(1)