Navegador

navegador / navegador / ingestion / bash.py
Source Blame History 263 lines
b45288f… lmata 1 """
b45288f… lmata 2 Bash/Shell script parser — extracts functions, top-level variables,
b45288f… lmata 3 source/. imports, and call edges from .sh/.bash files using tree-sitter.
b45288f… lmata 4 """
b45288f… lmata 5
b45288f… lmata 6 import logging
b45288f… lmata 7 from pathlib import Path
b45288f… lmata 8
b45288f… lmata 9 from navegador.graph.schema import EdgeType, NodeLabel
b45288f… lmata 10 from navegador.graph.store import GraphStore
b45288f… lmata 11 from navegador.ingestion.parser import LanguageParser
b45288f… lmata 12
b45288f… lmata 13 logger = logging.getLogger(__name__)
b45288f… lmata 14
b45288f… lmata 15
b45288f… lmata 16 def _get_bash_language():
b45288f… lmata 17 try:
b45288f… lmata 18 import tree_sitter_bash as tsbash # type: ignore[import]
b45288f… lmata 19 from tree_sitter import Language
b45288f… lmata 20
b45288f… lmata 21 return Language(tsbash.language())
b45288f… lmata 22 except ImportError as e:
b45288f… lmata 23 raise ImportError("Install tree-sitter-bash: pip install tree-sitter-bash") from e
b45288f… lmata 24
b45288f… lmata 25
b45288f… lmata 26 def _node_text(node, source: bytes) -> str:
b45288f… lmata 27 return source[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
b45288f… lmata 28
b45288f… lmata 29
b45288f… lmata 30 class BashParser(LanguageParser):
b45288f… lmata 31 """Parses Bash/Shell script files into the navegador graph."""
b45288f… lmata 32
b45288f… lmata 33 def __init__(self) -> None:
b45288f… lmata 34 from tree_sitter import Parser # type: ignore[import]
b45288f… lmata 35
b45288f… lmata 36 self._parser = Parser(_get_bash_language())
b45288f… lmata 37
b45288f… lmata 38 def parse_file(self, path: Path, repo_root: Path, store: GraphStore) -> dict[str, int]:
b45288f… lmata 39 source = path.read_bytes()
b45288f… lmata 40 tree = self._parser.parse(source)
b45288f… lmata 41 rel_path = str(path.relative_to(repo_root))
b45288f… lmata 42
b45288f… lmata 43 store.create_node(
b45288f… lmata 44 NodeLabel.File,
b45288f… lmata 45 {
b45288f… lmata 46 "name": path.name,
b45288f… lmata 47 "path": rel_path,
b45288f… lmata 48 "language": "bash",
b45288f… lmata 49 "line_count": source.count(b"\n"),
b45288f… lmata 50 },
b45288f… lmata 51 )
b45288f… lmata 52
b45288f… lmata 53 stats = {"functions": 0, "classes": 0, "edges": 0}
b45288f… lmata 54 self._walk(tree.root_node, source, rel_path, store, stats)
b45288f… lmata 55 return stats
b45288f… lmata 56
b45288f… lmata 57 # ── AST walker ────────────────────────────────────────────────────────────
b45288f… lmata 58
b45288f… lmata 59 def _walk(self, node, source: bytes, file_path: str, store: GraphStore, stats: dict) -> None:
b45288f… lmata 60 if node.type == "function_definition":
b45288f… lmata 61 self._handle_function(node, source, file_path, store, stats)
b45288f… lmata 62 return
b45288f… lmata 63 if node.type == "variable_assignment":
b45288f… lmata 64 self._handle_variable(node, source, file_path, store, stats)
b45288f… lmata 65 return
b45288f… lmata 66 if node.type == "command":
b45288f… lmata 67 self._handle_command(node, source, file_path, store, stats)
b45288f… lmata 68 return
b45288f… lmata 69 for child in node.children:
b45288f… lmata 70 self._walk(child, source, file_path, store, stats)
b45288f… lmata 71
b45288f… lmata 72 # ── Handlers ──────────────────────────────────────────────────────────────
b45288f… lmata 73
b45288f… lmata 74 def _handle_function(
b45288f… lmata 75 self,
b45288f… lmata 76 node,
b45288f… lmata 77 source: bytes,
b45288f… lmata 78 file_path: str,
b45288f… lmata 79 store: GraphStore,
b45288f… lmata 80 stats: dict,
b45288f… lmata 81 ) -> None:
b45288f… lmata 82 name_node = node.child_by_field_name("name")
b45288f… lmata 83 if not name_node:
b45288f… lmata 84 return
b45288f… lmata 85 name = _node_text(name_node, source)
b45288f… lmata 86
b45288f… lmata 87 store.create_node(
b45288f… lmata 88 NodeLabel.Function,
b45288f… lmata 89 {
b45288f… lmata 90 "name": name,
b45288f… lmata 91 "file_path": file_path,
b45288f… lmata 92 "line_start": node.start_point[0] + 1,
b45288f… lmata 93 "line_end": node.end_point[0] + 1,
b45288f… lmata 94 "docstring": "",
b45288f… lmata 95 "semantic_type": "shell_function",
b45288f… lmata 96 },
b45288f… lmata 97 )
b45288f… lmata 98
b45288f… lmata 99 store.create_edge(
b45288f… lmata 100 NodeLabel.File,
b45288f… lmata 101 {"path": file_path},
b45288f… lmata 102 EdgeType.CONTAINS,
b45288f… lmata 103 NodeLabel.Function,
b45288f… lmata 104 {"name": name, "file_path": file_path},
b45288f… lmata 105 )
b45288f… lmata 106 stats["functions"] += 1
b45288f… lmata 107 stats["edges"] += 1
b45288f… lmata 108
b45288f… lmata 109 self._extract_calls(node, source, file_path, name, store, stats)
b45288f… lmata 110
b45288f… lmata 111 def _handle_variable(
b45288f… lmata 112 self,
b45288f… lmata 113 node,
b45288f… lmata 114 source: bytes,
b45288f… lmata 115 file_path: str,
b45288f… lmata 116 store: GraphStore,
b45288f… lmata 117 stats: dict,
b45288f… lmata 118 ) -> None:
b45288f… lmata 119 # Only track top-level variable assignments (parent is program)
b45288f… lmata 120 if node.parent is None or node.parent.type not in ("program", "source_file"):
b45288f… lmata 121 return
b45288f… lmata 122
b45288f… lmata 123 name_node = node.child_by_field_name("name")
b45288f… lmata 124 if not name_node:
b45288f… lmata 125 return
b45288f… lmata 126 name = _node_text(name_node, source)
b45288f… lmata 127
b45288f… lmata 128 value_node = node.child_by_field_name("value")
b45288f… lmata 129 value = _node_text(value_node, source) if value_node else ""
b45288f… lmata 130
b45288f… lmata 131 store.create_node(
b45288f… lmata 132 NodeLabel.Variable,
b45288f… lmata 133 {
b45288f… lmata 134 "name": name,
b45288f… lmata 135 "file_path": file_path,
b45288f… lmata 136 "line_start": node.start_point[0] + 1,
b45288f… lmata 137 "line_end": node.end_point[0] + 1,
b45288f… lmata 138 "semantic_type": "shell_variable",
b45288f… lmata 139 "value": value,
b45288f… lmata 140 },
b45288f… lmata 141 )
b45288f… lmata 142
b45288f… lmata 143 store.create_edge(
b45288f… lmata 144 NodeLabel.File,
b45288f… lmata 145 {"path": file_path},
b45288f… lmata 146 EdgeType.CONTAINS,
b45288f… lmata 147 NodeLabel.Variable,
b45288f… lmata 148 {"name": name, "file_path": file_path},
b45288f… lmata 149 )
b45288f… lmata 150 stats["edges"] += 1
b45288f… lmata 151
b45288f… lmata 152 def _handle_command(
b45288f… lmata 153 self,
b45288f… lmata 154 node,
b45288f… lmata 155 source: bytes,
b45288f… lmata 156 file_path: str,
b45288f… lmata 157 store: GraphStore,
b45288f… lmata 158 stats: dict,
b45288f… lmata 159 ) -> None:
b45288f… lmata 160 """Handle source/. commands as imports."""
b45288f… lmata 161 name_node = node.child_by_field_name("name")
b45288f… lmata 162 if not name_node:
b45288f… lmata 163 return
b45288f… lmata 164 cmd_name = _node_text(name_node, source)
b45288f… lmata 165
b45288f… lmata 166 # Only handle source and . (dot-source) commands
b45288f… lmata 167 if cmd_name not in ("source", "."):
b45288f… lmata 168 return
b45288f… lmata 169
b45288f… lmata 170 # The sourced file path is the first argument
b45288f… lmata 171 arg_types = ("word", "string", "raw_string", "concatenation")
b45288f… lmata 172 args = [child for child in node.children if child != name_node and child.type in arg_types]
b45288f… lmata 173 if not args:
b45288f… lmata 174 return
b45288f… lmata 175 sourced_path = _node_text(args[0], source).strip("'\"")
b45288f… lmata 176
b45288f… lmata 177 store.create_node(
b45288f… lmata 178 NodeLabel.Import,
b45288f… lmata 179 {
b45288f… lmata 180 "name": sourced_path,
b45288f… lmata 181 "file_path": file_path,
b45288f… lmata 182 "line_start": node.start_point[0] + 1,
b45288f… lmata 183 "module": sourced_path,
b45288f… lmata 184 "semantic_type": "shell_source",
b45288f… lmata 185 },
b45288f… lmata 186 )
b45288f… lmata 187
b45288f… lmata 188 store.create_edge(
b45288f… lmata 189 NodeLabel.File,
b45288f… lmata 190 {"path": file_path},
b45288f… lmata 191 EdgeType.IMPORTS,
b45288f… lmata 192 NodeLabel.Import,
b45288f… lmata 193 {"name": sourced_path, "file_path": file_path},
b45288f… lmata 194 )
b45288f… lmata 195 stats["edges"] += 1
b45288f… lmata 196
b45288f… lmata 197 # ── Call extraction ───────────────────────────────────────────────────────
b45288f… lmata 198
b45288f… lmata 199 def _extract_calls(
b45288f… lmata 200 self,
b45288f… lmata 201 fn_node,
b45288f… lmata 202 source: bytes,
b45288f… lmata 203 file_path: str,
b45288f… lmata 204 fn_name: str,
b45288f… lmata 205 store: GraphStore,
b45288f… lmata 206 stats: dict,
b45288f… lmata 207 ) -> None:
b45288f… lmata 208 def walk(node):
b45288f… lmata 209 if node.type == "command":
b45288f… lmata 210 name_node = node.child_by_field_name("name")
b45288f… lmata 211 if name_node:
b45288f… lmata 212 callee = _node_text(name_node, source)
b45288f… lmata 213 # Skip builtins and source commands — only track function calls
b45288f… lmata 214 if callee not in (
b45288f… lmata 215 "source",
b45288f… lmata 216 ".",
b45288f… lmata 217 "echo",
b45288f… lmata 218 "printf",
b45288f… lmata 219 "cd",
b45288f… lmata 220 "exit",
b45288f… lmata 221 "return",
b45288f… lmata 222 "export",
b45288f… lmata 223 "local",
b45288f… lmata 224 "readonly",
b45288f… lmata 225 "declare",
b45288f… lmata 226 "typeset",
b45288f… lmata 227 "unset",
b45288f… lmata 228 "shift",
b45288f… lmata 229 "set",
b45288f… lmata 230 "eval",
b45288f… lmata 231 "exec",
b45288f… lmata 232 "test",
b45288f… lmata 233 "[",
b45288f… lmata 234 "[[",
b45288f… lmata 235 "true",
b45288f… lmata 236 "false",
b45288f… lmata 237 ":",
b45288f… lmata 238 "read",
b45288f… lmata 239 "if",
b45288f… lmata 240 "then",
b45288f… lmata 241 "else",
b45288f… lmata 242 "fi",
b45288f… lmata 243 "for",
b45288f… lmata 244 "while",
b45288f… lmata 245 "do",
b45288f… lmata 246 "done",
b45288f… lmata 247 "case",
b45288f… lmata 248 "esac",
b45288f… lmata 249 ):
b45288f… lmata 250 store.create_edge(
b45288f… lmata 251 NodeLabel.Function,
b45288f… lmata 252 {"name": fn_name, "file_path": file_path},
b45288f… lmata 253 EdgeType.CALLS,
b45288f… lmata 254 NodeLabel.Function,
b45288f… lmata 255 {"name": callee, "file_path": file_path},
b45288f… lmata 256 )
b45288f… lmata 257 stats["edges"] += 1
b45288f… lmata 258 for child in node.children:
b45288f… lmata 259 walk(child)
b45288f… lmata 260
b45288f… lmata 261 body = fn_node.child_by_field_name("body")
b45288f… lmata 262 if body:
b45288f… lmata 263 walk(body)

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button