-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathoptimize_code.sh
More file actions
75 lines (63 loc) · 2.2 KB
/
optimize_code.sh
File metadata and controls
75 lines (63 loc) · 2.2 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
#!/bin/bash
# Backtrader 代码优化脚本
# 使用 pyupgrade, ruff 等工具优化代码风格和格式
set -e
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_DIR"
echo "=========================================="
echo "Backtrader 代码优化工具"
echo "=========================================="
echo ""
# 检查必要的工具
check_tool() {
if ! command -v $1 &> /dev/null; then
echo "❌ 错误: 未找到 $1"
echo "请运行: pip install $2"
exit 1
fi
}
echo "📋 检查依赖工具..."
check_tool "python" "python3"
python -c "import pyupgrade" 2>/dev/null || (echo "❌ 缺少 pyupgrade"; exit 1)
python -c "import ruff" 2>/dev/null || (echo "❌ 缺少 ruff"; exit 1)
python -c "import black" 2>/dev/null || (echo "❌ 缺少 black"; exit 1)
python -c "import isort" 2>/dev/null || (echo "❌ 缺少 isort"; exit 1)
echo "✅ 所有依赖工具已安装"
echo ""
# 步骤 1: 使用 pyupgrade 升级 Python 语法
echo "🔧 步骤 1: 使用 pyupgrade 升级 Python 语法..."
python -m pyupgrade --py311-plus backtrader/**/*.py --exit-zero-even-if-changed
echo "✅ pyupgrade 完成"
echo ""
# 步骤 2: 使用 isort 规范导入顺序
echo "🔧 步骤 2: 使用 isort 规范导入顺序..."
python -m isort backtrader/
echo "✅ isort 完成"
echo ""
# 步骤 3: 使用 black 格式化代码
echo "🔧 步骤 3: 使用 black 格式化代码..."
python -m black backtrader/ --line-length 100
echo "✅ black 完成"
echo ""
# 步骤 4: 使用 ruff 进行 linting 并自动修复
echo "🔧 步骤 4: 使用 ruff 进行 linting 并自动修复..."
python -m ruff check backtrader/ --fix
echo "✅ ruff check 完成"
echo ""
# 步骤 5: 更新安装 backtrader
echo "📦 步骤 5: 更新安装 backtrader..."
pip install -U .
echo "✅ backtrader 更新完成"
echo ""
# 步骤 6: 运行全部测试验证
echo "🧪 步骤 6: 运行全部测试验证代码完整性..."
if [ -d "tests" ]; then
python -m pytest tests -n 8 --tb=short -q
echo "✅ 所有测试通过"
else
echo "⚠️ 未找到测试目录"
fi
echo ""
echo "=========================================="
echo "✅ 代码优化完成!"
echo "=========================================="