Files
claude-config/bin/rule_hook/heavy_command.py
T

25 lines
1.6 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."""
from rule_hook.docker_command import docker_arguments, docker_subcommand, LAUNCHERS, tool_name
# Each mutation tool, and the words that make its call a mutation run when one of them follows it within two words; where the set is empty, 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"})
def heavy(words: list[str]) -> bool:
"""Whether the simple command, run directly or through a launcher, is a docker build or a mutation run, or runs one in a container, and is not under 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
docker = docker_arguments(words)
if docker is not None:
subcommand = docker_subcommand(docker)
return subcommand in (["build"], ["buildx", "build"], ["compose", "build"]) or (subcommand[-1:] in (["run"], ["exec"]) and mutation_run(names[len(words) - len(docker):]))
# Past a launcher, the mutation tool may follow the launcher's own options, as in uvx --with pytest mutmut run.
return (names[0] in LAUNCHERS 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))