-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump_module_tree.py
More file actions
315 lines (246 loc) · 8.25 KB
/
dump_module_tree.py
File metadata and controls
315 lines (246 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from __future__ import annotations
import argparse
import json
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
@dataclass
class TreeNode:
name: str
full_name: str = ""
file_ids: list[str] = field(default_factory=list)
children: dict[str, "TreeNode"] = field(default_factory=dict)
def load_json(path: Path) -> Any:
return json.loads(path.read_text(encoding="utf-8"))
def normalize_module_name(name: str | None) -> str:
if not name:
return ""
result = name.strip()
result = result.replace(" :", ":")
result = result.replace(": ", ":")
result = result.replace(" . ", ".")
return result
def load_manifest(index_root: Path) -> dict[str, Any]:
return load_json(index_root / "manifest.json")
def load_modules(index_root: Path) -> dict[str, list[str]]:
raw = load_json(index_root / "modules.json")
return {
normalize_module_name(name): file_ids
for name, file_ids in raw.items()
}
def load_file_index(index_root: Path, file_id: str) -> dict[str, Any]:
return load_json(index_root / "files" / f"{file_id}.json")
def module_parts(module_name: str) -> list[str]:
module_name = normalize_module_name(module_name)
if not module_name:
return []
if ":" in module_name:
primary, partition = module_name.split(":", 1)
else:
primary, partition = module_name, ""
parts = [part for part in primary.split(".") if part]
if partition:
parts.append(f":{partition}")
return parts
def build_module_tree(modules: dict[str, list[str]]) -> TreeNode:
root = TreeNode(name="<modules>", full_name="")
for module_name, file_ids in sorted(modules.items(), key=lambda item: item[0].casefold()):
parts = module_parts(module_name)
if not parts:
continue
node = root
full_parts: list[str] = []
for part in parts:
if part.startswith(":"):
full_name = ".".join(full_parts) + part
else:
full_parts.append(part)
full_name = ".".join(full_parts)
child = node.children.get(part)
if child is None:
child = TreeNode(name=part, full_name=full_name)
node.children[part] = child
node = child
node.file_ids.extend(file_ids)
node.full_name = module_name
return root
def format_file_list(
*,
file_ids: list[str],
file_by_id: dict[str, dict[str, Any]],
max_files: int,
) -> str:
if not file_ids:
return ""
files = [
file_by_id[file_id]["relativePath"]
for file_id in file_ids
if file_id in file_by_id
]
if not files:
return ""
shown = files[:max_files]
suffix = ""
if len(files) > max_files:
suffix = f", +{len(files) - max_files} more"
return " [" + ", ".join(shown) + suffix + "]"
def dump_tree(
*,
node: TreeNode,
file_by_id: dict[str, dict[str, Any]],
indent: str = "",
max_depth: int | None = None,
current_depth: int = 0,
max_files: int = 1,
) -> list[str]:
lines: list[str] = []
if max_depth is not None and current_depth > max_depth:
return lines
children = sorted(node.children.values(), key=lambda item: item.name.casefold())
for child in children:
marker = "*" if child.file_ids else "+"
file_text = format_file_list(
file_ids=child.file_ids,
file_by_id=file_by_id,
max_files=max_files,
)
count_text = f" ({len(child.file_ids)} file)" if child.file_ids else ""
lines.append(f"{indent}{marker} {child.name}{count_text}{file_text}")
lines.extend(
dump_tree(
node=child,
file_by_id=file_by_id,
indent=indent + " ",
max_depth=max_depth,
current_depth=current_depth + 1,
max_files=max_files,
)
)
return lines
def collect_module_imports(index_root: Path, file_ids: list[str]) -> list[str]:
imports: list[str] = []
seen: set[str] = set()
for file_id in file_ids:
file_index = load_file_index(index_root, file_id)
for entry in file_index.get("imports", []):
target = normalize_module_name(
entry.get("resolvedModule") or entry.get("module")
)
if not target:
continue
if target not in seen:
seen.add(target)
imports.append(target)
imports.sort(key=str.casefold)
return imports
def dump_import_tree(
*,
index_root: Path,
modules: dict[str, list[str]],
module_name: str,
max_depth: int,
indent: str = "",
depth: int = 0,
seen: set[str] | None = None,
) -> list[str]:
module_name = normalize_module_name(module_name)
seen = seen or set()
lines: list[str] = []
if module_name in seen:
lines.append(f"{indent}- {module_name} [cycle]")
return lines
seen.add(module_name)
file_ids = modules.get(module_name, [])
status = "" if file_ids else " [not indexed]"
lines.append(f"{indent}- {module_name}{status}")
if depth >= max_depth or not file_ids:
return lines
for imported in collect_module_imports(index_root, file_ids):
lines.extend(
dump_import_tree(
index_root=index_root,
modules=modules,
module_name=imported,
max_depth=max_depth,
indent=indent + " ",
depth=depth + 1,
seen=set(seen),
)
)
return lines
def main() -> None:
parser = argparse.ArgumentParser(
description=(
"Dump module-oriented views from a cpp.project_index.v1 output. "
"This is a diagnostic/readability tool; it does not analyze code."
)
)
parser.add_argument(
"--index-root",
type=Path,
default=Path.cwd() / ".mcp-cpp-project-indexer",
help="Project index root containing manifest.json, modules.json and files/.",
)
parser.add_argument(
"--imports",
type=str,
default=None,
help="Dump import tree for the given full C++20 module name.",
)
parser.add_argument(
"--max-depth",
type=int,
default=None,
help="Maximum depth for the module name tree. For --imports, default is 4.",
)
parser.add_argument(
"--max-files",
type=int,
default=1,
help="Maximum file paths to show per module leaf in the name tree.",
)
parser.add_argument(
"--output",
type=Path,
default=None,
help="Optional output text file. Defaults to stdout.",
)
args = parser.parse_args()
manifest = load_manifest(args.index_root)
modules = load_modules(args.index_root)
file_by_id = {
item["fileId"]: item
for item in manifest.get("files", [])
}
if args.imports:
max_depth = args.max_depth if args.max_depth is not None else 4
lines = dump_import_tree(
index_root=args.index_root,
modules=modules,
module_name=args.imports,
max_depth=max_depth,
)
else:
tree = build_module_tree(modules)
lines = [
f"Module tree: {args.index_root}",
f"Modules: {len(modules)}",
"Legend: + namespace/group, * module leaf",
"",
]
lines.extend(
dump_tree(
node=tree,
file_by_id=file_by_id,
max_depth=args.max_depth,
max_files=args.max_files,
)
)
text = "\n".join(lines) + "\n"
if args.output:
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(text, encoding="utf-8")
else:
print(text, end="")
if __name__ == "__main__":
main()