made docker client local to handlers and lazy

loaded so that we don't have to start docker if we don't want it
This commit is contained in:
Karma Riuk
2025-05-18 08:22:32 +02:00
parent e310383721
commit c8b0557498
2 changed files with 8 additions and 11 deletions

View File

@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
import os, re, docker, signal, javalang
import os, re, docker, signal, javalang, time
from bs4 import BeautifulSoup
from typing import Iterable, Tuple, Iterator
from typing import Iterable, Optional, Tuple, Iterator
import xml.etree.ElementTree as ET
from javalang.tree import PackageDeclaration
import tarfile
@ -16,17 +16,18 @@ GROUP_ID = os.getgid()
class BuildHandler(ABC):
DOCKER_CLIENT: Optional[docker.DockerClient] = None
def __init__(self, repo_path: str, build_file: str, updates: dict) -> None:
super().__init__()
self.path: str = repo_path
self.build_file: str = build_file
self.updates = updates
def set_client(self, client: docker.DockerClient):
self.client = client
def __enter__(self):
self.container = self.client.containers.run(
if BuildHandler.DOCKER_CLIENT is None:
BuildHandler.DOCKER_CLIENT = docker.from_env()
self.container = BuildHandler.DOCKER_CLIENT.containers.run(
image=self.container_name(),
command="tail -f /dev/null", # to keep the container alive
volumes={os.path.abspath(self.path): {"bind": "/repo", "mode": "rw"}},

View File

@ -1,5 +1,4 @@
import os
import sys, docker
import sys
from utils.handlers import get_build_handler
from .paths import get_project_path
from sacrebleu import sentence_bleu as bleu
@ -11,8 +10,6 @@ REFERENCE_MAP = Dataset.from_json(
ARCHIVES_ROOT = str(get_project_path('../data/archives'))
DOCKER_CLIENT = docker.from_env()
def evaluate_comments(answers: dict[str, str], percent_cb):
total = len(answers)
@ -57,7 +54,6 @@ def evaluate_refinement(answers: dict[str, dict[str, str]], percent_cb):
build_handler = get_build_handler(
ARCHIVES_ROOT, entry.metadata.archive_name(ArchiveState.MERGED)
)
build_handler.set_client(DOCKER_CLIENT)
current_progress += 1
percent_cb(current_progress / total_number_of_steps * 100)
except Exception as e: