Code Analyzer Skill
版本: 1.0.0
作者: sohot-gdjinni
标签: code-review, python, security, optimization
简介
一个专业的 Python 代码分析与优化 Skill,提供:
- 语法检查与结构分析
- 安全漏洞扫描
- 性能优化建议
- 重构后的可直接使用代码
功能特性
| 功能 | 说明 |
|---|
| 🔍 语法检查 | Python 语法验证、AST 结构分析 |
| 🛡️ 安全扫描 | 检测硬编码密钥、裸 except、SQL 注入等 |
| ⚡ 性能分析 | 识别低效循环、冗余计算、缓存机会 |
| 📊 代码质量 | 复杂度评估、重复代码检测 |
| ✅ 修复版本 | 提供可直接使用的优化后代码 |
使用方法
1. 直接分析代码
# 分析单个文件
python3 -m code_analyzer analyze /path/to/your/code.py
# 分析目录
python3 -m code_analyzer analyze /path/to/project/ --recursive
2. 在 Python 中使用
from code_analyzer import CodeAnalyzer
analyzer = CodeAnalyzer()
results = analyzer.analyze_file('your_code.py')
# 查看问题列表
for issue in results.issues:
print(f"[{issue.severity}] {issue.message}")
# 获取修复建议
fixed_code = results.get_fixed_code()
3. 作为 Agent Skill 使用
当你需要分析代码时,直接粘贴代码给我,我会:
- 语法检查 - 验证代码能否正常运行
- 问题检测 - 找出安全/性能/质量问题
- 分级报告 - P0(必须修复) / P1(重要) / P2(建议) / P3(可选)
- 提供修复 - 给出可直接使用的优化版本
示例输出
输入代码
def api_get(path):
import urllib.request
try:
req = urllib.request.Request('https://api.example.com' + path)
with urllib.request.urlopen(req) as resp:
return json.loads(resp.read())
except:
return None
分析结果
📊 代码分析报告
==================================================
函数数量: 1
类数量: 0
导入语句: 1
🔍 发现的问题:
⚠️ [P0] 使用裸 except: 可能隐藏所有异常
⚠️ [P1] 硬编码 API 地址
⚠️ [P1] 缺少超时设置
⚠️ [P2] 导入语句在函数内
✅ 优化建议:
1. 使用具体的异常类型 (HTTPError, URLError)
2. 添加 timeout 参数
3. 将 import 移到文件顶部
修复后代码
import json
import urllib.request
import urllib.error
from typing import Optional, Dict
API_BASE_URL = 'https://api.example.com'
DEFAULT_TIMEOUT = 15
def api_get(path: str, timeout: int = DEFAULT_TIMEOUT) -> Optional[Dict]:
"""发送 GET 请求"""
try:
req = urllib.request.Request(API_BASE_URL + path)
with urllib.request.urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read().decode('utf-8'))
except urllib.error.HTTPError as e:
print(f"HTTP错误: {e.code}")
return None
except urllib.error.URLError as e:
print(f"连接错误: {e.reason}")
return None
except json.JSONDecodeError as e:
print(f"JSON解析错误: {e}")
return None
检查规则
P0 - 必须修复 (安全/稳定性)
P1 - 重要 (可靠性)
P2 - 建议 (质量)
P3 - 可选 (风格)
安装
# 克隆仓库
git clone https://github.com/yourusername/code-analyzer-skill.git
cd code-analyzer-skill
# 安装依赖
pip install -r requirements.txt
# 可选:安装为系统命令
pip install -e .
配置
创建 .code_analyzer.yaml:
# 忽略的文件/目录
exclude:
- "*/venv/*"
- "*/__pycache__/*"
- "*/tests/*"
# 自定义规则
rules:
max_line_length: 120
max_function_lines: 50
max_complexity: 10
# 严重性覆盖
severity:
bare_except: "error" # 裸 except 升级为错误
missing_timeout: "warning" # 缺少超时降级为警告
工作原理
┌─────────────────────────────────────────┐
│ 代码输入 │
└─────────────────┬───────────────────────┘
▼
┌─────────────────────────────────────────┐
│ 1. 语法检查 (AST解析) │
│ - Python 语法验证 │
│ - 结构分析 │
└─────────────────┬───────────────────────┘
▼
┌─────────────────────────────────────────┐
│ 2. 静态分析 │
│ - 安全扫描 │
│ - 性能检测 │
│ - 代码异味 │
└─────────────────┬───────────────────────┘
▼
┌─────────────────────────────────────────┐
│ 3. 问题分级 │
│ - P0/P1/P2/P3 │
│ - 影响评估 │
└─────────────────┬───────────────────────┘
▼
┌─────────────────────────────────────────┐
│ 4. 生成修复 │
│ - 代码重构 │
│ - 类型提示 │
│ - 文档生成 │
└─────────────────────────────────────────┘
贡献
欢迎贡献!请遵循以下流程:
- Fork 仓库
- 创建功能分支 (
git checkout -b feature/amazing-feature)
- 提交更改 (
git commit -m 'Add amazing feature')
- 推送到分支 (
git push origin feature/amazing-feature)
- 创建 Pull Request
许可证
MIT License - 详见 LICENSE 文件
致谢
基于以下开源项目构建:
- Python AST 模块
- Bandit (安全扫描)
- Radon (复杂度分析)
更新日志
v1.0.0 (2026-03-21)
- 🎉 初始版本发布
- ✅ 基础语法检查
- ✅ 安全漏洞扫描
- ✅ 性能优化建议
- ✅ 修复代码生成