|
20 | 20 | from testcontainers.core.exceptions import ContainerConnectException, ContainerStartException |
21 | 21 | from testcontainers.core.labels import LABEL_SESSION_ID, SESSION_ID |
22 | 22 | from testcontainers.core.network import Network |
23 | | -from testcontainers.core.transferable import Transferable |
| 23 | +from testcontainers.core.transferable import Transferable, TransferSpec |
24 | 24 | from testcontainers.core.utils import is_arm, setup_logger |
25 | 25 | from testcontainers.core.wait_strategies import LogMessageWaitStrategy |
26 | 26 | from testcontainers.core.waiting_utils import WaitStrategy, wait_container_is_ready |
@@ -75,7 +75,7 @@ def __init__( |
75 | 75 | network: Optional[Network] = None, |
76 | 76 | network_aliases: Optional[list[str]] = None, |
77 | 77 | _wait_strategy: Optional[WaitStrategy] = None, |
78 | | - transferables: Optional[list[Transferable]] = None, |
| 78 | + transferables: Optional[list[TransferSpec]] = None, |
79 | 79 | **kwargs: Any, |
80 | 80 | ) -> None: |
81 | 81 | self.env = env or {} |
@@ -104,7 +104,11 @@ def __init__( |
104 | 104 |
|
105 | 105 | self._kwargs = kwargs |
106 | 106 | self._wait_strategy: Optional[WaitStrategy] = _wait_strategy |
107 | | - self._transferables: list[Transferable] = transferables or [] |
| 107 | + |
| 108 | + self._transferable_specs: list[TransferSpec] = [] |
| 109 | + if transferables: |
| 110 | + for t in transferables: |
| 111 | + self.with_copy_into_container(*t) |
108 | 112 |
|
109 | 113 | def with_env(self, key: str, value: str) -> Self: |
110 | 114 | self.env[key] = value |
@@ -214,8 +218,8 @@ def start(self) -> Self: |
214 | 218 |
|
215 | 219 | logger.info("Container started: %s", self._container.short_id) |
216 | 220 |
|
217 | | - for t in self._transferables: |
218 | | - self._transfer_into_container(t.source, t.destination_in_container, t.mode) |
| 221 | + for t in self._transferable_specs: |
| 222 | + self._transfer_into_container(*t) |
219 | 223 |
|
220 | 224 | return self |
221 | 225 |
|
@@ -309,24 +313,20 @@ def _configure(self) -> None: |
309 | 313 | pass |
310 | 314 |
|
311 | 315 | def with_copy_into_container( |
312 | | - self, file_content: Union[bytes, pathlib.Path], destination_in_container: str, mode: int = 0o644 |
| 316 | + self, transferable: Transferable, destination_in_container: str, mode: int = 0o644 |
313 | 317 | ) -> Self: |
314 | | - self._transferables.append(Transferable(file_content, destination_in_container, mode)) |
| 318 | + self._transferable_specs.append((transferable, destination_in_container, mode)) |
315 | 319 | return self |
316 | 320 |
|
317 | | - def copy_into_container( |
318 | | - self, file_content: Union[bytes, pathlib.Path], destination_in_container: str, mode: int = 0o644 |
319 | | - ) -> None: |
320 | | - return self._transfer_into_container(file_content, destination_in_container, mode) |
| 321 | + def copy_into_container(self, transferable: Transferable, destination_in_container: str, mode: int = 0o644) -> None: |
| 322 | + return self._transfer_into_container(transferable, destination_in_container, mode) |
321 | 323 |
|
322 | | - def _transfer_into_container( |
323 | | - self, source: Union[bytes, pathlib.Path], destination_in_container: str, mode: int |
324 | | - ) -> None: |
325 | | - if isinstance(source, bytes): |
326 | | - file_content = source |
327 | | - elif isinstance(source, pathlib.Path): |
328 | | - assert source.is_file() # Temporary, only copying file supported |
329 | | - file_content = source.read_bytes() |
| 324 | + def _transfer_into_container(self, transferable: Transferable, destination_in_container: str, mode: int) -> None: |
| 325 | + if isinstance(transferable, bytes): |
| 326 | + file_content = transferable |
| 327 | + elif isinstance(transferable, pathlib.Path): |
| 328 | + assert transferable.is_file() # Temporary, only copying file supported |
| 329 | + file_content = transferable.read_bytes() |
330 | 330 | else: |
331 | 331 | raise TypeError("source must be bytes or PathLike") |
332 | 332 |
|
|
0 commit comments