sandbox.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os
  2. from abc import ABC, abstractmethod
  3. from typing import Dict, Tuple
  4. from opendevin.runtime.docker.process import Process
  5. from opendevin.runtime.plugins.mixin import PluginMixin
  6. class Sandbox(ABC, PluginMixin):
  7. background_commands: Dict[int, Process] = {}
  8. _env: Dict[str, str] = {}
  9. def __init__(self, **kwargs):
  10. for key in os.environ:
  11. if key.startswith('SANDBOX_ENV_'):
  12. sandbox_key = key.removeprefix('SANDBOX_ENV_')
  13. self.add_to_env(sandbox_key, os.environ[key])
  14. def add_to_env(self, key: str, value: str):
  15. self._env[key] = value
  16. @abstractmethod
  17. def execute(self, cmd: str) -> Tuple[int, str]:
  18. pass
  19. @abstractmethod
  20. def execute_in_background(self, cmd: str) -> Process:
  21. pass
  22. @abstractmethod
  23. def kill_background(self, id: int) -> Process:
  24. pass
  25. @abstractmethod
  26. def read_logs(self, id: int) -> str:
  27. pass
  28. @abstractmethod
  29. def close(self):
  30. pass
  31. @abstractmethod
  32. def copy_to(self, host_src: str, sandbox_dest: str, recursive: bool = False):
  33. pass
  34. @abstractmethod
  35. def get_working_directory(self):
  36. pass