Answer principles round 1: 0 🔴, 4 🟡 (0 misread); exempt only a bare docker under run-heavy with no PATH of its own, drop the stale item citation, unit-test the slot share in MiB, place the limits by the subcommand's index; also a named Option, exit 126, clearer names and the launcher's import order
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""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."""
|
||||
"""The docker that run-heavy puts first on its command's PATH. It adds the memory limits a container lacks, from the slot's share, then runs the real docker."""
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
@@ -7,17 +7,29 @@ import typing
|
||||
|
||||
from docker_command import capped
|
||||
|
||||
FOLDER = pathlib.Path(__file__).resolve().parent / "path_entry"
|
||||
SHARE = "RUN_HEAVY_MEMORY"
|
||||
# The folder run-heavy puts first on its command's PATH, holding the docker that runs main.
|
||||
PATH_ENTRY = pathlib.Path(__file__).resolve().parent / "path_entry"
|
||||
# The environment variable that hands this docker the slot's memory share, in MiB.
|
||||
SHARE_VARIABLE = "RUN_HEAVY_MEMORY"
|
||||
CANNOT_RUN = 126
|
||||
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)
|
||||
"""Replaces this process with the real docker; returns only where none is found or it cannot run."""
|
||||
path = without_path_entry(env.get("PATH", ""))
|
||||
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})
|
||||
share = env.get(SHARE_VARIABLE)
|
||||
try:
|
||||
os.execve(docker, [docker, *(capped(argv, f"{share}m") if share else argv)], {**env, "PATH": path})
|
||||
except OSError as failure:
|
||||
print(f"run-heavy: cannot run {docker}: {failure.strerror}", file=sys.stderr)
|
||||
return CANNOT_RUN
|
||||
|
||||
|
||||
def without_path_entry(path: str) -> str:
|
||||
"""path less PATH_ENTRY, so the real docker is the one found."""
|
||||
return os.pathsep.join(entry for entry in path.split(os.pathsep) if pathlib.Path(entry or ".").resolve() != PATH_ENTRY)
|
||||
|
||||
Reference in New Issue
Block a user