freeze_requirements.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from __future__ import annotations
  2. from concurrent.futures import ThreadPoolExecutor
  3. from os.path import abspath
  4. from subprocess import CalledProcessError, run
  5. from tools.lib import gitroot
  6. def worker(args: tuple[str, ...]) -> None:
  7. # pip-compile doesn't let you customize the header, so we write
  8. # one ourselves. However, pip-compile needs -o DEST otherwise
  9. # it will bump >= pins even if they're satisfied. So, we need to
  10. # unfortunately rewrite the whole file.
  11. dest = args[-1]
  12. try:
  13. run(args, check=True, capture_output=True)
  14. except CalledProcessError as e:
  15. raise e
  16. with open(dest, "rb+") as f:
  17. content = f.read()
  18. f.seek(0, 0)
  19. f.write(
  20. b"""# DO NOT MODIFY. This file was generated with `make freeze-requirements`.
  21. """
  22. + content
  23. )
  24. def main() -> int:
  25. base_path = abspath(gitroot())
  26. base_cmd = (
  27. "pip-compile",
  28. "--no-header",
  29. "--no-annotate",
  30. "--allow-unsafe",
  31. "-q",
  32. )
  33. with ThreadPoolExecutor(max_workers=2) as executor:
  34. futures = (
  35. # executor.submit(
  36. # worker,
  37. # (
  38. # *base_cmd,
  39. # f"{base_path}/requirements-base.txt",
  40. # "-o",
  41. # f"{base_path}/requirements-frozen.txt",
  42. # ),
  43. # ),
  44. executor.submit(
  45. worker,
  46. (
  47. *base_cmd,
  48. f"{base_path}/requirements-dev.txt",
  49. "-o",
  50. f"{base_path}/requirements-dev-only-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-dev.txt",
  59. # "-o",
  60. # f"{base_path}/requirements-dev-frozen.txt",
  61. # ),
  62. # ),
  63. )
  64. rc = 0
  65. for future in futures:
  66. try:
  67. future.result()
  68. except CalledProcessError as e:
  69. rc = 1
  70. print(
  71. f"""`{e.cmd}` returned code {e.returncode}
  72. stdout:
  73. {e.stdout.decode()}
  74. stderr:
  75. {e.stderr.decode()}
  76. """
  77. )
  78. return rc
  79. if __name__ == "__main__":
  80. raise SystemExit(main())