mohameden19961/codeguard
GitHub: mohameden19961/codeguard
CodeGuard 是一款集成十二项检查类别的 Python 静态分析与自动修复工具,帮助开发团队在 CI/CD 流水线中统一管控代码质量与安全风险。
Stars: 1 | Forks: 0
# 🛡️ CodeGuard
**Python 代码质量与安全分析工具**
*12 项检查 · 9 种输出格式 · 0.58秒 分析时间 · 20+ 贡献者*
[](LICENSE)
[](https://python.org)
[](https://github.com/mohameden19961/codeguard/actions)
[](https://mohameden19961.github.io/codeguard/)
[](https://github.com/mohameden19961/codeguard/releases)
[](https://github.com/mohameden19961/codeguard/stargazers)
```
$ codeguard analyze src/
[21:10:52] [INFO] Running 12 checks on 279 files
[21:10:52] [INFO] Analysis complete: 556 violations in 279 files (0.58s)
```
## 🔮 概述
**CodeGuard** 是一款全面的 Python 静态分析工具,内置 **12 项检查**,涵盖复杂度、安全性、代码风格、性能、文档、命名、导入、重复代码、类型注解、SSH 配置审计以及 SSH 密钥审计。它能生成 **9 种输出格式** 的丰富报告,并无缝集成到你的 CI/CD 流水线中。
```
pip install -e .
codeguard analyze src/
```
## 📊 项目指标
| 指标 | 数值 |
|--------|-------|
| 分析文件数 | 279 |
| 分析时间 | 0.58s |
| 检查类别 | 12 |
| 输出格式 | 9 |
| 总提交数 | 4,147 |
| 贡献者 | 20+ |
| License | MIT |
## ✨ 功能矩阵
| 类别 | 检测能力 | 输出 |
|----------|----------------------|--------|
| 🔍 **复杂度** | 圈复杂度、嵌套、函数长度、参数 | Terminal, JSON, HTML |
| 🛡️ **安全性** | SQLi、命令注入、路径遍历 | SARIF, HTML, JSON |
| 🎨 **代码风格** | 行长度、空格、命名、导入 | Terminal, HTML, Markdown |
| ⚡ **性能** | 嵌套循环、内存、缓慢导入 | JSON, CSV |
| 📚 **文档** | Module/function/class docstrings | HTML, Markdown |
| 🔐 **SSH 安全** | 配置审计、弱密钥、端口扫描 | Terminal, JSON |
| 🔄 **重复代码** | 代码克隆检测、相似度 | CSV, HTML |
| 🏷️ **类型注解** | 类型注解覆盖率、返回类型 | JUnit, XML |
## 🚀 快速开始
```
# 分析你的代码
codeguard analyze src/
# CI 模式 — 遇到 high+ 违规时失败
codeguard check src/ --severity high
# 生成 HTML 报告
codeguard analyze src/ --format html --output report.html
# 用于 GitHub Code Scanning 的 SARIF
codeguard analyze src/ --format sarif --output results.sarif
# 自动修复常见问题
codeguard fix src/ --fixers trailing_whitespace,line_endings
# 创建默认配置
codeguard init
```
## 📈 严重程度分布
```
%%{init: {'theme': 'dark', 'themeVariables': { 'pie1': '#ef4444', 'pie2': '#f59e0b', 'pie3': '#22d3ee' }}}%%
pie title Violations by Severity
"High (5)" : 5
"Medium (375)" : 375
"Low (176)" : 176
```
## 🔧 配置
```
# .codeguard.yml
verbose: false
severity_threshold: medium
max_workers: 4
checks_enabled: [complexity, style, security, performance, documentation, naming, imports, duplication, typing, ssh_config, ssh_keys, ssh_port]
complexity:
max_cyclomatic: 10
max_nesting: 4
max_lines_per_function: 50
max_parameters: 6
style:
max_line_length: 100
security:
level: high
check_sql_injection: true
check_path_traversal: true
check_command_injection: true
```
## 🧩 插件系统
```
# ~/.codeguard/plugins/my_check.py
from codeguard.checks.base import BaseCheck
from codeguard.core.types import Violation
class MyCheck(BaseCheck):
name = "my_check"
description = "My custom check"
def check(self, file_path, content, lines):
violations = []
# Your custom logic here
return violations
```
## 🔄 CI/CD 集成
### GitHub Actions
```
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with: {python-version: "3.11"}
- uses: ./.github/actions/codeguard
with:
path: src/
severity: medium
format: sarif
```
### Pre-commit Hook
```
repos:
- repo: https://github.com/mohameden19961/codeguard
rev: v0.2.0
hooks:
- id: codeguard
```
## 🧪 测试
```
pip install -e ".[dev]"
pytest tests/ -v --cov=src/codeguard
```
## 📁 项目结构
```
src/codeguard/
├── cli.py # CLI entry point
├── config.py # YAML configuration
├── core/
│ ├── engine.py # Analysis engine
│ ├── collector.py # File discovery
│ ├── types.py # Data models
│ ├── reporter.py # Report generation
│ └── formatter.py # Results formatting
├── checks/
│ ├── base.py # BaseCheck + CheckRegistry
│ ├── complexity.py # Cyclomatic complexity
│ ├── style.py # PEP 8 style
│ ├── security.py # Vulnerability scanning
│ ├── ssh.py # SSH config/key audit
│ └── ... # 10+ check modules
├── output/ # 9 output format writers
├── fixers/ # Auto-fix modules
├── utils/ # Cache, parallel, logging
└── plugins/ # Plugin system
```
## 📜 更新日志
请参阅 [CHANGELOG.md](CHANGELOG.md)
## 📄 License
**MIT** — 请参阅 [LICENSE](LICENSE)
**使用 ❤️ 基于 Python 构建** • 4,147 次提交 • 20+ 贡献者 • [GitHub](https://github.com/mohameden19961/codeguard)
[](https://github.com/mohameden19961/codeguard/stargazers)
[](https://github.com/mohameden19961/codeguard/forks)
标签:LNA, Python, SARIF, 弱口令爆破, 插件系统, 无后门, 模块化设计, 自动化修复, 调试插件, 逆向工具, 错误基检测, 静态代码分析