Skip to content

Commit 7ebdd2b

Browse files
committed
Merge branch 'copyFix' into pvenv_builder
2 parents 5c43564 + fe6a246 commit 7ebdd2b

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/pythainer/builders/__init__.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
tailored specifically for Docker environments.
88
"""
99
import os
10+
import shutil
1011
import tempfile
1112
from pathlib import Path
1213
from typing import Dict, List
1314

1415
from pythainer.builders.cmds import (
1516
AddPkgDockerBuildCommand,
17+
CopyDockerBuildCommand,
1618
DockerBuildCommand,
1719
StrDockerBuildCommand,
1820
)
@@ -220,16 +222,15 @@ def add_packages(self, packages: List[str]) -> None:
220222
"""
221223
self._build_commands.append(AddPkgDockerBuildCommand(packages=packages))
222224

223-
def copy(self, filename: PathType, destination: PathType) -> None:
225+
def copy(self, source_path: Path, destination_path: Path) -> None:
224226
"""
225227
Copies a file to the docker container
226228
227229
Parameters:
228-
filename (PathType): The file to copy to the container.
229-
destination (PathType): The location to place the file within the Docker container.
230+
source_path (Path): The file or folder to copy to the container.
231+
destination_path (Path): The location to place the file or folder within the Docker container.
230232
"""
231-
cmd = f"COPY {filename} {destination}"
232-
self._build_commands.append(StrDockerBuildCommand(cmd))
233+
self._build_commands.append(CopyDockerBuildCommand(source_path,destination_path))
233234

234235

235236
class DockerBuilder(PartialDockerBuilder):
@@ -389,19 +390,25 @@ def build(self, dockerfile_savepath: PathType = "", docker_context: PathType = "
389390
Parameters:
390391
dockerfile_savepath (PathType): Optional path to save the Dockerfile used for the build.
391392
"""
393+
392394
main_dir = Path("/tmp/pythainer/docker/")
393395
mkdir(main_dir)
394396
with tempfile.TemporaryDirectory(
395397
prefix="/tmp/pythainer/docker/docker-build-",
396398
dir=main_dir,
397399
) as temp_dir:
400+
398401
temp_path = Path(temp_dir)
399402
dockerfile_path = (temp_path / "Dockerfile").resolve()
400403
dockerfile_paths = [dockerfile_path] + (
401404
[dockerfile_savepath] if dockerfile_savepath else []
402405
)
403406
self.generate_dockerfile(dockerfile_paths=dockerfile_paths)
404407

408+
data_path = main_dir / "data"
409+
410+
shutil.move(data_path, temp_path)
411+
405412
command = self.get_build_commands(
406413
dockerfile_path=dockerfile_path,
407414
docker_build_dir=Path(docker_context).resolve() if docker_context else temp_path,
@@ -418,6 +425,7 @@ def build(self, dockerfile_savepath: PathType = "", docker_context: PathType = "
418425
output_is_log=True,
419426
)
420427

428+
421429
def get_runner(
422430
self,
423431
workdir: PathType | None = None,

src/pythainer/builders/cmds.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
and package managers.
1010
"""
1111

12+
import os
13+
from pathlib import Path
14+
import shutil
1215
from typing import List
1316

1417

@@ -68,6 +71,52 @@ def get_str_for_dockerfile(
6871
"""
6972
return str(self._str)
7073

74+
class CopyDockerBuildCommand(DockerBuildCommand):
75+
"""
76+
Represents a simple string command in a Dockerfile, such as a comment or other directive that
77+
does not involve complex logic or conditional behavior.
78+
"""
79+
80+
def __init__(self, source_path:Path ,destination_path:Path) -> None:
81+
"""
82+
Initializes the StrDockerBuildCommand with a string.
83+
84+
Parameters:
85+
s (str): The string that represents this Dockerfile command.
86+
"""
87+
super().__init__()
88+
self._source_path = source_path
89+
self._destination_path = destination_path
90+
91+
# pylint: disable=arguments-differ
92+
def get_str_for_dockerfile(
93+
self,
94+
docker_file_Path: Path,
95+
*args,
96+
**kwargs,
97+
) -> str:
98+
"""
99+
Returns the string that was initialized at the creation of the object.
100+
101+
Returns:
102+
str: The command string.
103+
"""
104+
105+
data_path = Path("/tmp/pythainer/docker/data")
106+
107+
if os.path.isfile(self._source_path):
108+
shutil.copyfile(self._source_path, data_path / self._source_path)
109+
elif os.path.isdir(self._source_path):
110+
shutil.copytree(self._source_path, data_path / self._source_path,dirs_exist_ok=True)
111+
else:
112+
raise FileExistsError(f'{self._source_path} is not a valid target to copy into the docker container')
113+
114+
115+
116+
cmd = f"COPY --chown=${{USER_NAME}} {self._source_path} {self._destination_path}"
117+
118+
return cmd
119+
71120

72121
class AddPkgDockerBuildCommand(DockerBuildCommand):
73122
"""

0 commit comments

Comments
 (0)