local_box.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import atexit
  2. import os
  3. import subprocess
  4. import sys
  5. from opendevin.core.config import config
  6. from opendevin.core.logger import opendevin_logger as logger
  7. from opendevin.core.schema import CancellableStream
  8. from opendevin.runtime.sandbox import Sandbox
  9. # ===============================================================================
  10. # ** WARNING **
  11. #
  12. # This sandbox should only be used when OpenDevin is running inside a container
  13. #
  14. # Sandboxes are generally isolated so that they cannot affect the host machine.
  15. # This Sandbox implementation does not provide isolation, and can inadvertently
  16. # run dangerous commands on the host machine, potentially rendering the host
  17. # machine unusable.
  18. #
  19. # This sandbox is meant for use with OpenDevin Quickstart
  20. #
  21. # DO NOT USE THIS SANDBOX IN A PRODUCTION ENVIRONMENT
  22. # ===============================================================================
  23. class LocalBox(Sandbox):
  24. def __init__(self, timeout: int = config.sandbox.timeout):
  25. os.makedirs(config.workspace_base, exist_ok=True)
  26. self.timeout = timeout
  27. atexit.register(self.cleanup)
  28. super().__init__()
  29. def execute(
  30. self, cmd: str, stream: bool = False, timeout: int | None = None
  31. ) -> tuple[int, str | CancellableStream]:
  32. timeout = timeout if timeout is not None else self.timeout
  33. try:
  34. completed_process = subprocess.run(
  35. cmd,
  36. shell=True,
  37. text=True,
  38. capture_output=True,
  39. timeout=timeout,
  40. cwd=config.workspace_base,
  41. env=self._env,
  42. )
  43. return completed_process.returncode, completed_process.stdout.strip()
  44. except subprocess.TimeoutExpired:
  45. return -1, 'Command timed out'
  46. def copy_to(self, host_src: str, sandbox_dest: str, recursive: bool = False):
  47. # mkdir -p sandbox_dest if it doesn't exist
  48. res = subprocess.run(
  49. f'mkdir -p {sandbox_dest}',
  50. shell=True,
  51. text=True,
  52. cwd=config.workspace_base,
  53. env=self._env,
  54. )
  55. if res.returncode != 0:
  56. raise RuntimeError(f'Failed to create directory {sandbox_dest} in sandbox')
  57. if recursive:
  58. res = subprocess.run(
  59. f'cp -r {host_src} {sandbox_dest}',
  60. shell=True,
  61. text=True,
  62. cwd=config.workspace_base,
  63. env=self._env,
  64. )
  65. if res.returncode != 0:
  66. raise RuntimeError(
  67. f'Failed to copy {host_src} to {sandbox_dest} in sandbox'
  68. )
  69. else:
  70. res = subprocess.run(
  71. f'cp {host_src} {sandbox_dest}',
  72. shell=True,
  73. text=True,
  74. cwd=config.workspace_base,
  75. env=self._env,
  76. )
  77. if res.returncode != 0:
  78. raise RuntimeError(
  79. f'Failed to copy {host_src} to {sandbox_dest} in sandbox'
  80. )
  81. def close(self):
  82. pass
  83. def cleanup(self):
  84. self.close()
  85. def get_working_directory(self):
  86. return config.workspace_base
  87. if __name__ == '__main__':
  88. local_box = LocalBox()
  89. sys.stdout.flush()
  90. try:
  91. while True:
  92. try:
  93. user_input = input('>>> ')
  94. except EOFError:
  95. logger.info('Exiting...')
  96. break
  97. if user_input.lower() == 'exit':
  98. logger.info('Exiting...')
  99. break
  100. exit_code, output = local_box.execute(user_input)
  101. logger.info('exit code: %d', exit_code)
  102. logger.info(output)
  103. sys.stdout.flush()
  104. except KeyboardInterrupt:
  105. logger.info('Exiting...')
  106. local_box.close()