import os
from moru import Sandbox
sandbox = Sandbox.create()
local_dir = "./synced_files"
os.makedirs(local_dir, exist_ok=True)
def sync_handler():
handle = sandbox.files.watch_dir("/home/user/output", recursive=True)
for event in handle.events():
if event.type == "CREATE" or event.type == "WRITE":
# Download the file
remote_path = f"/home/user/output/{event.name}"
local_path = os.path.join(local_dir, event.name)
content = sandbox.files.read(remote_path, format="bytes")
os.makedirs(os.path.dirname(local_path), exist_ok=True)
with open(local_path, "wb") as f:
f.write(content)
print(f"Synced: {event.name}")
# Run sync in background thread
import threading
thread = threading.Thread(target=sync_handler, daemon=True)
thread.start()