Skip to content

Commit

Permalink
Remove styxdefs generics: Docker runner
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed May 9, 2024
1 parent 167c2dc commit 11eeba0
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/styx/runners/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def _docker_mount(host_path: str, container_path: str, readonly: bool) -> str:
return f"type=bind,source={host_path},target={container_path}{',readonly' if readonly else ''}"


class DockerExecution(Execution[pl.Path, pl.Path]):
class DockerExecution(Execution):
def __init__(self, metadata: Metadata, output_dir: pl.Path) -> None:
self.metadata = metadata
self.input_files: list[tuple[pl.Path, str]] = []
Expand All @@ -26,10 +26,11 @@ def __init__(self, metadata: Metadata, output_dir: pl.Path) -> None:
self.output_file_next_id = 0
self.output_dir = output_dir

def input_file(self, host_file: pl.Path) -> str:
local_file = f"/styx_input/{self.input_file_next_id}/{host_file.name}"
def input_file(self, host_file: pl.Path | str) -> str:
_host_file = pl.Path(host_file)
local_file = f"/styx_input/{self.input_file_next_id}/{_host_file.name}"
self.input_file_next_id += 1
self.input_files.append((host_file, local_file))
self.input_files.append((_host_file, local_file))
return local_file

def output_file(self, local_file: str, optional: bool = False) -> pl.Path:
Expand Down Expand Up @@ -83,17 +84,17 @@ def stderr_handler(line: str) -> None:
exhaust = partial(pool.submit, partial(deque, maxlen=0))
exhaust(stdout_handler(line[:-1]) for line in process.stdout) # type: ignore
exhaust(stderr_handler(line[:-1]) for line in process.stderr) # type: ignore
retcode = process.poll()
if retcode:
raise CalledProcessError(retcode, process.args)
return_code = process.poll()
if return_code:
raise CalledProcessError(return_code, process.args)


def _default_execution_output_dir(metadata: Metadata) -> pl.Path:
filesafe_name = re.sub(r"\W+", "_", metadata.name)
return pl.Path(f"output_{filesafe_name}")


class DockerRunner(Runner[pl.Path, pl.Path]):
class DockerRunner(Runner):
def __init__(self, execution_output_dir: Callable[[Metadata], pl.Path] | None = None) -> None:
"""Create a new DockerRunner.
Expand All @@ -106,6 +107,6 @@ def __init__(self, execution_output_dir: Callable[[Metadata], pl.Path] | None =
_default_execution_output_dir if execution_output_dir is None else execution_output_dir
)

def start_execution(self, metadata: Metadata) -> Execution[pl.Path, pl.Path]:
def start_execution(self, metadata: Metadata) -> Execution:
output_dir = self.execution_output_dir(metadata)
return DockerExecution(metadata, output_dir)

0 comments on commit 11eeba0

Please sign in to comment.