54 lines
3.0 KiB
Python
54 lines
3.0 KiB
Python
"""Whether a simple command is a heavy one, for the `run-heavy` rule: a docker build, or a run of a mutation tool."""
|
|
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", "time", "uvx", "yarn"})
|
|
# Each mutation tool, and the words that make its call a mutation run when one of them follows it within two words; with no words, every call is one.
|
|
MUTATORS = {"cargo": {"mutants"}, "infection": set(), "mutmut": {"run"}, "stryker": {"run"}}
|
|
# Flags with which a tool only prints its help, version or list, so a call that carries one is light. Not -h, which docker run reads as the container's hostname.
|
|
LIGHT_FLAGS = frozenset({"--help", "--list", "--version"})
|
|
# The options of docker and docker compose that take a value, so the value is not read as a subcommand.
|
|
DOCKER_VALUED = frozenset({"--ansi", "--config", "--context", "--env-file", "--file", "--host", "--log-level", "--parallel", "--profile", "--progress", "--project-directory", "--project-name", "-c", "-f", "-H", "-l", "-p"})
|
|
|
|
|
|
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 heavy(words: list[str]) -> bool:
|
|
"""Whether the simple command runs a docker build, or a mutation run directly, through a launcher or in a container, outside run-heavy."""
|
|
names = [tool_name(word) for word in words]
|
|
if not names or "run-heavy" in names or LIGHT_FLAGS & set(words):
|
|
return False
|
|
# Past a launcher, the tool may follow the launcher's own options and assignments, as in nice -n 19 docker build or uvx --with pytest mutmut run; without one, the command itself is the tool.
|
|
launched = names[0] in LAUNCHERS
|
|
reachable = names if launched else names[:1]
|
|
docker = reachable.index("docker") if "docker" in reachable else None
|
|
if docker is not None:
|
|
subcommand = docker_subcommand(words[docker + 1:])
|
|
return subcommand in (["build"], ["buildx", "build"], ["compose", "build"]) or (subcommand[-1:] in (["run"], ["exec"]) and mutation_run(names[docker + 1:]))
|
|
return (launched or names[0] in MUTATORS) and mutation_run(names)
|
|
|
|
|
|
def mutation_run(names: list[str]) -> bool:
|
|
return any(name in MUTATORS and (not MUTATORS[name] or MUTATORS[name] & set(names[i + 1:i + 3])) for i, name in enumerate(names))
|
|
|
|
|
|
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") or len(positional) == 2:
|
|
break
|
|
return positional
|