Skip to content

Commit

Permalink
fix ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
xingyaoww committed Apr 7, 2024
1 parent 81bec95 commit 39ca2e7
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions opendevin/sandbox/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from typing import Dict, List, Tuple

import docker
import concurrent.futures

from opendevin import config
from opendevin.logging import opendevin_logger as logger
Expand Down Expand Up @@ -128,7 +127,7 @@ def __init__(
else:
self.container_image = container_image

self.container_name = f"sandbox-{self.instance_id}"
self.container_name = f'sandbox-{self.instance_id}'

if not self.is_container_running():
self.restart_docker_container()
Expand All @@ -137,36 +136,38 @@ def __init__(
self.start_ssh_session()
else:
# TODO: implement ssh into root
raise NotImplementedError('Running as root is not supported at the moment.')
raise NotImplementedError(
'Running as root is not supported at the moment.')
atexit.register(self.cleanup)

def setup_devin_user(self):
exit_code, logs = self.container.exec_run(
["/bin/bash", "-c", f"useradd -rm -d /home/opendevin -s /bin/bash -g root -G sudo -u {USER_ID} opendevin"],
['/bin/bash', '-c',
f'useradd -rm -d /home/opendevin -s /bin/bash -g root -G sudo -u {USER_ID} opendevin'],
workdir='/workspace',
)
exit_code, logs = self.container.exec_run(
["/bin/bash", "-c", "echo 'opendevin:opendevin' | chpasswd"],
['/bin/bash', '-c', "echo 'opendevin:opendevin' | chpasswd"],
workdir='/workspace',
)
exit_code, logs = self.container.exec_run(
["/bin/bash", "-c", "echo 'opendevin-sandbox' > /etc/hostname"],
['/bin/bash', '-c', "echo 'opendevin-sandbox' > /etc/hostname"],
workdir='/workspace',
)

def start_ssh_session(self):
# start ssh session at the background
self.ssh = pxssh.pxssh()
hostname = "localhost"
username = "opendevin"
password = "opendevin"
hostname = 'localhost'
username = 'opendevin'
password = 'opendevin'
self.ssh.login(hostname, username, password, port=2222)

# Fix: https://github.com/pexpect/pexpect/issues/669
self.ssh.sendline("bind 'set enable-bracketed-paste off'")
self.ssh.prompt()
# cd to workspace
self.ssh.sendline(f"cd /workspace")
self.ssh.sendline('cd /workspace')
self.ssh.prompt()

def get_exec_cmd(self, cmd: str) -> List[str]:
Expand All @@ -191,15 +192,17 @@ def execute(self, cmd: str) -> Tuple[int, str]:
# send a SIGINT to the process
self.ssh.sendintr()
self.ssh.prompt()
command_output = self.ssh.before.decode('utf-8').lstrip(cmd).strip()
command_output = self.ssh.before.decode(
'utf-8').lstrip(cmd).strip()
return -1, f'Command: "{cmd}" timed out. Sending SIGINT to the process: {command_output}'
command_output = self.ssh.before.decode('utf-8').lstrip(cmd).strip()

# get the exit code
self.ssh.sendline('echo $?')
self.ssh.prompt()
exit_code = self.ssh.before.decode('utf-8')
exit_code = int(exit_code.lstrip('echo $?').strip()) # remove the echo $? itself
# remove the echo $? itself
exit_code = int(exit_code.lstrip('echo $?').strip())
return exit_code, command_output

def execute_in_background(self, cmd: str) -> BackgroundCommand:
Expand Down Expand Up @@ -230,7 +233,7 @@ def kill_background(self, id: int) -> BackgroundCommand:
bg_cmd = self.background_commands[id]
if bg_cmd.pid is not None:
self.container.exec_run(
f"kill -9 {bg_cmd.pid}", workdir='/workspace')
f'kill -9 {bg_cmd.pid}', workdir='/workspace')
bg_cmd.result.output.close()
self.background_commands.pop(id)
return bg_cmd
Expand Down

0 comments on commit 39ca2e7

Please sign in to comment.