Ejemplo n.º 1
0
def get_file(container: docker.models.containers.Container,
             path: str,
             dest: str = ".") -> None:
    """Get a single file from a container.

    :param path: The path to retrieve.
    :param dest: The filename to extract to. If it is an existing directory, the filename from the
                 tar file will be used.
    """
    if os.path.isdir(dest):
        fname = None
    else:
        fname = dest

    it, _ = container.get_archive(path)
    reader = _DLReader(it)
    tf = tarfile.TarFile(mode="r", fileobj=reader)
    for member in tf:
        ## We only need the first one
        f = tf.extractfile(member)
        if fname is None:
            fname = os.path.join(dest, member.name)

        with open(fname, "wb") as of:
            shutil.copyfileobj(f, of)
Ejemplo n.º 2
0
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)