import io
import tarfile
import time
from declaw import Sandbox, Volumes, VolumeAttachment
def build_sample_tarball() -> bytes:
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w:gz") as tar:
for name, body in (
("config.json", b'{"env":"prod"}\n'),
("data/rows.csv", b"id,name\n1,alice\n2,bob\n"),
):
info = tarfile.TarInfo(name=name)
info.size = len(body)
info.mtime = int(time.time())
tar.addfile(info, io.BytesIO(body))
return buf.getvalue()
vol = Volumes.create(name="demo-dataset", data=build_sample_tarball())
print(vol.volume_id) # vol-...
print(vol.size_bytes)
sbx = Sandbox.create(
template="base",
timeout=120,
volumes=[VolumeAttachment(volume_id=vol.volume_id, mount_path="/data")],
)
try:
result = sbx.commands.run("ls -la /data && cat /data/config.json")
print(result.stdout)
finally:
sbx.kill()