Navegador

navegador / navegador / ingestion / puppet.py
Source Blame History 339 lines
b45288f… lmata 1 """
b45288f… lmata 2 Puppet manifest parser — extracts classes, defined types, node definitions,
b45288f… lmata 3 resource declarations, includes, and parameters from .pp 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_puppet_language():
b45288f… lmata 17 try:
b45288f… lmata 18 import tree_sitter_puppet as tspuppet # type: ignore[import]
b45288f… lmata 19 from tree_sitter import Language
b45288f… lmata 20
b45288f… lmata 21 return Language(tspuppet.language())
b45288f… lmata 22 except ImportError as e:
b45288f… lmata 23 raise ImportError("Install tree-sitter-puppet: pip install tree-sitter-puppet") 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 def _class_identifier_text(node, source: bytes) -> str:
b45288f… lmata 31 """Join identifier children of a class_identifier with '::'."""
b45288f… lmata 32 parts = [_node_text(child, source) for child in node.children if child.type == "identifier"]
b45288f… lmata 33 return "::".join(parts) if parts else _node_text(node, source)
b45288f… lmata 34
b45288f… lmata 35
b45288f… lmata 36 class PuppetParser(LanguageParser):
b45288f… lmata 37 """Parses Puppet manifest files into the navegador graph."""
b45288f… lmata 38
b45288f… lmata 39 def __init__(self) -> None:
b45288f… lmata 40 from tree_sitter import Parser # type: ignore[import]
b45288f… lmata 41
b45288f… lmata 42 self._parser = Parser(_get_puppet_language())
b45288f… lmata 43
b45288f… lmata 44 def parse_file(self, path: Path, repo_root: Path, store: GraphStore) -> dict[str, int]:
b45288f… lmata 45 source = path.read_bytes()
b45288f… lmata 46 tree = self._parser.parse(source)
b45288f… lmata 47 rel_path = str(path.relative_to(repo_root))
b45288f… lmata 48
b45288f… lmata 49 store.create_node(
b45288f… lmata 50 NodeLabel.File,
b45288f… lmata 51 {
b45288f… lmata 52 "name": path.name,
b45288f… lmata 53 "path": rel_path,
b45288f… lmata 54 "language": "puppet",
b45288f… lmata 55 "line_count": source.count(b"\n"),
b45288f… lmata 56 },
b45288f… lmata 57 )
b45288f… lmata 58
b45288f… lmata 59 stats = {"functions": 0, "classes": 0, "edges": 0}
b45288f… lmata 60 self._walk(tree.root_node, source, rel_path, store, stats)
b45288f… lmata 61 return stats
b45288f… lmata 62
b45288f… lmata 63 # ── AST walker ────────────────────────────────────────────────────────────
b45288f… lmata 64
b45288f… lmata 65 def _walk(self, node, source: bytes, file_path: str, store: GraphStore, stats: dict) -> None:
b45288f… lmata 66 if node.type == "class_definition":
b45288f… lmata 67 self._handle_class(node, source, file_path, store, stats)
b45288f… lmata 68 return
b45288f… lmata 69 if node.type == "defined_resource_type":
b45288f… lmata 70 self._handle_defined_type(node, source, file_path, store, stats)
b45288f… lmata 71 return
b45288f… lmata 72 if node.type == "node_definition":
b45288f… lmata 73 self._handle_node(node, source, file_path, store, stats)
b45288f… lmata 74 return
b45288f… lmata 75 if node.type == "include_statement":
b45288f… lmata 76 self._handle_include(node, source, file_path, store, stats)
b45288f… lmata 77 return
b45288f… lmata 78 for child in node.children:
b45288f… lmata 79 self._walk(child, source, file_path, store, stats)
b45288f… lmata 80
b45288f… lmata 81 # ── Handlers ──────────────────────────────────────────────────────────────
b45288f… lmata 82
b45288f… lmata 83 def _handle_class(
b45288f… lmata 84 self, node, source: bytes, file_path: str, store: GraphStore, stats: dict
b45288f… lmata 85 ) -> None:
b45288f… lmata 86 name = self._extract_class_identifier(node, source)
b45288f… lmata 87 if not name:
b45288f… lmata 88 return
b45288f… lmata 89
b45288f… lmata 90 store.create_node(
b45288f… lmata 91 NodeLabel.Class,
b45288f… lmata 92 {
b45288f… lmata 93 "name": name,
b45288f… lmata 94 "file_path": file_path,
b45288f… lmata 95 "line_start": node.start_point[0] + 1,
b45288f… lmata 96 "line_end": node.end_point[0] + 1,
b45288f… lmata 97 "docstring": "",
b45288f… lmata 98 "semantic_type": "puppet_class",
b45288f… lmata 99 },
b45288f… lmata 100 )
b45288f… lmata 101 store.create_edge(
b45288f… lmata 102 NodeLabel.File,
b45288f… lmata 103 {"path": file_path},
b45288f… lmata 104 EdgeType.CONTAINS,
b45288f… lmata 105 NodeLabel.Class,
b45288f… lmata 106 {"name": name, "file_path": file_path},
b45288f… lmata 107 )
b45288f… lmata 108 stats["classes"] += 1
b45288f… lmata 109 stats["edges"] += 1
b45288f… lmata 110
b45288f… lmata 111 self._extract_parameters(node, source, file_path, name, store, stats)
b45288f… lmata 112 self._extract_resources(node, source, file_path, name, store, stats)
b45288f… lmata 113
b45288f… lmata 114 def _handle_defined_type(
b45288f… lmata 115 self, node, source: bytes, file_path: str, store: GraphStore, stats: dict
b45288f… lmata 116 ) -> None:
b45288f… lmata 117 name = self._extract_class_identifier(node, source)
b45288f… lmata 118 if not name:
b45288f… lmata 119 return
b45288f… lmata 120
b45288f… lmata 121 store.create_node(
b45288f… lmata 122 NodeLabel.Class,
b45288f… lmata 123 {
b45288f… lmata 124 "name": name,
b45288f… lmata 125 "file_path": file_path,
b45288f… lmata 126 "line_start": node.start_point[0] + 1,
b45288f… lmata 127 "line_end": node.end_point[0] + 1,
b45288f… lmata 128 "docstring": "",
b45288f… lmata 129 "semantic_type": "puppet_defined_type",
b45288f… lmata 130 },
b45288f… lmata 131 )
b45288f… lmata 132 store.create_edge(
b45288f… lmata 133 NodeLabel.File,
b45288f… lmata 134 {"path": file_path},
b45288f… lmata 135 EdgeType.CONTAINS,
b45288f… lmata 136 NodeLabel.Class,
b45288f… lmata 137 {"name": name, "file_path": file_path},
b45288f… lmata 138 )
b45288f… lmata 139 stats["classes"] += 1
b45288f… lmata 140 stats["edges"] += 1
b45288f… lmata 141
b45288f… lmata 142 self._extract_parameters(node, source, file_path, name, store, stats)
b45288f… lmata 143 self._extract_resources(node, source, file_path, name, store, stats)
b45288f… lmata 144
b45288f… lmata 145 def _handle_node(
b45288f… lmata 146 self, node, source: bytes, file_path: str, store: GraphStore, stats: dict
b45288f… lmata 147 ) -> None:
b45288f… lmata 148 name = self._extract_node_name(node, source)
b45288f… lmata 149 if not name:
b45288f… lmata 150 return
b45288f… lmata 151
b45288f… lmata 152 store.create_node(
b45288f… lmata 153 NodeLabel.Class,
b45288f… lmata 154 {
b45288f… lmata 155 "name": name,
b45288f… lmata 156 "file_path": file_path,
b45288f… lmata 157 "line_start": node.start_point[0] + 1,
b45288f… lmata 158 "line_end": node.end_point[0] + 1,
b45288f… lmata 159 "docstring": "",
b45288f… lmata 160 "semantic_type": "puppet_node",
b45288f… lmata 161 },
b45288f… lmata 162 )
b45288f… lmata 163 store.create_edge(
b45288f… lmata 164 NodeLabel.File,
b45288f… lmata 165 {"path": file_path},
b45288f… lmata 166 EdgeType.CONTAINS,
b45288f… lmata 167 NodeLabel.Class,
b45288f… lmata 168 {"name": name, "file_path": file_path},
b45288f… lmata 169 )
b45288f… lmata 170 stats["classes"] += 1
b45288f… lmata 171 stats["edges"] += 1
b45288f… lmata 172
b45288f… lmata 173 self._extract_resources(node, source, file_path, name, store, stats)
b45288f… lmata 174
b45288f… lmata 175 def _handle_include(
b45288f… lmata 176 self, node, source: bytes, file_path: str, store: GraphStore, stats: dict
b45288f… lmata 177 ) -> None:
b45288f… lmata 178 ident_node = None
b45288f… lmata 179 for child in node.children:
b45288f… lmata 180 if child.type == "class_identifier":
b45288f… lmata 181 ident_node = child
b45288f… lmata 182 break
b45288f… lmata 183 if not ident_node:
b45288f… lmata 184 return
b45288f… lmata 185
b45288f… lmata 186 module = _class_identifier_text(ident_node, source)
b45288f… lmata 187 store.create_node(
b45288f… lmata 188 NodeLabel.Import,
b45288f… lmata 189 {
b45288f… lmata 190 "name": module,
b45288f… lmata 191 "file_path": file_path,
b45288f… lmata 192 "line_start": node.start_point[0] + 1,
b45288f… lmata 193 "module": module,
b45288f… lmata 194 "semantic_type": "puppet_include",
b45288f… lmata 195 },
b45288f… lmata 196 )
b45288f… lmata 197 store.create_edge(
b45288f… lmata 198 NodeLabel.File,
b45288f… lmata 199 {"path": file_path},
b45288f… lmata 200 EdgeType.IMPORTS,
b45288f… lmata 201 NodeLabel.Import,
b45288f… lmata 202 {"name": module, "file_path": file_path},
b45288f… lmata 203 )
b45288f… lmata 204 stats["edges"] += 1
b45288f… lmata 205
b45288f… lmata 206 # ── Extractors ────────────────────────────────────────────────────────────
b45288f… lmata 207
b45288f… lmata 208 def _extract_class_identifier(self, node, source: bytes) -> str | None:
b45288f… lmata 209 """Find and return the class_identifier text from a class/define node."""
b45288f… lmata 210 for child in node.children:
b45288f… lmata 211 if child.type == "class_identifier":
b45288f… lmata 212 return _class_identifier_text(child, source)
b45288f… lmata 213 return None
b45288f… lmata 214
b45288f… lmata 215 def _extract_node_name(self, node, source: bytes) -> str | None:
b45288f… lmata 216 """Extract the node name from a node_definition (string child of node_name)."""
b45288f… lmata 217 for child in node.children:
b45288f… lmata 218 if child.type == "node_name":
b45288f… lmata 219 for grandchild in child.children:
b45288f… lmata 220 if grandchild.type == "string":
b45288f… lmata 221 return _node_text(grandchild, source).strip("'\"")
b45288f… lmata 222 return _node_text(child, source).strip("'\"")
b45288f… lmata 223 return None
b45288f… lmata 224
b45288f… lmata 225 def _extract_parameters(
b45288f… lmata 226 self,
b45288f… lmata 227 node,
b45288f… lmata 228 source: bytes,
b45288f… lmata 229 file_path: str,
b45288f… lmata 230 class_name: str,
b45288f… lmata 231 store: GraphStore,
b45288f… lmata 232 stats: dict,
b45288f… lmata 233 ) -> None:
b45288f… lmata 234 """Extract parameters from a parameter_list inside a class/define."""
b45288f… lmata 235 for child in node.children:
b45288f… lmata 236 if child.type != "parameter_list":
b45288f… lmata 237 continue
b45288f… lmata 238 for param in child.children:
b45288f… lmata 239 if param.type != "parameter":
b45288f… lmata 240 continue
b45288f… lmata 241 var_node = None
b45288f… lmata 242 for pc in param.children:
b45288f… lmata 243 if pc.type == "variable":
b45288f… lmata 244 var_node = pc
b45288f… lmata 245 break
b45288f… lmata 246 if not var_node:
b45288f… lmata 247 continue
b45288f… lmata 248 var_name = _node_text(var_node, source).lstrip("$")
b45288f… lmata 249 store.create_node(
b45288f… lmata 250 NodeLabel.Variable,
b45288f… lmata 251 {
b45288f… lmata 252 "name": var_name,
b45288f… lmata 253 "file_path": file_path,
b45288f… lmata 254 "line_start": param.start_point[0] + 1,
b45288f… lmata 255 "semantic_type": "puppet_parameter",
b45288f… lmata 256 },
b45288f… lmata 257 )
b45288f… lmata 258 store.create_edge(
b45288f… lmata 259 NodeLabel.Class,
b45288f… lmata 260 {"name": class_name, "file_path": file_path},
b45288f… lmata 261 EdgeType.CONTAINS,
b45288f… lmata 262 NodeLabel.Variable,
b45288f… lmata 263 {"name": var_name, "file_path": file_path},
b45288f… lmata 264 )
b45288f… lmata 265 stats["edges"] += 1
b45288f… lmata 266
b45288f… lmata 267 def _extract_resources(
b45288f… lmata 268 self,
b45288f… lmata 269 node,
b45288f… lmata 270 source: bytes,
b45288f… lmata 271 file_path: str,
b45288f… lmata 272 class_name: str,
b45288f… lmata 273 store: GraphStore,
b45288f… lmata 274 stats: dict,
b45288f… lmata 275 ) -> None:
b45288f… lmata 276 """Walk the block of a class/define/node to find resource declarations."""
b45288f… lmata 277 for child in node.children:
b45288f… lmata 278 if child.type == "block":
b45288f… lmata 279 self._walk_block_for_resources(child, source, file_path, class_name, store, stats)
b45288f… lmata 280 break
b45288f… lmata 281
b45288f… lmata 282 def _walk_block_for_resources(
b45288f… lmata 283 self,
b45288f… lmata 284 node,
b45288f… lmata 285 source: bytes,
b45288f… lmata 286 file_path: str,
b45288f… lmata 287 class_name: str,
b45288f… lmata 288 store: GraphStore,
b45288f… lmata 289 stats: dict,
b45288f… lmata 290 ) -> None:
b45288f… lmata 291 """Recursively find resource_declaration nodes inside a block."""
b45288f… lmata 292 if node.type == "resource_declaration":
b45288f… lmata 293 self._handle_resource(node, source, file_path, class_name, store, stats)
b45288f… lmata 294 return
b45288f… lmata 295 for child in node.children:
b45288f… lmata 296 self._walk_block_for_resources(child, source, file_path, class_name, store, stats)
b45288f… lmata 297
b45288f… lmata 298 def _handle_resource(
b45288f… lmata 299 self,
b45288f… lmata 300 node,
b45288f… lmata 301 source: bytes,
b45288f… lmata 302 file_path: str,
b45288f… lmata 303 class_name: str,
b45288f… lmata 304 store: GraphStore,
b45288f… lmata 305 stats: dict,
b45288f… lmata 306 ) -> None:
b45288f… lmata 307 """Handle a resource_declaration: first identifier = type, first string = title."""
b45288f… lmata 308 res_type = None
b45288f… lmata 309 res_title = None
b45288f… lmata 310 for child in node.children:
b45288f… lmata 311 if child.type == "identifier" and res_type is None:
b45288f… lmata 312 res_type = _node_text(child, source)
b45288f… lmata 313 if child.type == "string" and res_title is None:
b45288f… lmata 314 res_title = _node_text(child, source).strip("'\"")
b45288f… lmata 315 if not res_type:
b45288f… lmata 316 return
b45288f… lmata 317
b45288f… lmata 318 name = f"{res_type}[{res_title}]" if res_title else res_type
b45288f… lmata 319 store.create_node(
b45288f… lmata 320 NodeLabel.Function,
b45288f… lmata 321 {
b45288f… lmata 322 "name": name,
b45288f… lmata 323 "file_path": file_path,
b45288f… lmata 324 "line_start": node.start_point[0] + 1,
b45288f… lmata 325 "line_end": node.end_point[0] + 1,
b45288f… lmata 326 "docstring": "",
b45288f… lmata 327 "class_name": class_name,
b45288f… lmata 328 "semantic_type": "puppet_resource",
b45288f… lmata 329 },
b45288f… lmata 330 )
b45288f… lmata 331 store.create_edge(
b45288f… lmata 332 NodeLabel.Class,
b45288f… lmata 333 {"name": class_name, "file_path": file_path},
b45288f… lmata 334 EdgeType.CONTAINS,
b45288f… lmata 335 NodeLabel.Function,
b45288f… lmata 336 {"name": name, "file_path": file_path},
b45288f… lmata 337 )
b45288f… lmata 338 stats["functions"] += 1
b45288f… lmata 339 stats["edges"] += 1

Keyboard Shortcuts

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