freeze_requirements.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from __future__ import annotations
  2. import argparse
  3. import os
  4. from concurrent.futures import Future, ThreadPoolExecutor
  5. from os.path import abspath
  6. from subprocess import CalledProcessError, run
  7. from typing import Sequence
  8. from tools.lib import gitroot
  9. def worker(args: tuple[str, ...]) -> None:
  10. env = os.environ.copy()
  11. env["CUSTOM_COMPILE_COMMAND"] = "make freeze-requirements"
  12. run(args, check=True, capture_output=True, env=env)
  13. def check_futures(futures: list[Future[None]]) -> int:
  14. rc = 0
  15. for future in futures:
  16. try:
  17. future.result()
  18. except CalledProcessError as e:
  19. rc = 1
  20. print(
  21. f"""`{e.cmd}` returned code {e.returncode}
  22. stdout:
  23. {e.stdout.decode()}
  24. stderr:
  25. {e.stderr.decode()}
  26. """
  27. )
  28. return rc
  29. def main(argv: Sequence[str] | None = None) -> int:
  30. parser = argparse.ArgumentParser()
  31. parser.parse_args(argv)
  32. base_path = abspath(gitroot())
  33. base_cmd = (
  34. "pip-compile",
  35. "--allow-unsafe",
  36. "--no-annotate",
  37. "--quiet",
  38. "--strip-extras",
  39. "--index-url=https://pypi.devinfra.sentry.io/simple",
  40. )
  41. executor = ThreadPoolExecutor(max_workers=2)
  42. futures = [
  43. executor.submit(
  44. worker,
  45. (
  46. *base_cmd,
  47. f"{base_path}/requirements-base.txt",
  48. f"{base_path}/requirements-getsentry.txt",
  49. "-o",
  50. f"{base_path}/requirements-frozen.txt",
  51. ),
  52. ),
  53. executor.submit(
  54. worker,
  55. (
  56. *base_cmd,
  57. f"{base_path}/requirements-base.txt",
  58. f"{base_path}/requirements-getsentry.txt",
  59. f"{base_path}/requirements-dev.txt",
  60. "-o",
  61. f"{base_path}/requirements-dev-frozen.txt",
  62. ),
  63. ),
  64. ]
  65. rc = check_futures(futures)
  66. executor.shutdown()
  67. return rc
  68. if __name__ == "__main__":
  69. raise SystemExit(main())