-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
68 lines (53 loc) · 2.27 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import io
import tarfile
from pathlib import Path
import os.path
from typing import Optional, List
import docker
class FileTooBigException(Exception):
def __init__(self, filename: str, size: int, max_size: int) -> None:
message = "File {} is {} bytes, (max size is {} bytes)".format(filename, size, max_size)
super().__init__(message)
self.filename = filename
self.size = size
self.max_size = max_size
def copy_container_to_host(container: docker.models.containers.Container, file: str, dest_path: str, maxsize: int = 0) -> None:
"""
Copies a file from a container to the host.
dest_path needs to be the destination directory.
Max size may be limited by maxsize. Value of 0 means there's no limitation.
"""
stream = io.BytesIO()
bits, stat = container.get_archive(file)
if maxsize > 0 and stat['size'] > maxsize:
raise FileTooBigException(size=stat['size'], max_size=maxsize, filename=stat['name'])
for chunk in bits:
stream.write(chunk)
stream.seek(0)
with tarfile.TarFile(fileobj=stream) as archive:
archive.extractall(dest_path)
def copy_host_to_container(container: docker.models.containers.Container, file: str, dest_path: str) -> None:
"""Copies a file or a folder from the host to the container. file may be a source folder or a file."""
if Path(file).is_dir():
archive = create_archive(file, arcname=Path(dest_path).name)
container.put_archive(path=Path(dest_path).parent.as_posix(), data=archive)
else:
archive = create_archive(file)
container.put_archive(path=dest_path, data=archive)
archive.close()
with create_archive(file) as archive:
container.put_archive(path=dest_path, data=archive)
def create_archive(file: str, arcname: Optional[str] = None) -> io.BytesIO:
"""Creates a TAR archive out of a file or a directory."""
stream = io.BytesIO()
if not arcname:
arcname = Path(file).name
with tarfile.TarFile(fileobj=stream, mode='w') as archive:
archive.add(file, arcname)
stream.seek(0)
return stream
def load_tokens(path: str) -> List[str]:
"""Loads the current tokens from file"""
with open(path, 'r') as f:
res = [x.strip() for x in f.readlines()]
return res