24 lines
948 B
Python
24 lines
948 B
Python
"""The docker a heavy command finds first on its PATH: it gives a container the command makes the limits it lacks, the slot's memory share in RUN_HEAVY_MEMORY, then runs the real docker."""
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import typing
|
|
|
|
from docker_command import capped
|
|
|
|
FOLDER = pathlib.Path(__file__).resolve().parent / "path_entry"
|
|
SHARE = "RUN_HEAVY_MEMORY"
|
|
NOT_FOUND = 127
|
|
|
|
|
|
def main(argv: list[str], env: typing.Mapping[str, str]) -> int:
|
|
"""Replaces this process with the real docker; returns only where none is found."""
|
|
path = os.pathsep.join(entry for entry in env.get("PATH", "").split(os.pathsep) if pathlib.Path(entry or ".").resolve() != FOLDER)
|
|
docker = shutil.which("docker", path=path)
|
|
if docker is None:
|
|
print("run-heavy: docker: command not found", file=sys.stderr)
|
|
return NOT_FOUND
|
|
share = env.get(SHARE)
|
|
os.execve(docker, [docker, *(capped(argv, share) if share else argv)], {**env, "PATH": path})
|