Skip to content

Commit

Permalink
update dependencies, fix mypy error
Browse files Browse the repository at this point in the history
  • Loading branch information
doodledood committed Nov 12, 2023
1 parent bb5f3b2 commit 08602b4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 16 deletions.
4 changes: 2 additions & 2 deletions chatflock/code/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from abc import ABC, abstractmethod
from typing import List, Optional
from typing import Optional, Sequence


class CodeExecutor(ABC):
@abstractmethod
def execute(self, code: str, dependencies: Optional[List[str]] = None) -> str:
def execute(self, code: str, dependencies: Optional[Sequence[str]] = None) -> str:
pass
25 changes: 14 additions & 11 deletions chatflock/code/docker.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import tempfile
from typing import Optional, Set
from typing import Optional, Set, Sequence

import docker
from docker.errors import ContainerError
from docker.models.images import Image
from halo import Halo

from .base import CodeExecutor
Expand All @@ -15,13 +16,13 @@ def __init__(self, client: Optional[docker.DockerClient] = None,
base_image: str = 'python:3.11-slim',
default_dependencies: Optional[Set[str]] = None,
spinner: Optional[Halo] = None):
self.client = client or docker.from_env()
self.client = client or docker.from_env() # type: ignore
self.image_tag = image_tag
self.base_image = base_image
self.default_dependencies = default_dependencies or {'requests', 'pytest'}
self.spinner = spinner

def create_dockerfile(self, python_code: str, dependencies: Optional[Set[str]] = None):
def create_dockerfile(self, python_code: str, dependencies: Optional[Set[str]] = None) -> str:
run_commands = [f'RUN pip install {package} --trusted-host pypi.org --trusted-host files.pythonhosted.org' for
package in dependencies or []]
run_commands_str = '\n'.join(run_commands)
Expand All @@ -39,7 +40,7 @@ def create_dockerfile(self, python_code: str, dependencies: Optional[Set[str]] =

return dockerfile

def build_image_with_code(self, python_code: str, dependencies: Optional[Set[str]] = None):
def build_image_with_code(self, python_code: str, dependencies: Optional[Set[str]] = None) -> Image:
spinner_text = None
if self.spinner is not None:
spinner_text = self.spinner.text
Expand Down Expand Up @@ -70,10 +71,10 @@ def build_image_with_code(self, python_code: str, dependencies: Optional[Set[str

return image

def execute(self, code: str, dependencies: Optional[Set[str]] = None) -> str:
def execute(self, code: str, dependencies: Optional[Sequence[str]] = None) -> str:
try:
# Ensure the image is built before execution
self.build_image_with_code(code, dependencies=dependencies or self.default_dependencies)
self.build_image_with_code(code, dependencies=set(dependencies or self.default_dependencies))
except Exception as e:
return f'Failed to build Docker image (did not run code yet): {e}'

Expand All @@ -90,9 +91,11 @@ def execute(self, code: str, dependencies: Optional[Set[str]] = None) -> str:
stderr=True,
detach=False
)
return container.decode('utf-8')
res: str = container.decode('utf-8')
except ContainerError as e:
return e.stderr.decode('utf-8')
finally:
if self.spinner is not None:
self.spinner.stop_and_persist(symbol='🐳', text='Code finished executing.')
res = e.stderr.decode('utf-8')

if self.spinner is not None:
self.spinner.stop_and_persist(symbol='🐳', text='Code finished executing.')

return res
4 changes: 2 additions & 2 deletions chatflock/code/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import traceback
import subprocess
from typing import Optional, List
from typing import Optional, Sequence

from halo import Halo

Expand All @@ -13,7 +13,7 @@ class LocalCodeExecutor(CodeExecutor):
def __init__(self, spinner: Optional[Halo] = None):
self.spinner = spinner

def execute(self, code: str, dependencies: Optional[List[str]] = None) -> str:
def execute(self, code: str, dependencies: Optional[Sequence[str]] = None) -> str:
captured_output = io.StringIO()
saved_stdout = sys.stdout
sys.stdout = captured_output
Expand Down
62 changes: 61 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pydantic = "^2.4.2"
beautifulsoup4 = "^4.12.2"
tenacity = "^8.2.3"
questionary = "^2.0.1"
docker = "^6.1.3"
[tool.poetry.dev-dependencies]
bandit = "^1.7.1"
black = { version = "^23.11.0", allow-prereleases = true }
Expand Down

0 comments on commit 08602b4

Please sign in to comment.