-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_analyzer.py
More file actions
90 lines (73 loc) · 2.97 KB
/
code_analyzer.py
File metadata and controls
90 lines (73 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import ollama
def analyze_code(code, language):
"""Analyze code with comprehensive feedback"""
prompt = f"""You are an expert senior software engineer conducting a thorough code review. Analyze this {language} code professionally.
Code to review:
```{language}
{code}
```
Provide a detailed review in the following format:
## 🐛 Bugs and Errors
[List any bugs, runtime errors, or logical issues. Be specific about line numbers if possible. If none found, say "No critical bugs detected."]
## ⚡ Performance Issues
[Identify performance bottlenecks, inefficient algorithms, or optimization opportunities. Include time/space complexity analysis where relevant. If none found, say "Performance looks good."]
## 🔒 Security Vulnerabilities
[Highlight security risks like SQL injection, XSS, hardcoded credentials, input validation issues, etc. If none found, say "No major security concerns detected."]
## ✨ Code Quality
[Comment on readability, maintainability, naming conventions, code structure, and documentation. Provide specific suggestions.]
## 📚 Best Practices
[Identify violations of language-specific best practices, design patterns that should be used, or industry standards not followed.]
## ✅ Positive Aspects
[Mention what's done well - good patterns, clean code, efficient solutions, etc.]
## 🎯 Recommendations
[Provide 3-5 actionable recommendations prioritized by importance.]
Be constructive, specific, and provide code examples where helpful."""
try:
response = ollama.chat(
model='llama3.2:3b',
messages=[{'role': 'user', 'content': prompt}]
)
return response['message']['content']
except Exception as e:
return f"❌ Error analyzing code: {str(e)}\n\nPlease ensure:\n1. Ollama is running (`ollama serve`)\n2. Model is installed (`ollama pull llama3.2:3b`)\n3. Your code is valid {language}"
def get_language_from_filename(filename):
"""Detect programming language from file extension"""
ext_map = {
'.py': 'Python',
'.js': 'JavaScript',
'.jsx': 'React (JSX)',
'.ts': 'TypeScript',
'.tsx': 'TypeScript React (TSX)',
'.java': 'Java',
'.cpp': 'C++',
'.cc': 'C++',
'.cxx': 'C++',
'.c': 'C',
'.h': 'C/C++ Header',
'.html': 'HTML',
'.htm': 'HTML',
'.css': 'CSS',
'.go': 'Go',
'.rs': 'Rust',
'.php': 'PHP',
'.rb': 'Ruby',
'.swift': 'Swift',
'.kt': 'Kotlin',
'.scala': 'Scala',
'.sql': 'SQL'
}
filename_lower = filename.lower()
for ext, lang in ext_map.items():
if filename_lower.endswith(ext):
return lang
return 'Unknown'
def get_severity_color(severity):
"""Get color code for severity level"""
colors = {
'critical': '🔴',
'high': '🟠',
'medium': '🟡',
'low': '🟢',
'info': '🔵'
}
return colors.get(severity.lower(), '⚪')