Skip to content

Commit

Permalink
feat(backend): Add container backend (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
kakkoyun authored May 23, 2024
1 parent ad82211 commit 2051e12
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dev = [
"virtualenv" = "isolate.backends.virtualenv:VirtualPythonEnvironment"
"conda" = "isolate.backends.conda:CondaEnvironment"
"local" = "isolate.backends.local:LocalPythonEnvironment"
"container" = "isolate.backends.container:ContainerizedPythonEnvironment"
"isolate-server" = "isolate.backends.remote:IsolateServer"
"pyenv" = "isolate.backends.pyenv:PyenvEnvironment"

Expand Down
49 changes: 49 additions & 0 deletions src/isolate/backends/container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import annotations

import sys
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, ClassVar

from isolate.backends import BaseEnvironment
from isolate.backends.common import sha256_digest_of
from isolate.backends.settings import DEFAULT_SETTINGS, IsolateSettings
from isolate.connections import PythonIPC


@dataclass
class ContainerizedPythonEnvironment(BaseEnvironment[Path]):
BACKEND_NAME: ClassVar[str] = "container"

image: dict[str, Any] = field(default_factory=dict)
python_version: str | None = None
tags: list[str] = field(default_factory=list)

@classmethod
def from_config(
cls,
config: dict[str, Any],
settings: IsolateSettings = DEFAULT_SETTINGS,
) -> BaseEnvironment:
environment = cls(**config)
environment.apply_settings(settings)
return environment

@property
def key(self) -> str:
# dockerfile_str is always there, but the validation is handled by the
# controller.
dockerfile_str = self.image.get("dockerfile_str", "")
return sha256_digest_of(dockerfile_str, *sorted(self.tags))

def create(self, *, force: bool = False) -> Path:
return Path(sys.exec_prefix)

def destroy(self, connection_key: Path) -> None:
raise NotImplementedError("ContainerizedPythonEnvironment cannot be destroyed")

def exists(self) -> bool:
return True

def open_connection(self, connection_key: Path) -> PythonIPC:
return PythonIPC(self, connection_key)

0 comments on commit 2051e12

Please sign in to comment.