Skip to content

Commit

Permalink
write to docker as tar file
Browse files Browse the repository at this point in the history
  • Loading branch information
granawkins committed Jul 19, 2024
1 parent 5dc87d6 commit 505588c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages=["ragdaemon"]

[project]
name = "ragdaemon"
version = "0.8.0"
version = "0.8.1"
description = "Generate and render a call graph for a Python project."
readme = "README.md"
dependencies = [
Expand Down
2 changes: 1 addition & 1 deletion ragdaemon/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.8.0"
__version__ = "0.8.1"
35 changes: 30 additions & 5 deletions ragdaemon/io/docker_io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import io
import os
import tarfile
import tempfile
from contextlib import contextmanager
from pathlib import Path
from typing import Iterator, Optional, Set
Expand Down Expand Up @@ -35,13 +39,27 @@ def read(self, size: int = -1) -> str:
def write(self, data: str) -> int:
if "w" not in self.mode:
raise IOError("File not opened in write mode")
result = self.container.exec_run(f"sh -c 'printf \"%s\" > {self.path}'" % data)
if result.exit_code != 0:
raise IOError(
f"Failed to write file {self.path} in container: {result.stderr.decode('utf-8')}"
)

with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(data.encode('utf-8'))
temp_file_path = temp_file.name

# Create a tar archive of the temporary file
tar_stream = io.BytesIO()
with tarfile.open(fileobj=tar_stream, mode='w') as tar:
tar.add(temp_file_path, arcname=os.path.basename(self.path))
tar_stream.seek(0)

# Put the archive into the container
self.container.put_archive(os.path.dirname(self.path), tar_stream)

# Clean up the temporary file
os.remove(temp_file_path)

return len(data)



def __enter__(self) -> "FileInDocker":
return self

Expand Down Expand Up @@ -153,3 +171,10 @@ def rename(self, src: Path | str, dst: Path | str):
def exists(self, path: Path | str) -> bool:
result = self.container.exec_run(f"test -e {self.cwd / path}")
return result.exit_code == 0


"""
b'1, image: https://via.placeholder.com/150 },
{ title: Card: 7: Syntax error: Unterminated quoted string
'
"""

0 comments on commit 505588c

Please sign in to comment.