sandbox.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import json
  2. import os
  3. from abc import ABC, abstractmethod
  4. from opendevin.core.config import config
  5. from opendevin.core.schema import CancellableStream
  6. from opendevin.runtime.plugins.mixin import PluginMixin
  7. class Sandbox(ABC, PluginMixin):
  8. _env: dict[str, str] = {}
  9. is_initial_session: bool = True
  10. def __init__(self, **kwargs):
  11. for key in os.environ:
  12. if key.startswith('SANDBOX_ENV_'):
  13. sandbox_key = key.removeprefix('SANDBOX_ENV_')
  14. self.add_to_env(sandbox_key, os.environ[key])
  15. if config.enable_auto_lint:
  16. self.add_to_env('ENABLE_AUTO_LINT', 'true')
  17. self.initialize_plugins: bool = config.initialize_plugins
  18. def add_to_env(self, key: str, value: str):
  19. self._env[key] = value
  20. # Note: json.dumps gives us nice escaping for free
  21. self.execute(f'export {key}={json.dumps(value)}')
  22. @abstractmethod
  23. def execute(
  24. self, cmd: str, stream: bool = False, timeout: int | None = None
  25. ) -> tuple[int, str | CancellableStream]:
  26. pass
  27. @abstractmethod
  28. def close(self):
  29. pass
  30. @abstractmethod
  31. def copy_to(self, host_src: str, sandbox_dest: str, recursive: bool = False):
  32. pass
  33. @abstractmethod
  34. def get_working_directory(self):
  35. pass