This commit is contained in:
2024-12-04 17:19:46 +01:00
parent af390dd779
commit da8583ab28
36 changed files with 712 additions and 69 deletions

View File

@@ -19,6 +19,7 @@ class BackgroundEntry(Entry):
def __init__(
self,
id: str,
work_dir: str,
script: str,
):
"""
@@ -26,9 +27,11 @@ class BackgroundEntry(Entry):
Args:
id: Unique identifier for this entry
work_dir: Working directory for the process
script: The script/command to execute
"""
super().__init__(id)
self.work_dir = work_dir
self.script = script
self._process: Optional[subprocess.Popen] = None
self.stdout = ""
@@ -81,6 +84,7 @@ class BackgroundEntry(Entry):
if self._process is None and self.exit_code is None:
self._process = subprocess.Popen(
self.script,
cwd=self.work_dir,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
@@ -155,6 +159,7 @@ class BackgroundEntry(Entry):
return {
"type": "background",
"id": self.id,
"work_dir": str(self.work_dir),
"script": self.script,
"stdout": self.stdout,
"stderr": self.stderr,

View File

@@ -1,4 +1,4 @@
from typing import Optional
from pathlib import Path
from . import Entry
from ..io_buffer import IOBuffer
@@ -11,12 +11,12 @@ from .single_entry import SingleEntry
from .write_entry import WriteEntry
class EntryFactory:
def create_entry(data: dict, io_buffer: IOBuffer) -> Entry:
def create_entry(data: dict, work_dir: Path, io_buffer: IOBuffer) -> Entry:
entry_type = data.get("type")
entry_id = data.get("id")
if entry_type == "background":
return BackgroundEntry(entry_id, data["script"])
return BackgroundEntry(entry_id, work_dir, data["script"])
elif entry_type == "read_stdin":
return ReadEntry(entry_id, io_buffer)
@@ -27,6 +27,7 @@ class EntryFactory:
elif entry_type == "repeat":
return RepeatEntry(
entry_id,
work_dir,
data["script"],
data.get("timeout"),
data.get("limit")
@@ -35,6 +36,7 @@ class EntryFactory:
elif entry_type == "single":
return SingleEntry(
entry_id,
work_dir,
data["script"],
data.get("timeout"),
data.get("limit")
@@ -51,6 +53,8 @@ class EntryFactory:
entry.id = data["id"]
if isinstance(entry, SingleEntry):
if "work_dir" in data:
entry.work_dir = Path(data["work_dir"])
if "script" in data:
entry.script = data["script"]
if "timeout" in data:
@@ -69,6 +73,8 @@ class EntryFactory:
entry.timed_out = bool(data["timed_out"])
if isinstance(entry, RepeatEntry):
if "work_dir" in data:
entry.work_dir = Path(data["work_dir"])
if "script" in data:
entry.script = data["script"]
if "timeout" in data:
@@ -87,6 +93,8 @@ class EntryFactory:
entry.timed_out = bool(data["timed_out"])
elif isinstance(entry, BackgroundEntry):
if "work_dir" in data:
entry.work_dir = Path(data["work_dir"])
if "script" in data:
entry.script = data["script"]
if "stdout" in data:

View File

@@ -1,3 +1,4 @@
from pathlib import Path
import subprocess
import xml.etree.ElementTree as ET
from typing import Optional
@@ -15,6 +16,7 @@ class RepeatEntry(Entry):
def __init__(
self,
id: str,
work_dir: Path,
script: str,
timeout: Optional[float] = None,
limit: Optional[int] = None,
@@ -29,6 +31,7 @@ class RepeatEntry(Entry):
limit: Maximum number of characters to capture from stdout/stderr
"""
super().__init__(id)
self.work_dir = work_dir
self.script = script
self.timeout = timeout
self.limit = limit
@@ -45,6 +48,7 @@ class RepeatEntry(Entry):
try:
process = subprocess.run(
self.script,
cwd=self.work_dir,
timeout=(self.timeout or self.default_timeout),
shell=True,
capture_output=True,
@@ -95,6 +99,7 @@ class RepeatEntry(Entry):
return {
"type": "repeat",
"id": self.id,
"work_dir": str(self.work_dir),
"script": self.script,
"timeout": self.timeout,
"limit": self.limit,

View File

@@ -1,3 +1,4 @@
from pathlib import Path
import subprocess
import xml.etree.ElementTree as ET
from typing import Optional
@@ -15,6 +16,7 @@ class SingleEntry(Entry):
def __init__(
self,
id: str,
work_dir: Path,
script: str,
timeout: Optional[float] = None,
limit: Optional[int] = None,
@@ -29,6 +31,7 @@ class SingleEntry(Entry):
limit: Maximum number of characters to capture from stdout/stderr
"""
super().__init__(id)
self.work_dir = work_dir
self.script = script
self.timeout = timeout
self.limit = limit
@@ -58,6 +61,7 @@ class SingleEntry(Entry):
try:
process = subprocess.run(
self.script,
cwd=self.work_dir,
timeout=(self.timeout or self.default_timeout),
shell=True,
capture_output=True,
@@ -106,6 +110,7 @@ class SingleEntry(Entry):
return {
"type": "single",
"id": self.id,
"work_dir": str(self.work_dir),
"script": self.script,
"timeout": self.timeout,
"limit": self.limit,