check_frozen_requirements.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from __future__ import annotations
  2. import argparse
  3. import sys
  4. from difflib import unified_diff
  5. from tempfile import TemporaryDirectory
  6. from typing import Sequence
  7. from tools import freeze_requirements
  8. def main(argv: Sequence[str] | None = None) -> int:
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument("repo", type=str, help="Repository name.")
  11. args = parser.parse_args(argv)
  12. repo = args.repo
  13. with TemporaryDirectory() as tmpdir:
  14. rc = freeze_requirements.main((repo, tmpdir))
  15. if rc != 0:
  16. print("There was an issue generating requirement lockfiles.") # noqa
  17. return rc
  18. rc = 0
  19. lockfiles = [
  20. "requirements-frozen.txt",
  21. "requirements-dev-frozen.txt",
  22. ]
  23. if repo == "sentry":
  24. # requirements-dev-only-frozen.txt is only used in sentry
  25. # (and reused in getsentry) as a fast path for some CI jobs.
  26. lockfiles.append("requirements-dev-only-frozen.txt")
  27. for lockfile in lockfiles:
  28. with open(lockfile) as f:
  29. current = f.readlines()
  30. with open(f"{tmpdir}/{lockfile}") as f:
  31. new = f.readlines()
  32. diff = tuple(
  33. unified_diff(
  34. current,
  35. new,
  36. fromfile=f"current {lockfile}",
  37. tofile=f"new {lockfile}",
  38. )
  39. )
  40. if not diff:
  41. continue
  42. rc = 1
  43. sys.stdout.writelines(diff)
  44. if rc != 0:
  45. if repo == "getsentry":
  46. print( # noqa
  47. """
  48. Requirement lockfiles are mismatched. To regenerate them,
  49. run `bin/bump-sentry (your desired sentry sha)`.
  50. """
  51. )
  52. return rc
  53. print( # noqa
  54. """
  55. Requirement lockfiles are mismatched. To regenerate them,
  56. run `make freeze-requirements`.
  57. """
  58. )
  59. return rc
  60. return 0
  61. if __name__ == "__main__":
  62. raise SystemExit(main())