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 command_lines.docker_command import docker_subcommand
from command_lines.shell_command import launched, 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(), "mutate-changes": 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"})
BUILDS = (["build"], ["buildx", "build"], ["compose", "build"])
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."""
launch = launched(words, {"docker", *MUTATORS})
if launch is None or "run-heavy" in launch.launchers or LIGHT_FLAGS & set(words):
return False
if launch.tool == "docker":
subcommand = docker_subcommand(launch.arguments)
return subcommand in BUILDS or (subcommand[-1:] in (["run"], ["exec"]) and mutation_run([tool_name(word) for word in launch.arguments]))
return mutation_run([launch.tool, *(tool_name(word) for word in launch.arguments)])
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))