check-extraindexurl.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. Checks each file in sys.argv for the string "--extra-index-url".
  9. Modified from https://github.com/jlebar/pre-commit-hooks/blob/master/check_do_not_submit.py
  10. """
  11. import subprocess
  12. import sys
  13. def err(s: str) -> None:
  14. print(s, file=sys.stderr)
  15. print(*sys.argv[1:])
  16. # There are many ways we could search for the string "--extra-index-url", but `git
  17. # grep --no-index` is nice because
  18. # - it's very fast (as compared to iterating over the file in Python)
  19. # - we can reasonably assume it's available on all machines
  20. # - unlike plain grep, which is slower and has different flags on MacOS versus
  21. # Linux, git grep is always the same.
  22. res = subprocess.run(
  23. ["git", "grep", "-Hn", "--no-index", "-e", r"--extra-index-url", *sys.argv[1:]],
  24. capture_output=True,
  25. )
  26. if res.returncode == 0:
  27. err('Error: The string "--extra-index-url" was found.\nPlease replace all calls to --extra-index-url with "--index-url"'
  28. )
  29. err(res.stdout.decode("utf-8"))
  30. sys.exit(1)
  31. elif res.returncode == 2:
  32. err(f"Error invoking grep on {', '.join(sys.argv[1:])}:")
  33. err(res.stderr.decode("utf-8"))
  34. sys.exit(2)