|
9 | 9 | and package managers. |
10 | 10 | """ |
11 | 11 |
|
12 | | -import os |
13 | 12 | from pathlib import Path |
14 | 13 | import shutil |
15 | 14 | from typing import List |
16 | 15 |
|
| 16 | +from pythainer.sysutils import mkdir |
| 17 | + |
17 | 18 |
|
18 | 19 | class DockerBuildCommand: |
19 | 20 | """ |
@@ -73,47 +74,52 @@ def get_str_for_dockerfile( |
73 | 74 |
|
74 | 75 | class CopyDockerBuildCommand(DockerBuildCommand): |
75 | 76 | """ |
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. |
| 77 | + Represents the command string to copy data from the host system to the docker container at build time. |
78 | 78 | """ |
79 | 79 |
|
80 | 80 | def __init__(self, source_path:Path ,destination_path:Path) -> None: |
81 | 81 | """ |
82 | | - Initializes the StrDockerBuildCommand with a string. |
| 82 | + Initializes the CopyDockerBuildCommand with a a source and destination path. |
83 | 83 |
|
84 | 84 | Parameters: |
85 | | - s (str): The string that represents this Dockerfile command. |
| 85 | + source_path (Path): Path of folder or file to copy to container |
| 86 | + destination_path (Path): Path to copy the file or folder to |
86 | 87 | """ |
87 | 88 | super().__init__() |
88 | | - self._source_path = source_path |
| 89 | + self._source_path = source_path.resolve() |
89 | 90 | self._destination_path = destination_path |
90 | 91 |
|
91 | 92 | # pylint: disable=arguments-differ |
92 | 93 | def get_str_for_dockerfile( |
93 | 94 | self, |
94 | | - docker_file_Path: Path, |
95 | 95 | *args, |
96 | 96 | **kwargs, |
97 | 97 | ) -> str: |
98 | 98 | """ |
99 | | - Returns the string that was initialized at the creation of the object. |
| 99 | + Generates a Dockerfile string to move files and folders. |
100 | 100 |
|
101 | 101 | Returns: |
102 | | - str: The command string. |
| 102 | + str: A Dockerfile command string for moving files and folders. |
103 | 103 | """ |
104 | 104 |
|
105 | 105 | data_path = Path("/tmp/pythainer/docker/data") |
| 106 | + resulting_path = data_path / self._source_path.relative_to("/") |
| 107 | + relative_path = Path("data") / self._source_path.relative_to("/") |
| 108 | + mkdir(data_path) |
| 109 | + |
| 110 | + print(data_path) |
106 | 111 |
|
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) |
| 112 | + if self._source_path.is_file(): |
| 113 | + mkdir(resulting_path.parent) |
| 114 | + shutil.copyfile(self._source_path, resulting_path) |
| 115 | + elif self._source_path.is_dir(): |
| 116 | + shutil.copytree(self._source_path, resulting_path,dirs_exist_ok=True) |
111 | 117 | else: |
112 | 118 | raise FileExistsError(f'{self._source_path} is not a valid target to copy into the docker container') |
113 | 119 |
|
114 | 120 |
|
115 | 121 |
|
116 | | - cmd = f"COPY --chown=${{USER_NAME}} {self._source_path} {self._destination_path}" |
| 122 | + cmd = f"COPY --chown=${{USER_NAME}} {relative_path} {self._destination_path}" |
117 | 123 |
|
118 | 124 | return cmd |
119 | 125 |
|
|
0 commit comments