-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_tool.py
More file actions
74 lines (53 loc) · 1.7 KB
/
Copy pathdocker_tool.py
File metadata and controls
74 lines (53 loc) · 1.7 KB
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
import docker
from pathlib import Path
client = docker.from_env()
PROJECT_DIR = Path(__file__).parent.resolve()
IMAGE_NAME = "bugfix-sandbox"
def read_file(path: str) -> str:
file_path = PROJECT_DIR / path
if not file_path.exists():
return f"File not found: {path}"
return file_path.read_text(encoding="utf-8")
def write_file(path: str, content: str) -> str:
file_path = PROJECT_DIR / path
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(content, encoding="utf-8")
return f"Successfully wrote {path}"
def run_in_sandbox(command: str) -> str:
print("\n[Docker] Starting container...")
print(f"[Docker] Command: {command}\n")
container = client.containers.run(
image=IMAGE_NAME,
command=["sh", "-c", command],
volumes={
str(PROJECT_DIR): {
"bind": "/workspace",
"mode": "rw",
}
},
working_dir="/workspace",
detach=True,
)
output = []
try:
for line in container.logs(
stream=True,
stdout=True,
stderr=True,
):
text = line.decode("utf-8", errors="replace")
print(f"[Docker] {text}", end="")
output.append(text)
result = container.wait()
exit_code = result["StatusCode"]
print(
f"\n[Docker] Container finished "
f"with exit code {exit_code}"
)
return "".join(output) + f"\n[exit_code={exit_code}]"
finally:
container.remove(force=True)
print("[Docker] Container removed.")
def run_tests() -> str:
result = run_in_sandbox("python -m pytest task")
return result