-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1448 lines (1247 loc) Β· 57.1 KB
/
Copy pathmain.py
File metadata and controls
1448 lines (1247 loc) Β· 57.1 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import requests
from neo4j import GraphDatabase
from git import Repo
import os
import ast
import base64
from urllib.parse import urlparse
import shutil
from dotenv import load_dotenv
load_dotenv()
# Configuration from environment variables
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
NEO4J_USER = os.getenv("NEO4J_USER")
NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD")
# Configuration constants
REPO_URL = "https://api.github.com/repos/openai/codex" # Example repo
NEO4J_URI = "bolt://localhost:7687" # Docker Neo4j instance
LOCAL_REPO_PATH = "./temp_repo" # Temporary path for cloning
OUTPUT_FILE = "./knowledge_graph_export.txt" # LLM-readable output file
# Initialize Neo4j driver
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USER, NEO4J_PASSWORD))
# GitHub API headers
headers = {
"Authorization": f"Bearer {GITHUB_TOKEN}",
"Accept": "application/vnd.github+json",
}
def clear_graph(tx):
"""Clear existing nodes and relationships in the graph."""
tx.run("MATCH (n) DETACH DELETE n")
def add_directory(tx, path):
"""Add a directory node to Neo4j."""
tx.run("MERGE (d:Directory {path: $path}) " "SET d.type = 'directory'", path=path)
def add_file(tx, path):
"""Add a file node to Neo4j."""
tx.run("MERGE (f:File {path: $path}) " "SET f.type = 'file'", path=path)
def add_contains(tx, parent, child):
"""Add a CONTAINS relationship between a directory/file and a file/function/class."""
tx.run(
"MATCH (p {path: $parent}) "
"MATCH (c {path: $child}) "
"MERGE (p)-[:CONTAINS]->(c)",
parent=parent,
child=child,
)
def add_function(tx, name, file_path):
"""Add a function node to Neo4j."""
func_path = f"{file_path}::{name}" # Unique identifier
tx.run(
"MERGE (func:Function {path: $path}) "
"SET func.name = $name, func.file = $file_path, func.type = 'function'",
path=func_path,
name=name,
file_path=file_path,
)
def add_class(tx, name, file_path):
"""Add a class node to Neo4j."""
class_path = f"{file_path}::{name}" # Unique identifier
tx.run(
"MERGE (c:Class {path: $path}) "
"SET c.name = $name, c.file = $file_path, c.type = 'class'",
path=class_path,
name=name,
file_path=file_path,
)
def add_commit(tx, commit_id, message, timestamp):
"""Add a commit node to Neo4j."""
tx.run(
"MERGE (c:Commit {id: $commit_id}) "
"SET c.message = $message, c.timestamp = $timestamp, c.type = 'commit'",
commit_id=commit_id,
message=message,
timestamp=timestamp,
)
def add_author(tx, name, email):
"""Add an author node to Neo4j."""
tx.run(
"MERGE (a:Author {name: $name}) " "SET a.email = $email, a.type = 'author'",
name=name,
email=email,
)
def add_authored(tx, author, commit_id):
"""Add an AUTHORED relationship between an author and a commit."""
tx.run(
"MATCH (a:Author {name: $author}) "
"MATCH (c:Commit {id: $commit_id}) "
"MERGE (a)-[:AUTHORED]->(c)",
author=author,
commit_id=commit_id,
)
def add_modified(tx, commit_id, file_path):
"""Add a MODIFIED relationship between a commit and a file."""
tx.run(
"MATCH (c:Commit {id: $commit_id}) "
"MATCH (f:File {path: $file_path}) "
"MERGE (c)-[:MODIFIED]->(f)",
commit_id=commit_id,
file_path=file_path,
)
def add_import(tx, from_file, to_file, import_name, import_type="module"):
"""Add an IMPORTS relationship between files."""
tx.run(
"MERGE (imp:Import {name: $import_name, from_file: $from_file, to_file: $to_file, type: $import_type}) "
"WITH imp "
"MATCH (f1:File {path: $from_file}) "
"MATCH (f2:File {path: $to_file}) "
"MERGE (f1)-[:IMPORTS {name: $import_name, type: $import_type}]->(f2) "
"MERGE (f1)-[:CONTAINS]->(imp) "
"MERGE (imp)-[:REFERENCES]->(f2)",
from_file=from_file,
to_file=to_file,
import_name=import_name,
import_type=import_type,
)
def add_function_call(tx, caller_path, callee_name, caller_file):
"""Add a CALLS relationship between functions."""
tx.run(
"MATCH (caller {path: $caller_path}) "
"MERGE (call:FunctionCall {caller: $caller_path, callee: $callee_name, file: $caller_file}) "
"MERGE (caller)-[:CALLS {function: $callee_name}]->(call)",
caller_path=caller_path,
callee_name=callee_name,
caller_file=caller_file,
)
def add_inheritance(tx, child_class_path, parent_class_name, file_path):
"""Add an INHERITS relationship between classes."""
tx.run(
"MATCH (child:Class {path: $child_class_path}) "
"MERGE (parent:Class {name: $parent_class_name, file: $file_path}) "
"MERGE (child)-[:INHERITS]->(parent)",
child_class_path=child_class_path,
parent_class_name=parent_class_name,
file_path=file_path,
)
def add_method(tx, method_name, class_path, file_path):
"""Add a method node and relationship to its class."""
method_path = f"{class_path}::{method_name}"
tx.run(
"MERGE (method:Method {path: $method_path}) "
"SET method.name = $method_name, method.class = $class_path, method.file = $file_path, method.type = 'method' "
"WITH method "
"MATCH (cls:Class {path: $class_path}) "
"MERGE (cls)-[:HAS_METHOD]->(method)",
method_path=method_path,
method_name=method_name,
class_path=class_path,
file_path=file_path,
)
def add_decorator(tx, decorated_path, decorator_name, file_path):
"""Add a DECORATED_BY relationship."""
tx.run(
"MATCH (decorated {path: $decorated_path}) "
"MERGE (decorator:Decorator {name: $decorator_name, file: $file_path}) "
"MERGE (decorated)-[:DECORATED_BY]->(decorator)",
decorated_path=decorated_path,
decorator_name=decorator_name,
file_path=file_path,
)
def add_variable(tx, var_name, scope_path, file_path, var_type="variable"):
"""Add a variable node and relationship to its scope."""
var_path = f"{scope_path}::{var_name}"
tx.run(
"MERGE (var:Variable {path: $var_path}) "
"SET var.name = $var_name, var.scope = $scope_path, var.file = $file_path, var.type = $var_type "
"WITH var "
"MATCH (scope {path: $scope_path}) "
"MERGE (scope)-[:DEFINES]->(var)",
var_path=var_path,
var_name=var_name,
scope_path=scope_path,
file_path=file_path,
var_type=var_type,
)
def fetch_repo_tree(repo_url):
"""Fetch the repository's file tree using GitHub API."""
response = requests.get(f"{repo_url}/git/trees/main?recursive=1", headers=headers)
response.raise_for_status()
return response.json()["tree"]
def fetch_file_content(repo_owner, repo_name, path):
"""Fetch the content of a file from GitHub."""
response = requests.get(
f"https://api.github.com/repos/{repo_owner}/{repo_name}/contents/{path}",
headers=headers,
)
response.raise_for_status()
content = response.json()
if "content" in content:
return base64.b64decode(content["content"]).decode("utf-8", errors="ignore")
return ""
def clone_repo(repo_url, local_path):
"""Clone the repository locally to access commit history."""
if os.path.exists(local_path):
shutil.rmtree(local_path)
# Convert API URL to git clone URL
# From: https://api.github.com/repos/owner/repo
# To: https://github.com/owner/repo.git
parsed_url = urlparse(repo_url)
path_parts = parsed_url.path.split("/")
if len(path_parts) >= 4 and path_parts[1] == "repos":
owner = path_parts[2]
repo_name = path_parts[3]
git_url = f"https://github.com/{owner}/{repo_name}.git"
else:
raise ValueError(f"Invalid GitHub API URL format: {repo_url}")
print(f"Cloning from: {git_url}")
return Repo.clone_from(git_url, local_path)
def parse_python_file(content, file_path, session):
"""Parse Python file content to extract functions, classes, and all relationships."""
try:
tree = ast.parse(content)
# Track current class context for methods
current_class = None
current_class_path = None
# Extract imports first
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
import_name = alias.name
# Try to resolve to file path (basic heuristic)
potential_file = import_name.replace(".", "/") + ".py"
session.execute_write(
add_import, file_path, potential_file, import_name, "module"
)
print(f"Added import: {file_path} -> {import_name}")
elif isinstance(node, ast.ImportFrom):
if node.module:
module_name = node.module
potential_file = module_name.replace(".", "/") + ".py"
for alias in node.names:
import_name = alias.name
session.execute_write(
add_import,
file_path,
potential_file,
f"{module_name}.{import_name}",
"from_import",
)
print(
f"Added from import: {file_path} -> {module_name}.{import_name}"
)
# Process top-level nodes in order to maintain context
for node in tree.body:
if isinstance(node, ast.ClassDef):
class_path = f"{file_path}::{node.name}"
current_class = node.name
current_class_path = class_path
# Add class
session.execute_write(add_class, node.name, file_path)
session.execute_write(add_contains, file_path, class_path)
print(f"Added class: {node.name} in {file_path}")
# Add inheritance relationships
for base in node.bases:
if isinstance(base, ast.Name):
parent_name = base.id
session.execute_write(
add_inheritance, class_path, parent_name, file_path
)
print(f"Added inheritance: {node.name} -> {parent_name}")
elif isinstance(base, ast.Attribute):
# Handle module.ClassName inheritance
parent_name = (
ast.unparse(base)
if hasattr(ast, "unparse")
else str(base.attr)
)
session.execute_write(
add_inheritance, class_path, parent_name, file_path
)
print(f"Added inheritance: {node.name} -> {parent_name}")
# Add decorators
for decorator in node.decorator_list:
decorator_name = (
ast.unparse(decorator)
if hasattr(ast, "unparse")
else str(decorator)
)
session.execute_write(
add_decorator, class_path, decorator_name, file_path
)
print(f"Added decorator: {decorator_name} on class {node.name}")
# Process class methods
for class_node in node.body:
if isinstance(class_node, ast.FunctionDef):
method_path = f"{class_path}::{class_node.name}"
# Add method
session.execute_write(
add_method, class_node.name, class_path, file_path
)
print(f"Added method: {class_node.name} in class {node.name}")
# Add method decorators
for decorator in class_node.decorator_list:
decorator_name = (
ast.unparse(decorator)
if hasattr(ast, "unparse")
else str(decorator)
)
session.execute_write(
add_decorator, method_path, decorator_name, file_path
)
print(
f"Added decorator: {decorator_name} on method {class_node.name}"
)
# Extract function calls within method
_extract_function_calls(
class_node, method_path, file_path, session
)
# Extract variables within method
_extract_variables(class_node, method_path, file_path, session)
elif isinstance(class_node, ast.Assign):
# Class attributes
for target in class_node.targets:
if isinstance(target, ast.Name):
session.execute_write(
add_variable,
target.id,
class_path,
file_path,
"class_attribute",
)
print(
f"Added class attribute: {target.id} in {node.name}"
)
current_class = None
current_class_path = None
elif isinstance(node, ast.FunctionDef):
func_path = f"{file_path}::{node.name}"
# Add function
session.execute_write(add_function, node.name, file_path)
session.execute_write(add_contains, file_path, func_path)
print(f"Added function: {node.name} in {file_path}")
# Add decorators
for decorator in node.decorator_list:
decorator_name = (
ast.unparse(decorator)
if hasattr(ast, "unparse")
else str(decorator)
)
session.execute_write(
add_decorator, func_path, decorator_name, file_path
)
print(f"Added decorator: {decorator_name} on function {node.name}")
# Extract function calls within function
_extract_function_calls(node, func_path, file_path, session)
# Extract variables within function
_extract_variables(node, func_path, file_path, session)
elif isinstance(node, ast.Assign):
# Module-level variables
for target in node.targets:
if isinstance(target, ast.Name):
session.execute_write(
add_variable,
target.id,
file_path,
file_path,
"module_variable",
)
print(f"Added module variable: {target.id} in {file_path}")
except SyntaxError as e:
print(f"Failed to parse {file_path}: {e}")
def _extract_function_calls(func_node, func_path, file_path, session):
"""Extract function calls from within a function or method."""
for node in ast.walk(func_node):
if isinstance(node, ast.Call):
if isinstance(node.func, ast.Name):
# Simple function call: func_name()
callee_name = node.func.id
session.execute_write(
add_function_call, func_path, callee_name, file_path
)
print(f"Added function call: {func_path} -> {callee_name}")
elif isinstance(node.func, ast.Attribute):
# Method call: obj.method() or module.func()
callee_name = (
ast.unparse(node.func)
if hasattr(ast, "unparse")
else f"{node.func.attr}"
)
session.execute_write(
add_function_call, func_path, callee_name, file_path
)
print(f"Added method call: {func_path} -> {callee_name}")
def _extract_variables(func_node, func_path, file_path, session):
"""Extract variable assignments from within a function or method."""
for node in ast.walk(func_node):
if isinstance(node, ast.Assign):
for target in node.targets:
if isinstance(target, ast.Name):
var_name = target.id
session.execute_write(
add_variable, var_name, func_path, file_path, "local_variable"
)
print(f"Added local variable: {var_name} in {func_path}")
elif isinstance(target, ast.Attribute):
# Instance variables: self.var = value
if isinstance(target.value, ast.Name) and target.value.id == "self":
var_name = target.attr
session.execute_write(
add_variable,
var_name,
func_path,
file_path,
"instance_variable",
)
print(f"Added instance variable: {var_name} in {func_path}")
elif isinstance(node, ast.AnnAssign) and node.target:
# Type annotated assignments: var: int = 5
if isinstance(node.target, ast.Name):
var_name = node.target.id
session.execute_write(
add_variable, var_name, func_path, file_path, "annotated_variable"
)
print(f"Added annotated variable: {var_name} in {func_path}")
def process_repository():
"""Traverse the repository, parse Python files, and build the knowledge graph in Neo4j."""
# Clear existing graph
with driver.session() as session:
session.execute_write(clear_graph)
print("Cleared existing graph.")
# Fetch repository tree
print("Fetching repository tree...")
tree = fetch_repo_tree(REPO_URL)
repo_owner, repo_name = urlparse(REPO_URL).path.split("/")[2:4]
# Process directories, files, and Python file contents
with driver.session() as session:
for item in tree:
path = item["path"]
if item["type"] == "tree":
session.execute_write(add_directory, path)
print(f"Added directory: {path}")
elif item["type"] == "blob":
session.execute_write(add_file, path)
print(f"Added file: {path}")
# Parse Python files for functions and classes
if path.endswith(".py"):
content = fetch_file_content(repo_owner, repo_name, path)
parse_python_file(content, path, session)
# Add CONTAINS relationships for directory structure
if "/" in path:
parent_dir = "/".join(path.split("/")[:-1])
if parent_dir:
session.execute_write(add_contains, parent_dir, path)
print(f"Added CONTAINS: {parent_dir} -> {path}")
# Clone repo to access commit history
print("Cloning repository locally...")
repo = clone_repo(REPO_URL, LOCAL_REPO_PATH)
# Process commits (limit to recent 10 for example)
with driver.session() as session:
for commit in list(repo.iter_commits())[:10]:
commit_id = commit.hexsha
author_name = commit.author.name
author_email = commit.author.email
message = commit.message.strip()
timestamp = commit.committed_datetime.isoformat()
# Add commit and author
session.execute_write(add_commit, commit_id, message, timestamp)
session.execute_write(add_author, author_name, author_email)
session.execute_write(add_authored, author_name, commit_id)
print(f"Added commit: {commit_id} by {author_name}")
# Add MODIFIED relationships for files
for file_path in commit.stats.files:
session.execute_write(add_modified, commit_id, file_path)
print(f"Added MODIFIED: {commit_id} -> {file_path}")
def export_to_llm_readable_format(output_file):
"""Export the knowledge graph to an LLM-readable text format optimized for AI coding agents."""
with driver.session() as session:
with open(output_file, "w", encoding="utf-8") as f:
f.write("# Repository Knowledge Graph\n")
f.write("# Optimized for AI Coding Agent Analysis\n\n")
# Export project overview and entry points
f.write("## Project Overview\n\n")
# Identify configuration and entry point files
config_files = session.run(
"""
MATCH (f:File)
WHERE f.path =~ '.*\\.(json|yaml|yml|toml|ini|cfg|conf|py)$'
AND (f.path CONTAINS 'config' OR f.path CONTAINS 'setup' OR
f.path CONTAINS 'requirements' OR f.path CONTAINS 'package' OR
f.path =~ '.*main\\.py$' OR f.path =~ '.*__init__\\.py$')
RETURN f.path
ORDER BY f.path
"""
).data()
f.write("### Configuration & Entry Points:\n")
for config_record in config_files:
f.write(f"- {config_record['f.path']}\n")
# Export repository structure (simplified)
f.write("\n### Key Directories:\n")
directories = session.run(
"""
MATCH (d:Directory)
WHERE NOT d.path CONTAINS '__pycache__' AND NOT d.path CONTAINS '.git'
RETURN d.path
ORDER BY d.path
LIMIT 20
"""
).data()
for dir_record in directories:
f.write(f"- {dir_record['d.path']}\n")
# Export code structure with enhanced function information
f.write("\n## Code Architecture\n\n")
# Enhanced Functions with signatures and relationships
f.write("### Functions & Methods:\n")
functions = session.run(
"""
MATCH (func:Function)
OPTIONAL MATCH (f:File {path: func.file})
RETURN func.name, func.file, func.path
ORDER BY func.file, func.name
"""
).data()
current_file = None
for func_record in functions:
file_path = func_record["func.file"]
func_name = func_record["func.name"]
if file_path != current_file:
current_file = file_path
f.write(f"\n**{file_path}:**\n")
f.write(f" - {func_name}()\n")
# Enhanced Classes
f.write("\n### Classes & Objects:\n")
classes = session.run(
"""
MATCH (cls:Class)
RETURN cls.name, cls.file
ORDER BY cls.file, cls.name
"""
).data()
current_file = None
for cls_record in classes:
file_path = cls_record["cls.file"]
cls_name = cls_record["cls.name"]
if file_path != current_file:
current_file = file_path
f.write(f"\n**{file_path}:**\n")
f.write(f" - class {cls_name}\n")
# NEW: File Dependencies and Import Relationships
f.write("\n## Dependencies & Import Graph\n\n")
# Python files and their potential imports (basic analysis)
python_files = session.run(
"""
MATCH (f:File)
WHERE f.path ENDS WITH '.py'
RETURN f.path
ORDER BY f.path
"""
).data()
f.write("### Python Module Structure:\n")
for py_file in python_files:
file_path = py_file["f.path"]
# Convert file path to module name
module_name = file_path.replace("/", ".").replace(".py", "")
if module_name.endswith(".__init__"):
module_name = module_name[:-9] # Remove .__init__
f.write(f"- {module_name} ({file_path})\n")
# NEW: COMPREHENSIVE RELATIONSHIPS SECTION
f.write("\n## Code Relationships & Dependencies\n\n")
# File -> Functions/Classes relationships
f.write("### File Contents (What's defined where):\n")
file_contents = session.run(
"""
MATCH (f:File)-[:CONTAINS]->(item)
WHERE item:Function OR item:Class
WITH f.path as file_path,
collect(CASE WHEN item:Function THEN 'func:' + item.name ELSE 'class:' + item.name END) as items
WHERE size(items) > 0
RETURN file_path, items
ORDER BY file_path
"""
).data()
for file_content in file_contents:
file_path = file_content["file_path"]
items = file_content["items"]
f.write(f"\n**{file_path}:**\n")
for item in items:
if item.startswith("func:"):
f.write(f" π Function: {item[5:]}\n")
elif item.startswith("class:"):
f.write(f" ποΈ Class: {item[6:]}\n")
# Function/Class -> File relationships (reverse lookup)
f.write("\n### Code Element Locations (Where to find things):\n")
# Functions by file
functions_by_file = session.run(
"""
MATCH (func:Function)
RETURN func.file as file_path, collect(func.name) as functions
ORDER BY file_path
"""
).data()
f.write("\n#### Functions by File:\n")
for func_file in functions_by_file:
file_path = func_file["file_path"]
functions = func_file["functions"]
if functions:
f.write(f"- **{file_path}**: {', '.join(functions)}\n")
# Classes by file
classes_by_file = session.run(
"""
MATCH (cls:Class)
RETURN cls.file as file_path, collect(cls.name) as classes
ORDER BY file_path
"""
).data()
f.write("\n#### Classes by File:\n")
for class_file in classes_by_file:
file_path = class_file["file_path"]
classes = class_file["classes"]
if classes:
f.write(f"- **{file_path}**: {', '.join(classes)}\n")
# Directory structure with code elements
f.write("\n### Directory-based Code Organization:\n")
dir_structure = session.run(
"""
MATCH (d:Directory)-[:CONTAINS]->(f:File)
OPTIONAL MATCH (f)-[:CONTAINS]->(func:Function)
OPTIONAL MATCH (f)-[:CONTAINS]->(cls:Class)
WITH d.path as dir_path, f.path as file_path,
count(DISTINCT func) as func_count,
count(DISTINCT cls) as class_count
WHERE func_count > 0 OR class_count > 0
RETURN dir_path, collect({file: file_path, functions: func_count, classes: class_count}) as files
ORDER BY dir_path
"""
).data()
for dir_info in dir_structure:
dir_path = dir_info["dir_path"]
files = dir_info["files"]
if files:
f.write(f"\n**π {dir_path}/:**\n")
for file_info in files:
file_path = file_info["file"]
func_count = file_info["functions"]
class_count = file_info["classes"]
f.write(
f" - {file_path}: {func_count} functions, {class_count} classes\n"
)
# Cross-file relationships and potential dependencies
f.write("\n### Potential Cross-File Dependencies:\n")
f.write("*Note: Based on naming patterns and file structure analysis*\n\n")
# Files that might import each other (based on naming patterns)
potential_imports = session.run(
"""
MATCH (f1:File), (f2:File)
WHERE f1.path ENDS WITH '.py' AND f2.path ENDS WITH '.py' AND f1 <> f2
AND (
f1.path CONTAINS split(f2.path, '.')[0] OR
f2.path CONTAINS split(f1.path, '.')[0] OR
split(f1.path, '/')[0] = split(f2.path, '/')[0]
)
RETURN f1.path as file1, f2.path as file2
ORDER BY f1.path, f2.path
LIMIT 20
"""
).data()
if potential_imports:
f.write("#### Likely Import Relationships:\n")
for import_rel in potential_imports:
file1 = import_rel["file1"]
file2 = import_rel["file2"]
f.write(f"- {file1} βοΈ {file2}\n")
else:
f.write("#### No obvious import patterns detected from file names.\n")
# Function/Class name analysis for relationships
f.write("\n### Code Element Relationships:\n")
# Functions that might call each other (same file)
same_file_functions = session.run(
"""
MATCH (f:File)-[:CONTAINS]->(func1:Function)
MATCH (f)-[:CONTAINS]->(func2:Function)
WHERE func1 <> func2
RETURN f.path as file_path, collect(func1.name) as functions
ORDER BY f.path
"""
).data()
f.write("#### Functions in Same Files (Potential Call Relationships):\n")
for same_file in same_file_functions:
file_path = same_file["file_path"]
functions = same_file["functions"]
if len(functions) > 1:
f.write(
f"- **{file_path}**: {' β '.join(functions[:5])}{'...' if len(functions) > 5 else ''}\n"
)
# Classes and their potential methods
class_methods = session.run(
"""
MATCH (f:File)-[:CONTAINS]->(cls:Class)
MATCH (f)-[:CONTAINS]->(func:Function)
RETURN cls.name as class_name, cls.file as file_path, collect(func.name) as potential_methods
ORDER BY cls.file, cls.name
"""
).data()
f.write("\n#### Classes and Potential Methods:\n")
for class_method in class_methods:
class_name = class_method["class_name"]
file_path = class_method["file_path"]
methods = class_method["potential_methods"]
if methods:
f.write(
f"- **{class_name}** ({file_path}): {', '.join(methods[:5])}{'...' if len(methods) > 5 else ''}\n"
)
# Summary of relationships for AI agent
f.write("\n### Relationship Summary for AI Agent:\n")
total_contains = session.run(
"MATCH ()-[:CONTAINS]->() RETURN count(*) as count"
).single()["count"]
files_with_code = session.run(
"""
MATCH (f:File)-[:CONTAINS]->(item)
WHERE item:Function OR item:Class
RETURN count(DISTINCT f) as count
"""
).single()["count"]
f.write(f"- **Total containment relationships**: {total_contains}\n")
f.write(f"- **Files containing code elements**: {files_with_code}\n")
f.write(
f"- **Key insight**: Use file paths to understand module structure\n"
)
f.write(f"- **Key insight**: Functions in same file likely interact\n")
f.write(
f"- **Key insight**: Directory structure indicates logical grouping\n"
)
# NEW: DETAILED RELATIONSHIP ANALYSIS
f.write("\n## Detailed Relationship Analysis\n\n")
# Import relationships
f.write("### Import Dependencies:\n")
import_relationships = session.run(
"""
MATCH (f1:File)-[r:IMPORTS]->(f2:File)
RETURN f1.path as from_file, f2.path as to_file, r.name as import_name, r.type as import_type
ORDER BY from_file, to_file
"""
).data()
if import_relationships:
current_file = None
for import_rel in import_relationships:
from_file = import_rel["from_file"]
to_file = import_rel["to_file"]
import_name = import_rel["import_name"]
import_type = import_rel["import_type"]
if from_file != current_file:
current_file = from_file
f.write(f"\n**{from_file}:**\n")
f.write(
f" - imports `{import_name}` from {to_file} ({import_type})\n"
)
else:
f.write("No import relationships detected.\n")
# Function call relationships
f.write("\n### Function Call Relationships:\n")
function_calls = session.run(
"""
MATCH (caller)-[r:CALLS]->(call:FunctionCall)
RETURN caller.path as caller_path, r.function as callee_name, call.file as file_path
ORDER BY caller_path, callee_name
"""
).data()
if function_calls:
current_caller = None
for call_rel in function_calls:
caller_path = call_rel["caller_path"]
callee_name = call_rel["callee_name"]
file_path = call_rel["file_path"]
if caller_path != current_caller:
current_caller = caller_path
f.write(f"\n**{caller_path}:**\n")
f.write(f" - calls `{callee_name}()`\n")
else:
f.write("No function call relationships detected.\n")
# Class inheritance relationships
f.write("\n### Class Inheritance Hierarchy:\n")
inheritance_relationships = session.run(
"""
MATCH (child:Class)-[:INHERITS]->(parent:Class)
RETURN child.name as child_name, child.file as child_file,
parent.name as parent_name, parent.file as parent_file
ORDER BY child_file, child_name
"""
).data()
if inheritance_relationships:
for inherit_rel in inheritance_relationships:
child_name = inherit_rel["child_name"]
child_file = inherit_rel["child_file"]
parent_name = inherit_rel["parent_name"]
parent_file = inherit_rel["parent_file"]
f.write(
f"- **{child_name}** ({child_file}) inherits from **{parent_name}** ({parent_file})\n"
)
else:
f.write("No inheritance relationships detected.\n")
# Class methods relationships
f.write("\n### Class Methods:\n")
class_method_relationships = session.run(
"""
MATCH (cls:Class)-[:HAS_METHOD]->(method:Method)
RETURN cls.name as class_name, cls.file as class_file,
collect(method.name) as methods
ORDER BY class_file, class_name
"""
).data()
if class_method_relationships:
for class_methods in class_method_relationships:
class_name = class_methods["class_name"]
class_file = class_methods["class_file"]
methods = class_methods["methods"]
f.write(f"\n**{class_name}** ({class_file}):\n")
for method in methods:
f.write(f" - {method}()\n")
else:
f.write("No class method relationships detected.\n")
# Decorator relationships
f.write("\n### Decorators:\n")
decorator_relationships = session.run(
"""
MATCH (decorated)-[:DECORATED_BY]->(decorator:Decorator)
RETURN decorated.path as decorated_path, decorator.name as decorator_name
ORDER BY decorated_path
"""
).data()
if decorator_relationships:
current_decorated = None
for decorator_rel in decorator_relationships:
decorated_path = decorator_rel["decorated_path"]
decorator_name = decorator_rel["decorator_name"]
if decorated_path != current_decorated:
current_decorated = decorated_path
f.write(f"\n**{decorated_path}:**\n")
f.write(f" - @{decorator_name}\n")
else:
f.write("No decorator relationships detected.\n")
# Variable relationships
f.write("\n### Variables and Scope:\n")
variable_relationships = session.run(
"""
MATCH (scope)-[:DEFINES]->(var:Variable)
RETURN scope.path as scope_path, var.name as var_name, var.type as var_type
ORDER BY scope_path, var_type, var_name
"""
).data()
if variable_relationships:
current_scope = None
for var_rel in variable_relationships:
scope_path = var_rel["scope_path"]
var_name = var_rel["var_name"]
var_type = var_rel["var_type"]
if scope_path != current_scope:
current_scope = scope_path
f.write(f"\n**{scope_path}:**\n")
type_emoji = {
"module_variable": "π",
"class_attribute": "ποΈ",
"instance_variable": "π§",
"local_variable": "π",
"annotated_variable": "π",
}.get(var_type, "π")
f.write(f" - {type_emoji} {var_name} ({var_type})\n")
else:
f.write("No variable relationships detected.\n")