FileSystem
class FileSystem()
サンドボックス内のファイルシステム操作を提供します。
このクラスは、Daytona のサンドボックス内で実行できるファイルシステム操作に対する高水準インターフェースを実装します。
FileSystem.__init__
def __init__(sandbox_id: str, toolbox_api: ToolboxApi, get_root_dir: Callable[[], str])
新しい FileSystem インスタンスを初期化します。
引数:
sandbox_id
str - サンドボックス ID。toolbox_api
ToolboxApi - サンドボックス操作用の API クライアント。get_root_dir
Callable[[], str] - サンドボックスのデフォルトのルートディレクトリを取得する関数。
FileSystem.create_folder
@intercept_errors(message_prefix="Failed to create folder: ")def create_folder(path: str, mode: str) -> None
指定したパスに、与えられた権限でサンドボックス内に新しいディレクトリを作成します。
引数:
path
str - フォルダを作成するパス。相対パスはユーザーのルートディレクトリを基準に解決されます。mode
str - 8 進数形式のフォルダ権限(例: “755” は rwxr-xr-x)。
例:
# 標準的な権限でディレクトリを作成sandbox.fs.create_folder("workspace/data", "755")
# プライベートディレクトリを作成sandbox.fs.create_folder("workspace/secrets", "700")
FileSystem.delete_file
@intercept_errors(message_prefix="Failed to delete file: ")def delete_file(path: str) -> None
サンドボックスからファイルを削除します。
引数:
path
str - 削除するファイルへの絶対パス。
例:
# ファイルを削除sandbox.fs.delete_file("workspace/data/old_file.txt")
FileSystem.download_file
@overloaddef download_file(remote_path: str, timeout: int = 30 * 60) -> bytes
サンドボックスからファイルをダウンロードします。ファイル内容を bytes オブジェクトとして返します。 ディスクに保存せずにファイルをメモリに読み込みたい場合に便利です。 小さなファイルにのみ使用できます。
引数:
remote_path
str - サンドボックス内のファイルへのパス。相対パスはユーザーのルートディレクトリを基準に解決されます。timeout
int - ダウンロード処理のタイムアウト(秒)。0 はタイムアウトなし。デフォルトは 30 分。
戻り値:
bytes
- ファイル内容の bytes オブジェクト。
例:
# ダウンロードしてローカルに保存content = sandbox.fs.download_file("workspace/data/file.txt")with open("local_copy.txt", "wb") as f: f.write(content)
# ダウンロードしてテキスト内容を処理content = sandbox.fs.download_file("workspace/data/config.json")config = json.loads(content.decode('utf-8'))
FileSystem.download_file
@overloaddef download_file(remote_path: str, local_path: str, timeout: int = 30 * 60) -> None
サンドボックスからファイルをダウンロードし、ストリームでローカルファイルに保存します。 メモリに収まりきらない可能性のある大きなファイルをダウンロードしたい場合に便利です。
引数:
remote_path
str - サンドボックス内のファイルへのパス。相対パスはユーザーのルートディレクトリを基準に解決されます。local_path
str - ローカルにファイルを保存するパス。timeout
int - ダウンロード処理のタイムアウト(秒)。0 はタイムアウトなし。デフォルトは 30 分。
例:
local_path = "local_copy.txt"sandbox.fs.download_file("tmp/large_file.txt", local_path)size_mb = os.path.getsize(local_path) / 1024 / 1024print(f"Size of the downloaded file {local_path}: {size_mb} MB")
FileSystem.find_files
@intercept_errors(message_prefix="Failed to find files: ")def find_files(path: str, pattern: str) -> List[Match]
パターンに一致するファイルを検索します。grep
コマンドに似ています。
引数:
path
str - 検索対象のファイルまたはディレクトリのパス。パスがディレクトリの場合、 検索は再帰的に実行されます。相対パスはユーザーの ルートディレクトリを基準に解決されます。pattern
str - ファイル内容に対して照合する検索パターン。
戻り値:
List[Match]
- ファイル内で見つかった一致のリスト。各 Match オブジェクトには以下が含まれます:- file: 一致が含まれるファイルのパス
- line: 一致が見つかった行番号
- content: 一致した行の内容
例:
# Search for TODOs in Python filesmatches = sandbox.fs.find_files("workspace/src", "TODO:")for match in matches: print(f"{match.file}:{match.line}: {match.content.strip()}")
FileSystem.get_file_info
@intercept_errors(message_prefix="Failed to get file info: ")def get_file_info(path: str) -> FileInfo
ファイルまたはディレクトリの詳細情報を取得します。サイズ、パーミッション、タイムスタンプを含みます。
引数:
path
str - ファイルまたはディレクトリのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。
戻り値:
FileInfo
- 次を含む詳細なファイル情報:- name: ファイル名
- is_dir: パスがディレクトリかどうか
- size: バイト単位のファイルサイズ
- mode: ファイルのパーミッション
- mod_time: 最終更新タイムスタンプ
- permissions: 8 進数表記のファイルパーミッション
- owner: ファイル所有者
- group: ファイルグループ
例:
# Get file metadatainfo = sandbox.fs.get_file_info("workspace/data/file.txt")print(f"Size: {info.size} bytes")print(f"Modified: {info.mod_time}")print(f"Mode: {info.mode}")
# Check if path is a directoryinfo = sandbox.fs.get_file_info("workspace/data")if info.is_dir: print("Path is a directory")
FileSystem.list_files
@intercept_errors(message_prefix="Failed to list files: ")def list_files(path: str) -> List[FileInfo]
指定したパス内のファイルとディレクトリを一覧表示し、その情報を返します。ls -l
コマンドに似ています。
引数:
path
str - 内容を一覧表示するディレクトリのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。
戻り値:
List[FileInfo]
- ファイルおよびディレクトリ情報のリスト。各 FileInfo オブジェクトには get_file_info() と同じフィールドが含まれます。
例:
# List directory contentsfiles = sandbox.fs.list_files("workspace/data")
# Print files and their sizesfor file in files: if not file.is_dir: print(f"{file.name}: {file.size} bytes")
# List only directoriesdirs = [f for f in files if f.is_dir]print("Subdirectories:", ", ".join(d.name for d in dirs))
FileSystem.move_files
@intercept_errors(message_prefix="Failed to move files: ")def move_files(source: str, destination: str) -> None
ファイルまたはディレクトリを移動またはリネームします。宛先の親ディレクトリは存在している必要があります。
引数:
source
str - 移動元のファイルまたはディレクトリのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。destination
str - 移動先のパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。
例:
# Rename a filesandbox.fs.move_files( "workspace/data/old_name.txt", "workspace/data/new_name.txt")
# Move a file to a different directorysandbox.fs.move_files( "workspace/data/file.txt", "workspace/archive/file.txt")
# Move a directorysandbox.fs.move_files( "workspace/old_dir", "workspace/new_dir")
FileSystem.replace_in_files
@intercept_errors(message_prefix="Failed to replace in files: ")def replace_in_files(files: List[str], pattern: str, new_value: str) -> List[ReplaceResult]
複数ファイルに対して検索と置換を実行します。
引数:
files
List[str] - 置換を実行するファイルパスのリスト。相対パスはユーザーの ルートディレクトリを基準に解決されます。pattern
str - 検索するパターン。new_value
str - マッチした部分を置き換えるテキスト。
戻り値:
List[ReplaceResult]
- 各ファイルで行われた置換を示す結果のリスト。各 ReplaceResult には次が含まれます:- file: 変更されたファイルのパス
- success: 操作が成功したかどうか
- error: 操作が失敗した場合のエラーメッセージ
例:
# 特定のファイルで置換を実行results = sandbox.fs.replace_in_files( files=["workspace/src/file1.py", "workspace/src/file2.py"], pattern="old_function", new_value="new_function")
# 結果を出力for result in results: if result.success: print(f"{result.file}: {result.success}") else: print(f"{result.file}: {result.error}")
FileSystem.search_files
@intercept_errors(message_prefix="Failed to search files: ")def search_files(path: str, pattern: str) -> SearchFilesResponse
指定したパターンに一致する名前のファイルやディレクトリを検索します。パターンは単純な文字列またはグロブパターンを指定できます。
引数:
path
str - 検索を開始するルートディレクトリのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。pattern
str - ファイル名にマッチさせるパターン。グロブ パターン(例: Python ファイルなら “*.py”)をサポートします。
戻り値:
SearchFilesResponse
- 検索結果を含みます:- files: 一致したファイルおよびディレクトリのパスのリスト
例:
# すべての Python ファイルを検索result = sandbox.fs.search_files("workspace", "*.py")for file in result.files: print(file)
# 特定のプレフィックスを持つファイルを検索result = sandbox.fs.search_files("workspace/data", "test_*")print(f"Found {len(result.files)} test files")
FileSystem.set_file_permissions
@intercept_errors(message_prefix="Failed to set file permissions: ")def set_file_permissions(path: str, mode: str = None, owner: str = None, group: str = None) -> None
ファイルまたはディレクトリのパーミッションと所有権を設定します。いずれのパラメータも None を指定すると、その属性は変更されません。
引数:
path
str - ファイルまたはディレクトリのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。mode
Optional[str] - 8 進数形式のファイルモード/パーミッション (例: “644” は rw-r—r—)。owner
Optional[str] - ファイルの所有ユーザー。group
Optional[str] - ファイルの所有グループ。
例:
# ファイルを実行可能にするsandbox.fs.set_file_permissions( path="workspace/scripts/run.sh", mode="755" # rwxr-xr-x)
# ファイルの所有者を変更sandbox.fs.set_file_permissions( path="workspace/data/file.txt", owner="daytona", group="daytona")
FileSystem.upload_file
@overloaddef upload_file(file: bytes, remote_path: str, timeout: int = 30 * 60) -> None
サンドボックス内の指定したパスにファイルをアップロードします。宛先に同名のファイルが既に存在する場合は上書きされます。メモリに収まる小さなファイルをアップロードする場合に適しています。
引数:
file
bytes - バイト列オブジェクトとしてのファイル内容。remote_path
str - 宛先ファイルのパス。相対パスはユーザーの ルートディレクトリを基準に解決されます。timeout
int - アップロード処理のタイムアウト(秒)。0 はタイムアウトなし。デフォルトは 30 分。
例:
# テキストファイルをアップロードcontent = b"Hello, World!"sandbox.fs.upload_file(content, "tmp/hello.txt")
# ローカルファイルをアップロードwith open("local_file.txt", "rb") as f: content = f.read()sandbox.fs.upload_file(content, "tmp/file.txt")
# バイナリデータをアップロードimport jsondata = {"key": "value"}content = json.dumps(data).encode('utf-8')sandbox.fs.upload_file(content, "tmp/config.json")
FileSystem.upload_file
@overloaddef upload_file(local_path: str, remote_path: str, timeout: int = 30 * 60) -> None
ローカルファイルシステムからサンドボックス内の指定パスにファイルをアップロードします。 宛先パスにすでにファイルが存在する場合は上書きされます。このメソッドは ストリーミングでファイルをアップロードするため、メモリに収まりきらない大きなファイルの アップロードに適しています。
引数:
local_path
str - アップロードするローカルファイルのパス。remote_path
str - サンドボックス内の宛先ファイルのパス。相対パスは ユーザーのルートディレクトリを基準に解決されます。timeout
int - アップロード処理のタイムアウト(秒)。0 はタイムアウトなし。既定は 30 分。
例:
sandbox.fs.upload_file("local_file.txt", "tmp/large_file.txt")
FileSystem.upload_files
@intercept_errors(message_prefix="Failed to upload files: ")def upload_files(files: List[FileUpload], timeout: int = 30 * 60) -> None
複数のファイルをサンドボックスにアップロードします。宛先パスにすでにファイルが存在する場合は 上書きされます。
引数:
files
List[FileUpload] - アップロードするファイルのリスト。timeout
int - アップロード処理のタイムアウト(秒)。0 はタイムアウトなし。既定は 30 分。
例:
# 複数のテキストファイルをアップロードfiles = [ FileUpload( source=b"Content of file 1", destination="/tmp/file1.txt" ), FileUpload( source="workspace/data/file2.txt", destination="/tmp/file2.txt" ), FileUpload( source=b'{"key": "value"}', destination="/tmp/config.json" )]sandbox.fs.upload_files(files)
FileUpload
@dataclassclass FileUpload()
サンドボックスにアップロードするファイルを表します。
属性:
source
Union[bytes, str] - ファイル内容。バイト列オブジェクト、またはローカルファイルパスを指定できます。バイト列を指定する場合は、メモリに収まるサイズであることを確認してください。そうでない場合は、内容がサンドボックスへストリーミングされるローカルファイルパスを使用してください。destination
str - サンドボックス内の保存先の絶対パス。相対パスはユーザーのルートディレクトリを基準に解決されます。