forked from oliexdev/openScale
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknowledge_graph.py
More file actions
74 lines (62 loc) · 2.97 KB
/
Copy pathknowledge_graph.py
File metadata and controls
74 lines (62 loc) · 2.97 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
import os
import json
from pathlib import Path
def build_knowledge_graph():
graph = {
"nodes": [],
"edges": []
}
root_dir = "."
module_dirs = []
# Dynamically find top-level modules based on build files or significant directories
for entry in os.scandir(root_dir):
if entry.is_dir() and not entry.name.startswith('.'):
# Check if directory has build files or is a known source dir
has_build_files = any(os.path.exists(os.path.join(entry.path, f)) for f in ["build.gradle", "build.gradle.kts", "CMakeLists.txt", "Makefile"])
is_significant = entry.name in ["docs", "fastlane", "arduino_mcu"]
if has_build_files or is_significant:
module_dirs.append(entry.name)
node_type = "module"
if entry.name == "docs": node_type = "docs"
if entry.name == "fastlane": node_type = "deployment"
graph["nodes"].append({
"id": entry.name,
"label": entry.name.replace("_", " ").title(),
"type": node_type,
"path": os.path.relpath(entry.path, root_dir).replace('\\', '/')
})
relation = "documents" if node_type == "docs" else "contains"
graph["edges"].append({
"source": "root",
"target": entry.name,
"relation": relation
})
# Dynamically search for deeper components (e.g., core packages, libraries)
for mod in module_dirs:
for dirpath, dirnames, filenames in os.walk(mod):
# Skip hidden and build dirs
dirnames[:] = [d for d in dirnames if not d.startswith('.') and d not in ['build', 'tmp', 'schemas']]
# Identify components by looking for specific directory names that indicate architecture
path_parts = Path(dirpath).parts
if "core" in path_parts or "libraries" in path_parts:
comp_name = path_parts[-1]
# Avoid adding too many nodes, only add top-level core components
if len(path_parts) > 1 and path_parts[-2] in ["core", "libraries"]:
comp_id = f"{mod}_{comp_name}"
graph["nodes"].append({
"id": comp_id,
"label": comp_name.title() + " Component",
"type": "component",
"path": os.path.relpath(dirpath, root_dir).replace('\\', '/')
})
graph["edges"].append({
"source": mod,
"target": comp_id,
"relation": "implements"
})
os.makedirs(".github/data", exist_ok=True)
with open(".github/data/knowledge_graph.json", "w") as f:
json.dump(graph, f, indent=2)
print("Dynamic knowledge graph built.")
if __name__ == "__main__":
build_knowledge_graph()