"""A shell command's simple commands, each as its words and the directory it runs in, and which tool a simple command runs through which launchers.""" import dataclasses import posixpath import re import shlex import typing # Launchers that run the tool named later in their words, such as uvx --with pytest mutmut run, uv run mutmut run or sudo docker build. LAUNCHERS = frozenset({"bunx", "doas", "env", "nice", "npx", "pipx", "pnpm", "poetry", "run-heavy", "sudo", "time", "timeout", "uv", "uvx", "yarn"}) HEREDOC = re.compile(r"<<-?[ \t]*([\"']?)([A-Za-z_]\w*)\1[^\n]*\n.*?^[ \t]*\2[ \t]*$", re.DOTALL | re.MULTILINE) ASSIGNMENT = re.compile(r"[A-Za-z_][A-Za-z0-9_]*=") OPERATOR = set("\n();&|<>") # A token that is only a redirect: the command's words go on after its target. `&<` is not one: it is a `&` that ends the command, then a `<`. REDIRECT = re.compile(r"&>>?|[<>]+[&|]?") # A quoted string or an escape is taken whole, so a # inside one starts no comment; a comment starts a word. PIECE = re.compile(r"""'[^']*'|"(?:\\.|[^"\\])*"|\\.|(? list[Simple]: """Each simple command, without leading variable assignments or redirect targets, in the directory `cd` left it in.""" directory = cwd found = [] for words in segments(command): if words[0] == "cd" and len(words) == 2: directory = resolved(directory, words[1], home) found.append(Simple(words, directory)) return found def segments(command: str) -> list[list[str]]: """The command's simple commands as lists of words, split at ; & | ( ) and line breaks, without redirect targets. Shell forms it does not parse: - a quoted string is one word, so `bash -c "git commit"` and `echo "$(git commit)"` hide the inner command; - a backtick substitution stays inside its words, while `$(git commit)` outside quotes splits into the `$` and a command of its own; - a keyword or a brace opens its command's words, so `then git commit` reads as the command `then`; - `<(...)` and `>(...)` end the command, and the first word inside them is dropped as a redirect target, so `tee >(git commit)` hides the commit; - a file descriptor before a redirect, as in `2>&1`, stays a word; - a heredoc's body is dropped; - variables, aliases and functions are not expanded; - a line with an unbalanced quote doesn't yield anything.""" text = uncommented(HEREDOC.sub(lambda heredoc: heredoc.group(0).split("\n", 1)[0], command)) tokens = lexed(text) if tokens is None: # One unbalanced quote should not hide the other lines. tokens = [token for line in text.splitlines() for token in (lexed(line) or []) + ["\n"]] found: list[list[str]] = [[]] target = False for token in tokens: if target: target = False elif REDIRECT.fullmatch(token): # A redirect's target is not an operand. target = True elif set(token) <= OPERATOR and token: # A separator, or a redirect glued to one as in `;>f`, `&" in token found.append([]) elif found[-1] or not ASSIGNMENT.match(token): found[-1].append(token) return [words for words in found if words] def lexed(text: str) -> list[str] | None: lexer = shlex.shlex(text, posix=True, punctuation_chars="();<>|&\n") lexer.commenters = "" lexer.whitespace = " \t\r" lexer.whitespace_split = True try: return list(lexer) except ValueError: return None def uncommented(text: str) -> str: """The text without its comments; shlex's own would also swallow the line break that ends one, joining two commands.""" return PIECE.sub(lambda piece: "" if piece.group(0).startswith("#") else piece.group(0), text) def resolved(base: str, path: str, home: str) -> str: return posixpath.normpath(posixpath.join(base, home + path[1:] if path == "~" or path.startswith("~/") else path)) def home_word(token: str) -> str: """The word with a leading $HOME or ${HOME} spelt as ~; the lexer has joined a quoted "$HOME" to the rest of its word.""" return re.sub(r"^\$(HOME|\{HOME\})(?=/|$)", "~", token) 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 launched(words: list[str], tools: typing.Collection[str]) -> Launch | None: """Which of the given tools the simple command runs, directly or through launchers, taking the first one named; None where it doesn't run any of them.""" names = [tool_name(word) for word in words] # Past a launcher, the tool may follow the launcher's own options and assignments, as in nice -n 19 docker build; without one, the command itself is the tool. reachable = names if names[:1] and names[0] in LAUNCHERS else names[:1] at = next((i for i, name in enumerate(reachable) if name in tools), None) if at is None: return None return Launch(frozenset(LAUNCHERS.intersection(names[:at])), names[at], words[at], words[:at], words[at + 1:])