SandboxFileSystem

class baseten.sandbox.SandboxFileSystem

Bases: object

Files and directories in a sandbox.

Relative paths are resolved against the sandbox’s working directory.

Parameters:
copy(*, source, destination, timeout=datetime.timedelta(seconds=180))

Copy a file or directory.

Runs cp -r in the sandbox through SandboxProcess.exec(), then waits for it with SandboxProcess.wait(), so cp’s rules apply, such as copying into a destination that is an existing directory.

Parameters:
  • source (str) – Path of the file or directory to copy.

  • destination (str) – Path to copy to.

  • timeout (timedelta | None) – How long to wait for the copy, which keeps running regardless. None waits with no limit.

Raises:
  • SandboxFileSystemCopyError – The copy did not succeed.

  • SandboxProcessWaitTimeoutError – The copy was still running when the wait timed out.

Return type:

None

find(*, path, type=None, patterns=None, max_results=None, exclude_dirs=None, exclude_hidden=None)

Find files and directories by name under a directory.

Parameters:
  • path (str) – Path of the directory to search in.

  • type (Literal['file', 'directory'] | str | None) – Whether to find only files or only directories. None finds both.

  • patterns (list[str] | None) – File patterns to include, such as "*.py".

  • max_results (int | None) – Maximum number of results. 0 means all. The sandbox defaults to 20.

  • exclude_dirs (list[str] | None) – Directory names to skip. The sandbox defaults to common dependency and build directories, such as node_modules and .git.

  • exclude_hidden (bool | None) – Whether to skip hidden files and directories. The sandbox defaults to true.

Return type:

SandboxFileSystemFindResult

grep(*, path, query, case_sensitive=None, max_results=None, file_pattern=None, exclude_dirs=None, context_lines=None)

Search the contents of files under a directory for text.

Parameters:
  • path (str) – Path of the directory to search in.

  • query (str) – Text to search for.

  • case_sensitive (bool | None) – Whether the search is case sensitive. The sandbox defaults to false.

  • max_results (int | None) – Maximum number of results. The sandbox defaults to 100.

  • file_pattern (str | None) – File pattern to include, such as "*.py".

  • exclude_dirs (list[str] | None) – Directory names to skip. The sandbox defaults to common dependency and build directories, such as node_modules and .git.

  • context_lines (int | None) – How many lines before and after each match to include in its SandboxFileSystemGrepMatch.context, at most 20. None includes none.

Return type:

SandboxFileSystemGrepResult

list(*, path)

List a directory’s files and subdirectories, one level deep.

Parameters:

path (str) – Path of the directory.

Return type:

SandboxFileSystemDirectory

mkdir(*, path, permissions=None)

Create a directory, along with any missing parents.

Parameters:
  • path (str) – Path of the directory.

  • permissions (str | None) – Octal directory mode, such as "0755". None uses the sandbox’s default.

Return type:

None

read(*, path)

Read a text file.

Parameters:

path (str) – Path of the file.

Return type:

str

read_bytes(*, path)

Read a file as bytes.

Parameters:

path (str) – Path of the file.

Return type:

bytes

remove(*, path, recursive=None)

Remove a file or directory.

Parameters:
  • path (str) – Path of the file or directory.

  • recursive (bool | None) – Whether to remove a directory and everything in it. A directory that is not empty fails without it.

Return type:

None

watch(*, path, recursive=None, ignore=None)

Stream changes in a directory as they happen.

Only the directory’s direct entries are watched unless recursive is set. Closing the stream stops watching.

Parameters:
  • path (str) – Path of the directory to watch.

  • recursive (bool | None) – Whether to also watch every subdirectory, including ones created later.

  • ignore (list[str] | None) – Skips events whose full path contains any of these substrings.

Return type:

SandboxStream[SandboxFileSystemWatchEvent]

write(*, path, content)

Write a text file, creating it or replacing its content.

Content over 5MB is uploaded in 5MB parts through the multipart upload endpoints, at most 2 parts at a time per sandbox.

Parameters:
  • path (str) – Path of the file, created or replaced.

  • content (str) – Text content to write.

Return type:

None

write_bytes(*, path, content, permissions=None)

Write a file from bytes, creating it or replacing its content.

Content over 5MB is uploaded in 5MB parts through the multipart upload endpoints, at most 2 parts at a time per sandbox.

Parameters:
  • path (str) – Path of the file, created or replaced.

  • content (bytes) – Content to write.

  • permissions (str | None) – Octal file mode, such as "0755", applied when the file is created. An existing file keeps its mode. None uses the sandbox’s default.

Return type:

None

write_tree(*, path, files)

Write several text files under a directory in one call, creating or replacing each.

Parameters:
  • path (str) – Path of the directory to write under.

  • files (Mapping[str, str]) – Text content of each file, by path relative to path.

Return type:

None