Skip to content

Commit 40fd110

Browse files
committed
refactor for transferable
Signed-off-by: mgorsk1 <[email protected]>
1 parent acd19f5 commit 40fd110

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

core/testcontainers/core/container.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import contextlib
22
import io
3+
import os
34
import tarfile
4-
from pathlib import Path
55
from platform import system
66
from socket import socket
77
from typing import TYPE_CHECKING, Optional
@@ -16,6 +16,7 @@
1616
from testcontainers.core.exceptions import ContainerStartException
1717
from testcontainers.core.labels import LABEL_SESSION_ID, SESSION_ID
1818
from testcontainers.core.network import Network
19+
from testcontainers.core.transferable import Transferable
1920
from testcontainers.core.utils import inside_container, is_arm, setup_logger
2021
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
2122

@@ -55,7 +56,7 @@ def __init__(
5556
self._network: Optional[Network] = None
5657
self._network_aliases: Optional[list[str]] = None
5758
self._kwargs = kwargs
58-
self._files: list[tuple[Path, Path]] = []
59+
self._files: list[Transferable] = []
5960

6061
def with_env(self, key: str, value: str) -> Self:
6162
self.env[key] = value
@@ -82,12 +83,12 @@ def with_kwargs(self, **kwargs) -> Self:
8283
self._kwargs = kwargs
8384
return self
8485

85-
def with_copy_file_to_container(self, source_file: Path, destination_file: Path) -> Self:
86-
self._files.append((source_file, destination_file))
86+
def with_copy_file_to_container(self, transferable: Transferable) -> Self:
87+
self._files.append(transferable)
8788

8889
return self
8990

90-
def copy_file_from_container(self, container_file: Path, destination_file: Path) -> Path:
91+
def copy_file_from_container(self, container_file: os.PathLike, destination_file: os.PathLike) -> os.PathLike:
9192
tar_stream, _ = self._container.get_archive(container_file)
9293

9394
for chunk in tar_stream:
@@ -99,11 +100,11 @@ def copy_file_from_container(self, container_file: Path, destination_file: Path)
99100
return destination_file
100101

101102
@staticmethod
102-
def _put_file_in_container(container, source_file: Path, destination_file: Path):
103+
def _put_data_in_container(container, transferable: Transferable):
103104
data = io.BytesIO()
104105

105-
with tarfile.open(fileobj=data, mode="w") as tar:
106-
tar.add(source_file, arcname=destination_file)
106+
with transferable as f, tarfile.open(fileobj=data, mode="w") as tar:
107+
tar.add(f.input_path, arcname=f.output_path)
107108

108109
data.seek(0)
109110

@@ -149,10 +150,8 @@ def start(self) -> Self:
149150
if self._network:
150151
self._network.connect(self._container.id, self._network_aliases)
151152

152-
for copy_spec in self._files:
153-
source, destination = copy_spec[0], copy_spec[1]
154-
155-
DockerContainer._put_file_in_container(self._container, source, destination)
153+
for transferable in self._files:
154+
DockerContainer._put_data_in_container(self._container, transferable)
156155

157156
return self
158157

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
import tempfile
3+
from typing import Union
4+
5+
6+
class Transferable:
7+
def __init__(self, input_data: Union[os.PathLike, bytes], output_path: os.PathLike):
8+
self._input = input_data
9+
self._output_path = output_path
10+
11+
self._tmp_file: bool = False
12+
13+
def __enter__(self):
14+
if isinstance(self._input, bytes):
15+
tmp_file = tempfile.NamedTemporaryFile(delete=False)
16+
tmp_file.write(self._input)
17+
18+
self._input = tmp_file.name
19+
self._tmp_file = True
20+
21+
return self
22+
23+
def __exit__(self, *args):
24+
if self._tmp_file:
25+
os.remove(self._input)
26+
27+
@property
28+
def input_path(self):
29+
return self._input
30+
31+
@property
32+
def output_path(self):
33+
return self._output_path

0 commit comments

Comments
 (0)