1
1
import contextlib
2
- import dataclasses
3
2
import io
4
3
import pathlib
5
4
import tarfile
@@ -74,7 +73,7 @@ def __init__(
74
73
volumes : Optional [list [tuple [str , str , str ]]] = None ,
75
74
network : Optional [Network ] = None ,
76
75
network_aliases : Optional [list [str ]] = None ,
77
- transferrables : Optional [list [Transferable ]] = None ,
76
+ transferables : Optional [list [Transferable ]] = None ,
78
77
** kwargs : Any ,
79
78
) -> None :
80
79
self .env = env or {}
@@ -102,7 +101,7 @@ def __init__(
102
101
self .with_network_aliases (* network_aliases )
103
102
104
103
self ._kwargs = kwargs
105
- self ._transferables : list [Transferable ] = transferrables or []
104
+ self ._transferables : list [Transferable ] = transferables or []
106
105
107
106
def with_env (self , key : str , value : str ) -> Self :
108
107
self .env [key ] = value
@@ -285,20 +284,23 @@ def _configure(self) -> None:
285
284
pass
286
285
287
286
def with_copy_into_container (
288
- self , file_content : bytes | PathLike , destination_in_container : str , mode : int = 0o644
289
- ):
287
+ self , file_content : Union [ bytes , pathlib . Path ] , destination_in_container : str , mode : int = 0o644
288
+ ) -> Self :
290
289
self ._transferables .append (Transferable (file_content , destination_in_container , mode ))
291
290
return self
292
291
293
- def copy_into_container (self , file_content : bytes | PathLike , destination_in_container : str , mode : int = 0o644 ):
292
+ def copy_into_container (
293
+ self , file_content : Union [bytes , pathlib .Path ], destination_in_container : str , mode : int = 0o644
294
+ ) -> None :
294
295
return self ._transfer_into_container (file_content , destination_in_container , mode )
295
296
296
- def _transfer_into_container (self , source : bytes | PathLike , destination_in_container : str , mode : int ):
297
+ def _transfer_into_container (
298
+ self , source : Union [bytes , pathlib .Path ], destination_in_container : str , mode : int
299
+ ) -> None :
297
300
if isinstance (source , bytes ):
298
301
file_content = source
299
- elif isinstance (source , PathLike ):
300
- p = pathlib .Path (source )
301
- file_content = p .read_bytes ()
302
+ elif isinstance (source , pathlib .Path ):
303
+ file_content = source .read_bytes ()
302
304
else :
303
305
raise TypeError ("source must be bytes or PathLike" )
304
306
@@ -309,17 +311,21 @@ def _transfer_into_container(self, source: bytes | PathLike, destination_in_cont
309
311
tarinfo .mode = mode
310
312
tar .addfile (tarinfo , io .BytesIO (file_content ))
311
313
fileobj .seek (0 )
314
+ assert self ._container is not None
312
315
rv = self ._container .put_archive (path = "/" , data = fileobj .getvalue ())
313
316
assert rv is True
314
317
315
- def copy_from_container (self , source_in_container : str , destination_on_host : PathLike ):
318
+ def copy_from_container (self , source_in_container : str , destination_on_host : pathlib .Path ) -> None :
319
+ assert self ._container is not None
316
320
tar_stream , _ = self ._container .get_archive (source_in_container )
317
321
318
322
for chunk in tar_stream :
319
323
with tarfile .open (fileobj = io .BytesIO (chunk )) as tar :
320
324
for member in tar .getmembers ():
321
325
with open (destination_on_host , "wb" ) as f :
322
- f .write (tar .extractfile (member ).read ())
326
+ fileobj = tar .extractfile (member )
327
+ assert fileobj is not None
328
+ f .write (fileobj .read ())
323
329
324
330
325
331
class Reaper :
0 commit comments