Skip to content

Commit 918f120

Browse files
committed
refactor for transferable
Signed-off-by: mgorsk1 <[email protected]>
1 parent 4e7aeda commit 918f120

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
@@ -14,6 +14,7 @@
1414
from testcontainers.core.exceptions import ContainerStartException
1515
from testcontainers.core.labels import LABEL_SESSION_ID, SESSION_ID
1616
from testcontainers.core.network import Network
17+
from testcontainers.core.transferable import Transferable
1718
from testcontainers.core.utils import inside_container, is_arm, setup_logger
1819
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
1920

@@ -53,7 +54,7 @@ def __init__(
5354
self._network: Optional[Network] = None
5455
self._network_aliases: Optional[list[str]] = None
5556
self._kwargs = kwargs
56-
self._files: list[tuple[Path, Path]] = []
57+
self._files: list[Transferable] = []
5758

5859
def with_env(self, key: str, value: str) -> Self:
5960
self.env[key] = value
@@ -80,12 +81,12 @@ def with_kwargs(self, **kwargs) -> Self:
8081
self._kwargs = kwargs
8182
return self
8283

83-
def with_copy_file_to_container(self, source_file: Path, destination_file: Path) -> Self:
84-
self._files.append((source_file, destination_file))
84+
def with_copy_file_to_container(self, transferable: Transferable) -> Self:
85+
self._files.append(transferable)
8586

8687
return self
8788

88-
def copy_file_from_container(self, container_file: Path, destination_file: Path) -> Path:
89+
def copy_file_from_container(self, container_file: os.PathLike, destination_file: os.PathLike) -> os.PathLike:
8990
tar_stream, _ = self._container.get_archive(container_file)
9091

9192
for chunk in tar_stream:
@@ -97,11 +98,11 @@ def copy_file_from_container(self, container_file: Path, destination_file: Path)
9798
return destination_file
9899

99100
@staticmethod
100-
def _put_file_in_container(container, source_file: Path, destination_file: Path):
101+
def _put_data_in_container(container, transferable: Transferable):
101102
data = io.BytesIO()
102103

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

106107
data.seek(0)
107108

@@ -133,10 +134,8 @@ def start(self) -> Self:
133134
if self._network:
134135
self._network.connect(self._container.id, self._network_aliases)
135136

136-
for copy_spec in self._files:
137-
source, destination = copy_spec[0], copy_spec[1]
138-
139-
DockerContainer._put_file_in_container(self._container, source, destination)
137+
for transferable in self._files:
138+
DockerContainer._put_data_in_container(self._container, transferable)
140139

141140
return self
142141

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)