check-license.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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"^\/\/ Copyright (c) Microsoft Corporation.$"),
  16. (r"^# SPDX-License-Identifier: Apache-2.0$", r"^\/\/ SPDX-License-Identifier: Apache-2.0$"),
  17. (r"^# DeepSpeed Team$", r"^\/\/ DeepSpeed Team$"),
  18. ]
  19. success = True
  20. failures = []
  21. for f in sys.argv[1:]:
  22. for copyright_line in COPYRIGHT:
  23. cmd = ["git", "grep", "--quiet"]
  24. for line in copyright_line:
  25. cmd.extend(["-e", line])
  26. cmd.append(f)
  27. res = subprocess.run(cmd, capture_output=True)
  28. if res.returncode == 1:
  29. success = False
  30. failures.append(f)
  31. break
  32. elif res.returncode == 2:
  33. err(f"Error invoking grep on {', '.join(sys.argv[1:])}:")
  34. err(res.stderr.decode("utf-8"))
  35. sys.exit(2)
  36. if not success:
  37. err(f'{failures}: Missing license at top of file')
  38. err(res.stdout.decode("utf-8"))
  39. sys.exit(1)