Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): Add container backend #135

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
efiop marked this conversation as resolved.
Show resolved Hide resolved
"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)
Loading