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
14
14
from testcontainers .core .exceptions import ContainerStartException
15
15
from testcontainers .core .labels import LABEL_SESSION_ID , SESSION_ID
16
16
from testcontainers .core .network import Network
17
+ from testcontainers .core .transferable import Transferable
17
18
from testcontainers .core .utils import inside_container , is_arm , setup_logger
18
19
from testcontainers .core .waiting_utils import wait_container_is_ready , wait_for_logs
19
20
@@ -53,7 +54,7 @@ def __init__(
53
54
self ._network : Optional [Network ] = None
54
55
self ._network_aliases : Optional [list [str ]] = None
55
56
self ._kwargs = kwargs
56
- self ._files : list [tuple [ Path , Path ] ] = []
57
+ self ._files : list [Transferable ] = []
57
58
58
59
def with_env (self , key : str , value : str ) -> Self :
59
60
self .env [key ] = value
@@ -80,12 +81,12 @@ def with_kwargs(self, **kwargs) -> Self:
80
81
self ._kwargs = kwargs
81
82
return self
82
83
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 )
85
86
86
87
return self
87
88
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 :
89
90
tar_stream , _ = self ._container .get_archive (container_file )
90
91
91
92
for chunk in tar_stream :
@@ -97,11 +98,11 @@ def copy_file_from_container(self, container_file: Path, destination_file: Path)
97
98
return destination_file
98
99
99
100
@staticmethod
100
- def _put_file_in_container (container , source_file : Path , destination_file : Path ):
101
+ def _put_data_in_container (container , transferable : Transferable ):
101
102
data = io .BytesIO ()
102
103
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 )
105
106
106
107
data .seek (0 )
107
108
@@ -133,10 +134,8 @@ def start(self) -> Self:
133
134
if self ._network :
134
135
self ._network .connect (self ._container .id , self ._network_aliases )
135
136
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 )
140
139
141
140
return self
142
141
0 commit comments