-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathutils.py
More file actions
78 lines (69 loc) · 2.44 KB
/
Copy pathutils.py
File metadata and controls
78 lines (69 loc) · 2.44 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
75
76
77
78
import subprocess
import tempfile
from pathlib import Path
from minisweagent.environments.docker import DockerEnvironment
from codeclash.utils.environment import assert_zero_exit_code
def copy_between_containers(
src_container: DockerEnvironment,
dest_container: DockerEnvironment,
src_path: str | Path,
dest_path: str | Path,
):
"""
Copy files from one Docker container to another via a temporary local directory.
"""
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir) / Path(src_path).name
# Copy from source container to temporary local directory
cmd_src = [
"docker",
"cp",
f"{src_container.container_id}:{src_path}",
str(temp_path),
]
result_src = subprocess.run(
cmd_src, check=False, capture_output=True, text=True
)
if result_src.returncode != 0:
raise RuntimeError(
f"Failed to copy from {src_container.container_id} to local temp: {result_src.stdout}{result_src.stderr}"
)
# Ensure destination folder exists
assert_zero_exit_code(
dest_container.execute(f"mkdir -p {Path(dest_path).parent}")
)
# Copy from temporary local directory to destination container
cmd_dest = [
"docker",
"cp",
str(temp_path),
f"{dest_container.container_id}:{dest_path}",
]
result_dest = subprocess.run(
cmd_dest, check=False, capture_output=True, text=True
)
if result_dest.returncode != 0:
raise RuntimeError(
f"Failed to copy from local temp to {dest_container.container_id}: {result_dest.stdout}{result_dest.stderr}"
)
def copy_file_to_container(
container: DockerEnvironment,
src_path: str | Path,
dest_path: str | Path,
):
"""
Copy a file from the local filesystem to a Docker container.
"""
if not str(dest_path).startswith("/"):
dest_path = f"/{container.config.cwd}/{dest_path}"
cmd = [
"docker",
"cp",
str(src_path),
f"{container.container_id}:{dest_path}",
]
result = subprocess.run(cmd, check=False, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(
f"Failed to copy {src_path} to {container.container_id}:{dest_path}: {result.stdout}{result.stderr}"
)