11import contextlib
22import io
3+ import os
34import tarfile
4- from pathlib import Path
55from platform import system
66from socket import socket
77from typing import TYPE_CHECKING , Optional
1414from testcontainers .core .exceptions import ContainerStartException
1515from testcontainers .core .labels import LABEL_SESSION_ID , SESSION_ID
1616from testcontainers .core .network import Network
17+ from testcontainers .core .transferable import Transferable
1718from testcontainers .core .utils import inside_container , is_arm , setup_logger
1819from 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
0 commit comments