-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
163 lines (139 loc) · 4.21 KB
/
setup
File metadata and controls
163 lines (139 loc) · 4.21 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
#!/usr/bin/env bash
# 错误处理
set -e
# 通用变量
ENV_NAME="bug-knowledge"
# 智能检测可用的Python命令
if command -v python3 &> /dev/null; then
PYTHON_CMD="python3"
elif command -v python &> /dev/null; then
PYTHON_CMD="python"
else
echo "Error: Neither python3 nor python command found"
exit 1
fi
PYTHON_MIN_VERSION="3.8"
# 判断操作系统类型
is_windows() {
[[ "$OSTYPE" == "msys"* ]] || [[ "$OSTYPE" == "cygwin"* ]] || [[ "$OSTYPE" == "win"* ]]
}
# 检查Python版本
check_python_version() {
if ! command -v $PYTHON_CMD &> /dev/null; then
echo "Error: Python not found"
exit 1
fi
version=$($PYTHON_CMD -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
if (( $(echo "$version < $PYTHON_MIN_VERSION" | bc -l) )); then
echo "Error: Python $PYTHON_MIN_VERSION or higher is required (current: $version)"
exit 1
fi
}
# 清理现有环境
clean_environment() {
# 检查是否在conda环境中
if [[ -n "$CONDA_PREFIX" ]]; then
if is_windows; then
cmd //c "conda deactivate"
else
conda deactivate
fi
# 如果存在conda环境,删除它
if command -v conda &> /dev/null && conda env list | grep -q "$ENV_NAME"; then
echo "Removing existing conda environment..."
if is_windows; then
cmd //c "conda env remove -n $ENV_NAME -y"
else
conda env remove -n $ENV_NAME -y
fi
fi
fi
# 检查是否在venv环境中并退出
if [[ -n "$VIRTUAL_ENV" ]]; then
deactivate 2>/dev/null || true
fi
}
# 检查并激活环境
activate_environment() {
if command -v conda &> /dev/null; then
# 尝试激活conda环境
if ! conda activate $ENV_NAME 2>/dev/null; then
echo "Error: $ENV_NAME conda environment not found. Please run ./setup first"
exit 1
fi
else
# 尝试激活venv环境
if is_windows; then
VENV_ACTIVATE=".venv/Scripts/activate"
else
VENV_ACTIVATE=".venv/bin/activate"
fi
if [ ! -f "$VENV_ACTIVATE" ]; then
echo "Error: Python virtual environment not found. Please run ./setup first"
exit 1
fi
source "$VENV_ACTIVATE"
fi
}
# 使用Python venv创建虚拟环境
setup_with_venv() {
echo "Setting up with Python venv..."
# 如果venv不存在才创建新环境
if [ ! -d ".venv" ]; then
if is_windows; then
$PYTHON_CMD -m venv .venv
else
$PYTHON_CMD -m venv .venv
fi
fi
# 激活环境
if is_windows; then
source .venv/Scripts/activate
else
source .venv/bin/activate
fi
python -m pip install --upgrade pip
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
echo "Warning: requirements.txt not found"
fi
echo "Python venv setup complete!"
if is_windows; then
echo "To activate the environment, run: source .venv/Scripts/activate"
else
echo "To activate the environment, run: source .venv/bin/activate"
fi
}
# 使用conda创建环境
setup_with_conda() {
echo "Setting up with conda..."
if is_windows; then
echo "Windows environment detected"
cmd //c "conda env create -f environment.yml"
cmd //c "conda activate $ENV_NAME"
else
echo "Linux/Unix environment detected"
conda env create -f environment.yml
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate $ENV_NAME
fi
echo "Conda environment setup complete!"
echo "To activate the environment, run: conda activate $ENV_NAME"
}
# 主要安装流程
echo "Checking Python version..."
check_python_version
# 首先清理现有环境
clean_environment
# 检查conda是否可用并选择安装方式
if command -v conda &> /dev/null; then
echo "Conda found, using conda for environment setup..."
setup_with_conda
else
echo "Conda not found, falling back to Python venv..."
setup_with_venv
fi
# 创建必要的目录
mkdir -p logs data/annoy
echo "Environment setup complete!"