MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _bash_commands

Function _bash_commands

graphify/cli.py:1363–1424  ·  view source on GitHub ↗

Yield ``(name, args, piped)`` for each command a Bash string executes. The command parsing behind _bash_invokes_search (#3121): heredoc bodies are dropped, quoted spans become one placeholder word ``Q``, and the string is split at shell operators and substitution boundaries. In each seg

(cmd_str: str)

Source from the content-addressed store, hash-verified

1361
1362
1363def _bash_commands(cmd_str: str):
1364 """Yield ``(name, args, piped)`` for each command a Bash string executes.
1365
1366 The command parsing behind _bash_invokes_search (#3121): heredoc bodies are
1367 dropped, quoted spans become one placeholder word ``Q``, and the string is
1368 split at shell operators and substitution boundaries. In each segment the
1369 executable is the first token after ``VAR=value`` prefixes and wrappers
1370 (sudo/xargs/env and their flags). ``name`` is its basename, lowercased, with
1371 any ``.exe`` removed; ``args`` are the tokens after it; ``piped`` says the
1372 segment reads a pipe. A word in prose, a quoted argument or a heredoc body is
1373 never yielded as a command.
1374 """
1375 text = cmd_str
1376 # Drop heredoc bodies: from the line after `<<WORD` through the line that
1377 # is exactly WORD. An unterminated heredoc drops to the end of the string.
1378 m = _HEREDOC_OPEN_RE.search(text)
1379 while m:
1380 nl_idx = text.find("\n", m.end())
1381 if nl_idx == -1:
1382 break
1383 term = re.compile(r"^\s*" + re.escape(m.group(2)) + r"\s*$", re.MULTILINE)
1384 t = term.search(text, nl_idx + 1)
1385 if t is None:
1386 # Unterminated heredoc: everything after the opener line is body.
1387 text = text[: nl_idx + 1]
1388 break
1389 text = text[: nl_idx + 1] + text[t.end():]
1390 m = _HEREDOC_OPEN_RE.search(text, nl_idx + 1)
1391 # Replace quoted spans with one placeholder word (backslash escapes inside
1392 # double quotes are irrelevant here - anything quoted is an argument, never
1393 # the executable, but a quoted sed/awk program is still an operand).
1394 text = re.sub(r"'[^']*'", "Q", text)
1395 text = re.sub(r'"[^"]*"', "Q", text)
1396 # Split into command segments at shell operators / substitution boundaries,
1397 # keeping each operator so a segment knows whether it reads a pipe. A pipe
1398 # carries over the empty segments of `|&` and of a pipeline continued on
1399 # the next line.
1400 parts = re.split(r"(\|\|?|[;&\n]|\$\(|`|\(|\)|\{|\})", text)
1401 piped = False
1402 for index in range(0, len(parts), 2):
1403 if index > 0 and parts[index - 1] in ("|", "||"):
1404 piped = parts[index - 1] == "|"
1405 tokens = parts[index].split()
1406 if not tokens:
1407 continue
1408 segment_piped, piped = piped, False
1409 i = 0
1410 while i < len(tokens):
1411 tok = tokens[i]
1412 if "=" in tok.split("/")[-1] and not tok.startswith(("-", "/")):
1413 i += 1 # VAR=value prefix
1414 continue
1415 name = tok.replace("\\", "/").rsplit("/", 1)[-1].lower()
1416 name = name[:-4] if name.endswith(".exe") else name
1417 if name in _COMMAND_WRAPPERS:
1418 i += 1
1419 # skip the wrapper's own flags (`xargs -0`, `env -i`)
1420 while i < len(tokens) and tokens[i].startswith("-"):

Callers 2

_bash_invokes_searchFunction · 0.85
_bash_read_targetFunction · 0.85

Calls 1

findMethod · 0.80

Tested by

no test coverage detected