54 lines
2.9 KiB
Python
54 lines
2.9 KiB
Python
"""What a simple command asks of docker, for rule-hook's rules: its subcommand, and whether a container it starts has a memory limit."""
|
|
import posixpath
|
|
import re
|
|
|
|
# Launchers that run the tool named later in their words, such as uvx --with pytest mutmut run.
|
|
LAUNCHERS = frozenset({"bunx", "env", "nice", "npx", "pipx", "pnpm", "run-heavy", "time", "uvx", "yarn"})
|
|
# The options of docker, docker buildx and docker compose that take a value, so the value is not read as a subcommand.
|
|
DOCKER_VALUED = frozenset({"--ansi", "--builder", "--config", "--context", "--env-file", "--file", "--host", "--log-level", "--parallel", "--profile", "--progress", "--project-directory", "--project-name", "--tlscacert", "--tlscert", "--tlskey", "-c", "-f", "-H", "-l", "-p"})
|
|
# The subcommands that start a container, which takes the memory limit docker run's -m sets.
|
|
STARTS_CONTAINER = (["run"], ["create"], ["container", "run"], ["container", "create"])
|
|
# -m4g is -m with its value attached; --memory-swap alone sets no limit.
|
|
MEMORY_LIMIT = re.compile(r"-m.*|--memory(=.*)?")
|
|
|
|
|
|
def tool_name(word: str) -> str:
|
|
"""The tool a word names, such as mutmut for `mutmut>=3,<4` or `.venv/bin/mutmut`; empty for a word that names none, such as a file name."""
|
|
named = re.fullmatch(r"([A-Za-z_-]+)(?:[<>=!~@].*)?", posixpath.basename(word))
|
|
return named.group(1) if named else ""
|
|
|
|
|
|
def docker_arguments(words: list[str]) -> list[str] | None:
|
|
"""The words after docker where the simple command runs it directly or through a launcher, as in nice -n 19 docker build .; None where it runs no docker."""
|
|
names = [tool_name(word) for word in words]
|
|
# Past a launcher, docker may follow the launcher's own options and assignments; without one, the command itself is the tool.
|
|
reachable = names if names[:1] and names[0] in LAUNCHERS else names[:1]
|
|
return words[reachable.index("docker") + 1:] if "docker" in reachable else None
|
|
|
|
|
|
def docker_subcommand(words: list[str]) -> list[str]:
|
|
"""The subcommand docker's words name, past its options: such as ["build"], ["compose", "run"] or ["buildx", "build"]."""
|
|
positional: list[str] = []
|
|
skip = False
|
|
for word in words:
|
|
if skip:
|
|
skip = False
|
|
elif word.startswith("-"):
|
|
skip = word in DOCKER_VALUED
|
|
else:
|
|
positional.append(word)
|
|
if positional[0] not in ("buildx", "compose", "container") or len(positional) == 2:
|
|
break
|
|
return positional
|
|
|
|
|
|
def unlimited_container(words: list[str]) -> bool:
|
|
"""Whether the simple command starts a container through docker run or docker create, directly or through a launcher, and sets no memory limit.
|
|
|
|
A --memory among the container's own arguments, past its image, reads as the limit too: telling it apart needs every value-taking option of docker run.
|
|
"""
|
|
arguments = docker_arguments(words)
|
|
if arguments is None or "--help" in arguments:
|
|
return False
|
|
return docker_subcommand(arguments) in STARTS_CONTAINER and not any(MEMORY_LIMIT.fullmatch(word) for word in arguments)
|