-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.py
More file actions
569 lines (473 loc) · 21.7 KB
/
visualizer.py
File metadata and controls
569 lines (473 loc) · 21.7 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
#!/usr/bin/env python3
import os
import sys
import json
import ast
import re
import webbrowser
import threading
import time
from flask import Flask, jsonify, send_from_directory, Response
from flask_cors import CORS
from queue import Queue
app = Flask(__name__, static_folder='frontend/dist', static_url_path='')
CORS(app)
# Store the initial path for graph generation
INITIAL_PATH = None
# Global progress tracking
progress_clients = []
current_progress = {'status': 'idle', 'message': '', 'percentage': 0}
def load_gitignore_patterns(path):
"""Load patterns from .gitignore file."""
gitignore_path = os.path.join(path, '.gitignore')
patterns = []
if os.path.exists(gitignore_path):
try:
with open(gitignore_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
patterns.append(line.rstrip('/'))
except Exception as e:
print(f"Error reading .gitignore: {e}")
return patterns
def scan_directory(path, ignore_patterns=None):
"""Scan directory for files, excluding ignored patterns and filtering by allowed extensions."""
if ignore_patterns is None:
# Default patterns plus from .gitignore
default_patterns = ['.git', 'node_modules', '__pycache__', '.vscode', 'dist', 'build', '.husky', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', '.npmrc', '.yarnrc', '.prettierrc', '.eslintrc', 'prettier.config.js', 'eslint.config.js', '.prettierignore', '.editorconfig']
gitignore_patterns = load_gitignore_patterns(path)
ignore_patterns = default_patterns + gitignore_patterns
# Only allow these file extensions
allowed_extensions = ['.js', '.json', '.jsx', '.tsx', '.jpg', '.ts', '.py', '.c']
files = []
for root, dirs, files_list in os.walk(path):
# Remove ignored directories
dirs[:] = [d for d in dirs if not any(d == pattern or d.startswith(pattern + '/') for pattern in ignore_patterns)]
for file in files_list:
# Skip files starting with .
if file.startswith('.'):
continue
# Only include files with allowed extensions
if not any(file.endswith(ext) for ext in allowed_extensions):
continue
if not any(pattern in os.path.join(root, file) for pattern in ignore_patterns):
full_path = os.path.join(root, file)
# Skip binary files or very large files (except for small images)
if os.path.getsize(full_path) < 1024 * 1024: # 1MB limit
files.append(full_path)
return files
def parse_python_dependencies(file_path):
"""Parse Python file for import dependencies."""
dependencies = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
tree = ast.parse(content, filename=file_path)
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
# Convert module name to potential file path
dep = alias.name.replace('.', '/') + '.py'
dependencies.append(dep)
elif isinstance(node, ast.ImportFrom):
if node.module:
dep = node.module.replace('.', '/') + '.py'
dependencies.append(dep)
except Exception as e:
print(f"Error parsing {file_path}: {e}")
return dependencies
def parse_html_dependencies(file_path):
"""Parse HTML file for link/script/img dependencies."""
dependencies = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Match href in link tags, src in script/img tags
patterns = [
r'<link[^>]*href=["\']([^"\']+)["\']',
r'<script[^>]*src=["\']([^"\']+)["\']',
r'<img[^>]*src=["\']([^"\']+)["\']',
r'<source[^>]*src=["\']([^"\']+)["\']',
r'<iframe[^>]*src=["\']([^"\']+)["\']'
]
for pattern in patterns:
matches = re.findall(pattern, content, re.IGNORECASE)
for match in matches:
if match.startswith('./') or match.startswith('../') or (not match.startswith('http') and not match.startswith('//') and not match.startswith('data:')):
dependencies.append(match)
except Exception as e:
print(f"Error parsing {file_path}: {e}")
return dependencies
def parse_css_dependencies(file_path):
"""Parse CSS file for @import and url() dependencies."""
dependencies = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Match @import and url()
patterns = [
r'@import\s+["\']([^"\']+)["\']',
r'url\(["\']?([^"\']+)["\']?\)'
]
for pattern in patterns:
matches = re.findall(pattern, content)
for match in matches:
if match.startswith('./') or match.startswith('../') or (not match.startswith('http') and not match.startswith('//') and not match.startswith('data:')):
dependencies.append(match)
except Exception as e:
print(f"Error parsing {file_path}: {e}")
return dependencies
def parse_json_dependencies(file_path):
"""Parse JSON file for relative path references."""
dependencies = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Find all quoted strings that look like relative paths
matches = re.findall(r'["\']((?:\./|\.\./)[^"\']+)["\']', content)
for match in matches:
dependencies.append(match)
except Exception as e:
print(f"Error parsing {file_path}: {e}")
return dependencies
def parse_svg_dependencies(file_path):
"""Parse SVG file for image href references."""
dependencies = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Match image href in SVG
matches = re.findall(r'<image[^>]*href=["\']([^"\']+)["\']', content, re.IGNORECASE)
for match in matches:
if match.startswith('./') or match.startswith('../') or (not match.startswith('http') and not match.startswith('//') and not match.startswith('data:')):
dependencies.append(match)
except Exception as e:
print(f"Error parsing {file_path}: {e}")
return dependencies
def parse_md_dependencies(file_path):
"""Parse Markdown file for image references."""
dependencies = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Match  and <img src="path">
patterns = [
r'!\[.*?\]\(([^)]+)\)',
r'<img[^>]*src=["\']([^"\']+)["\']'
]
for pattern in patterns:
matches = re.findall(pattern, content, re.IGNORECASE)
for match in matches:
if match.startswith('./') or match.startswith('../') or (not match.startswith('http') and not match.startswith('//') and not match.startswith('data:')):
dependencies.append(match)
except Exception as e:
print(f"Error parsing {file_path}: {e}")
return dependencies
def parse_general_dependencies(file_path):
"""Parse any text file for relative path references."""
dependencies = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Find all relative paths like ./path or ../path
matches = re.findall(r'["\']?((?:\./|\.\./)[^"\']*?)["\']?', content)
for match in matches:
# Clean up the match (remove quotes if present)
path = match.strip('"\'')
if path and not path.startswith('http') and not path.startswith('//') and not path.startswith('data:'):
dependencies.append(path)
except Exception as e:
# Silently ignore binary files or encoding errors
pass
return dependencies
def parse_js_dependencies(file_path):
"""Parse JavaScript/TypeScript file for import dependencies."""
dependencies = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Match import statements
import_patterns = [
r'import\s+.*?\s+from\s+[\'"]([^\'"]+)[\'"]',
r'import\s+[\'"]([^\'"]+)[\'"]',
r'require\s*\(\s*[\'"]([^\'"]+)[\'"]\s*\)',
r'import\s*\(\s*[\'"]([^\'"]+)[\'"]\s*\)' # Dynamic imports
]
for pattern in import_patterns:
matches = re.findall(pattern, content)
for match in matches:
# Convert relative imports to file paths
if match.startswith('./') or match.startswith('../'):
dep = match
if '.' not in os.path.basename(dep):
dep += '.js' # Assume .js if no extension
dependencies.append(dep)
except Exception as e:
print(f"Error parsing {file_path}: {e}")
return dependencies
def update_progress(status, message, percentage=0):
"""Update global progress and notify all SSE clients."""
global current_progress
current_progress = {'status': status, 'message': message, 'percentage': percentage}
# Send update to all SSE clients
for client in progress_clients[:]: # Copy list to avoid modification during iteration
try:
client.put(f"data: {json.dumps(current_progress)}\n\n")
except:
# Remove disconnected clients
if client in progress_clients:
progress_clients.remove(client)
def build_graph(files, root_path):
"""Build graph data by searching for file references across all files."""
nodes = []
edges = []
update_progress('scanning', 'Scanning directory for files...', 10)
# Sort files by relative path for better layout grouping
sorted_files = sorted(files, key=lambda f: os.path.relpath(f, root_path))
# Create nodes with calculated positions
for index, file_path in enumerate(sorted_files):
rel_path = os.path.relpath(file_path, root_path)
file_ext = os.path.splitext(file_path)[1].lower()
# Calculate position with sufficient spacing (6 per row, larger gaps)
row = index // 6
col = index % 6
x = col * 280 # Increased horizontal spacing
y = row * 180 # Increased vertical spacing
node = {
'id': rel_path,
'label': os.path.basename(file_path),
'type': file_ext,
'path': rel_path,
'position': {'x': x, 'y': y}
}
nodes.append(node)
update_progress('reading', 'Reading file contents...', 30)
# Read all file contents
file_contents = {}
for file_path in files:
try:
with open(file_path, 'r', encoding='utf-8') as f:
file_contents[file_path] = f.read()
except Exception:
# Skip binary files or encoding errors
file_contents[file_path] = ''
update_progress('analyzing', 'Analyzing file relationships...', 50)
# For each target file, search all source files for references
total_files = len(files)
for i, target_path in enumerate(files):
if i % 5 == 0: # Progress every 5 files for more frequent updates
progress_pct = 50 + int((i / total_files) * 40) # 50-90% range
update_progress('analyzing', f'Processing file {i+1}/{total_files}: {os.path.basename(target_path)}', progress_pct)
target_rel = os.path.relpath(target_path, root_path)
target_name = os.path.basename(target_path)
target_path_no_ext = target_rel.rsplit('.', 1)[0] if '.' in target_rel else target_rel
# Also include dotted versions for Python-style imports
target_rel_dotted = target_rel.replace('/', '.')
target_path_no_ext_dotted = target_path_no_ext.replace('/', '.')
# Also include @ alias versions (assuming @ points to src)
target_rel_at = target_rel.replace('src/', '@/', 1) if target_rel.startswith('src/') else None
target_path_no_ext_at = target_path_no_ext.replace('src/', '@/', 1) if target_path_no_ext.startswith('src/') else None
search_terms = [target_name, target_rel, target_path_no_ext, target_rel_dotted, target_path_no_ext_dotted]
if target_rel_at:
search_terms.extend([target_rel_at, target_path_no_ext_at])
for source_path in files:
if source_path != target_path:
source_rel = os.path.relpath(source_path, root_path)
content = file_contents[source_path]
# Also compute relative path from source to target
source_dir = os.path.dirname(source_path)
try:
rel_import = os.path.relpath(target_path, source_dir)
# Normalize to use forward slashes
rel_import = rel_import.replace(os.sep, '/')
# For same directory, make it ./filename
if not rel_import.startswith('.'):
rel_import = './' + rel_import
search_terms.append(rel_import)
# Also add without extension
rel_import_no_ext = rel_import.rsplit('.', 1)[0] if '.' in rel_import else rel_import
search_terms.append(rel_import_no_ext)
except ValueError:
# Paths on different drives, skip
pass
# Check if any reference to the target file appears in source content
if any(term in content for term in search_terms):
edges.append({
'from': source_rel,
'to': target_rel
})
update_progress('complete', f'Analysis complete! Found {len(nodes)} files and {len(edges)} connections.', 100)
return {
'metadata': {
'project_path': root_path,
'project_name': os.path.basename(os.path.abspath(root_path)),
'generated_at': time.time(),
'file_count': len(nodes),
'connection_count': len(edges)
},
'nodes': nodes,
'edges': edges
}
def resolve_dependency(dep, current_file, file_map):
"""Resolve a dependency path to an actual file in the map."""
current_dir = os.path.dirname(current_file)
if dep.startswith('./') or dep.startswith('../'):
# Relative import
full_path = os.path.normpath(os.path.join(current_dir, dep))
# Try different extensions if no extension provided
if '.' not in os.path.basename(full_path):
for ext in ['.js', '.ts', '.jsx', '.tsx', '.py', '.html', '.css']:
test_path = full_path + ext
if test_path in file_map:
return test_path
else:
if full_path in file_map:
return full_path
elif dep.startswith('@/'):
# @ alias (commonly points to src directory)
rel_dep = dep[2:] # Remove @/
# Assume @ points to src directory
src_path = os.path.join('src', rel_dep)
full_path = os.path.normpath(src_path)
# Try different extensions if no extension provided
if '.' not in os.path.basename(full_path):
for ext in ['.js', '.ts', '.jsx', '.tsx', '.py', '.html', '.css']:
test_path = full_path + ext
if test_path in file_map:
return test_path
else:
if full_path in file_map:
return full_path
elif dep.startswith('/'):
# Absolute path (from HTML base)
rel_dep = dep[1:]
full_path = os.path.normpath(os.path.join(current_dir, rel_dep))
if full_path in file_map:
return full_path
return None
@app.route('/api/graph')
def get_graph():
path = sys.argv[1] if len(sys.argv) > 1 else '.'
abs_path = os.path.abspath(path)
if not os.path.exists(abs_path):
return jsonify({'error': 'Directory not found'}), 404
files = scan_directory(abs_path)
graph = build_graph(files, abs_path)
print(f"Generated graph with {len(graph['nodes'])} nodes and {len(graph['edges'])} edges")
# Save graph data to JSON file in frontend public directory
graph_path = 'frontend/dist/graph.json'
with open(graph_path, 'w') as f:
json.dump(graph, f, indent=2)
print(f"Graph data saved to {graph_path}")
return jsonify(graph)
@app.route('/')
def index():
return send_from_directory('frontend/dist', 'index.html')
@app.route('/<path:path>')
def static_files(path):
return send_from_directory('frontend/dist', path)
@app.route('/graph.json')
def serve_graph_json():
"""Serve the generated graph JSON file, regenerating if needed."""
# Regenerate graph for the specified directory
abs_path = os.path.abspath(INITIAL_PATH)
if not os.path.exists(abs_path):
return jsonify({'error': 'Directory not found'}), 404
files = scan_directory(abs_path)
graph = build_graph(files, abs_path)
# Save updated graph data
with open('graph.json', 'w') as f:
json.dump(graph, f, indent=2)
try:
return send_from_directory('.', 'graph.json', mimetype='application/json')
except FileNotFoundError:
return jsonify({'error': 'Graph generation failed'}), 500
@app.route('/api/reindex')
def reindex_codebase():
"""Force regeneration of the graph for the current project."""
graph_path = 'frontend/dist/graph.json'
if not os.path.exists(graph_path):
return jsonify({'error': 'No existing graph to reindex'}), 404
# Load existing metadata to get the path
try:
with open(graph_path, 'r') as f:
existing_data = json.load(f)
project_path = existing_data.get('metadata', {}).get('project_path', '.')
except:
return jsonify({'error': 'Could not read existing graph'}), 500
abs_path = os.path.abspath(project_path)
if not os.path.exists(abs_path):
return jsonify({'error': 'Project directory no longer exists'}), 404
files = scan_directory(abs_path)
graph = build_graph(files, abs_path)
# Save updated graph data
with open(graph_path, 'w') as f:
json.dump(graph, f, indent=2)
return jsonify({'status': 'reindexed', 'nodes': len(graph['nodes']), 'edges': len(graph['edges'])})
@app.route('/api/open-in-vscode/<path:file_path>')
def open_in_vscode(file_path):
"""Open a file in VS Code editor."""
try:
# Get the project path from the graph.json file (same one the frontend uses)
graph_path = 'graph.json'
if os.path.exists(graph_path):
with open(graph_path, 'r') as f:
existing_data = json.load(f)
project_path = existing_data.get('metadata', {}).get('project_path', '.')
else:
# Fallback to INITIAL_PATH if graph.json doesn't exist
global INITIAL_PATH
project_path = INITIAL_PATH if INITIAL_PATH else '.'
abs_project_path = os.path.abspath(project_path)
abs_file_path = os.path.join(abs_project_path, file_path)
# Verify the file exists and is within the project directory
if not os.path.exists(abs_file_path):
return jsonify({'error': f'File not found: {abs_file_path}'}), 404
# Ensure the file is within the project directory for security
if not os.path.abspath(abs_file_path).startswith(os.path.abspath(abs_project_path)):
return jsonify({'error': 'Access denied'}), 403
# Open the file in VS Code
import subprocess
result = subprocess.run(['code', abs_file_path], capture_output=True, text=True)
if result.returncode != 0:
print(f"VS Code command failed: {result.stderr}")
return jsonify({'error': f'Failed to open in VS Code: {result.stderr}'}), 500
return jsonify({'status': 'opened', 'file': file_path, 'project_path': project_path})
except Exception as e:
print(f"Error opening file in VS Code: {e}")
return jsonify({'error': str(e)}), 500
@app.route('/progress')
def progress():
"""Server-Sent Events endpoint for progress updates."""
def generate():
q = Queue()
progress_clients.append(q)
try:
# Send current progress immediately
yield f"data: {json.dumps(current_progress)}\n\n"
# Keep connection alive and send updates
while True:
try:
message = q.get(timeout=30) # 30 second timeout
yield message
except:
# Send keepalive
yield f"data: {json.dumps({'status': 'keepalive'})}\n\n"
except GeneratorExit:
# Client disconnected
if q in progress_clients:
progress_clients.remove(q)
return Response(generate(), mimetype='text/event-stream')
def open_browser():
time.sleep(1) # Wait for server to start
webbrowser.open('http://localhost:5000')
if __name__ == '__main__':
print("Starting Codebase Visualizer...")
# Set the initial path for graph generation
INITIAL_PATH = sys.argv[1] if len(sys.argv) > 1 else '.'
print(f"Configured to analyze path: {INITIAL_PATH}")
print("Open http://localhost:5000 in your browser")
# Open browser in a separate thread
threading.Thread(target=open_browser).start()
app.run(debug=True, host='0.0.0.0', port=5000)