-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreeze.py
executable file
·74 lines (55 loc) · 2.64 KB
/
freeze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
"""Freeze container requirements for use with a final container build."""
from __future__ import annotations
import argparse
import pathlib
import subprocess
import sys
def run(command: str | list[str], capture_output: bool = False, container_id: None | str = None) -> subprocess.CompletedProcess[str]:
try:
return subprocess.run(command, check=True, text=True, capture_output=capture_output)
except subprocess.CalledProcessError as err:
if container_id:
if capture_output:
print(f"ERROR: {err.stderr}, {err.stdout}")
print(f"Stopping container {container_id}")
subprocess.run(["docker", "stop", container_id])
sys.exit(err.returncode)
def main() -> None:
"""Main entry point."""
parser = argparse.ArgumentParser()
parser.add_argument("--container-runtime", default="docker")
parser.add_argument("container", nargs="?", default="koku-test-container-freezer")
parser.add_argument("--no-cache", action="store_true")
args = parser.parse_args()
container_runtime = args.container_runtime
container = args.container
no_cache = args.no_cache
build_command = [container_runtime, "build", "--tag", container, "--file", "Freezer", "."]
if no_cache:
build_command.insert(2, "--no-cache")
print("Building a container to freeze the requirements.")
run(build_command)
container_id = run([container_runtime, "run", "--rm", "--tty", "--detach", container], capture_output=True).stdout.rstrip()
print("Freezing requirements")
freezer_venv = "/opt/venvs/freezer"
command = [container_runtime, "exec", container_id, "python", "-m", "venv", freezer_venv]
run(command, container_id=container_id)
command = [
container_runtime, "exec", container_id,
f"{freezer_venv}/bin/python", "-m", "pip", "install", "--upgrade", "--disable-pip-version-check",
"--requirement", "/usr/share/container-setup/requirements/requirements.in",
"--constraint", "/usr/share/container-setup/requirements/constraints.txt",
] # fmt: off
run(command, container_id=container_id)
command = [
container_runtime, "exec", container_id,
f"{freezer_venv}/bin/python", "-m", "pip", "freeze", "-qqq", "--disable-pip-version-check",
] # fmt: off
freeze = run(command, capture_output=True, container_id=container_id).stdout
freeze_file = pathlib.Path("requirements/requirements.txt")
freeze_file.write_text(freeze)
run([container_runtime, "stop", container_id])
print(f"Freezing complete. Requirements in {freeze_file} updated.")
if __name__ == "__main__":
main()