1
1
import contextlib
2
2
import io
3
+ import os
3
4
import tarfile
4
- from pathlib import Path
5
5
from platform import system
6
6
from socket import socket
7
7
from typing import TYPE_CHECKING , Optional
16
16
from testcontainers .core .exceptions import ContainerStartException
17
17
from testcontainers .core .labels import LABEL_SESSION_ID , SESSION_ID
18
18
from testcontainers .core .network import Network
19
+ from testcontainers .core .transferable import Transferable
19
20
from testcontainers .core .utils import inside_container , is_arm , setup_logger
20
21
from testcontainers .core .waiting_utils import wait_container_is_ready , wait_for_logs
21
22
@@ -55,7 +56,7 @@ def __init__(
55
56
self ._network : Optional [Network ] = None
56
57
self ._network_aliases : Optional [list [str ]] = None
57
58
self ._kwargs = kwargs
58
- self ._files : list [tuple [ Path , Path ] ] = []
59
+ self ._files : list [Transferable ] = []
59
60
60
61
def with_env (self , key : str , value : str ) -> Self :
61
62
self .env [key ] = value
@@ -82,12 +83,12 @@ def with_kwargs(self, **kwargs) -> Self:
82
83
self ._kwargs = kwargs
83
84
return self
84
85
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 )
87
88
88
89
return self
89
90
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 :
91
92
tar_stream , _ = self ._container .get_archive (container_file )
92
93
93
94
for chunk in tar_stream :
@@ -99,11 +100,11 @@ def copy_file_from_container(self, container_file: Path, destination_file: Path)
99
100
return destination_file
100
101
101
102
@staticmethod
102
- def _put_file_in_container (container , source_file : Path , destination_file : Path ):
103
+ def _put_data_in_container (container , transferable : Transferable ):
103
104
data = io .BytesIO ()
104
105
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 )
107
108
108
109
data .seek (0 )
109
110
@@ -149,10 +150,8 @@ def start(self) -> Self:
149
150
if self ._network :
150
151
self ._network .connect (self ._container .id , self ._network_aliases )
151
152
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 )
156
155
157
156
return self
158
157
0 commit comments