-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·163 lines (139 loc) · 4.32 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·163 lines (139 loc) · 4.32 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 -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
usage() {
cat <<'EOF'
Usage:
./install.sh [--with-browser] [--skip-browser] [--recreate-venv]
Options:
--with-browser Force install Playwright Chromium (downloads ~100MB+)
--skip-browser Skip Playwright browser check/install
--recreate-venv Delete and recreate ./venv
EOF
}
WITH_BROWSER=0
SKIP_BROWSER=0
RECREATE_VENV=0
for arg in "$@"; do
case "$arg" in
--with-browser) WITH_BROWSER=1 ;;
--skip-browser) SKIP_BROWSER=1 ;;
--recreate-venv) RECREATE_VENV=1 ;;
-h|--help) usage; exit 0 ;;
*)
echo "❌ Unknown argument: $arg"
usage
exit 2
;;
esac
done
if [[ ! -f "requirements.txt" ]] || [[ ! -f "main.py" ]]; then
echo "❌ Please run this script in the project root (needs requirements.txt + main.py)."
exit 1
fi
check_python_version() {
"$1" -c 'import sys; raise SystemExit(0 if (3,8) <= sys.version_info[:2] < (3,13) else 1)' >/dev/null 2>&1
}
choose_python() {
for cmd in python3 python; do
if command -v "$cmd" >/dev/null 2>&1; then
if check_python_version "$cmd"; then
echo "$cmd"
return 0
fi
fi
done
return 1
}
if [[ "$RECREATE_VENV" == "1" ]] && [[ -d "venv" ]]; then
echo "🗑️ Removing existing venv/ ..."
rm -rf venv
fi
VENV_PY="venv/bin/python"
if [[ -x "$VENV_PY" ]]; then
if ! check_python_version "$VENV_PY"; then
echo "❌ Existing venv uses unsupported Python version: $("$VENV_PY" -V 2>&1)"
echo "💡 Please recreate venv with Python 3.11/3.12:"
echo " ./install.sh --recreate-venv"
exit 1
fi
else
PYTHON_CMD="$(choose_python || true)"
if [[ -z "${PYTHON_CMD:-}" ]]; then
echo "❌ Python 3.8–3.12 not found. Please install Python 3.11/3.12 (recommended) then re-run."
exit 1
fi
echo "✅ Using Python: $PYTHON_CMD ($($PYTHON_CMD -V 2>&1))"
echo "🐍 Creating venv/ ..."
"$PYTHON_CMD" -m venv venv
fi
echo "✅ Using venv: $VENV_PY ($($VENV_PY -V 2>&1))"
export PYTHONUTF8=1
export PIP_DISABLE_PIP_VERSION_CHECK=1
export PLAYWRIGHT_BROWSERS_PATH="${PLAYWRIGHT_BROWSERS_PATH:-$HOME/.xhs_system/ms-playwright}"
PIP_ARGS=(--timeout 120 --retries 3 --prefer-binary)
echo "📦 Upgrading pip ..."
"$VENV_PY" -m pip install --upgrade pip setuptools wheel "${PIP_ARGS[@]}" || true
echo "📦 Installing dependencies ..."
"$VENV_PY" -m pip install -r requirements.txt "${PIP_ARGS[@]}"
echo "✅ Verifying imports ..."
"$VENV_PY" -c "import PyQt5; import sqlalchemy; import playwright; print('ok')"
check_playwright() {
"$VENV_PY" - <<'PY'
import sys
try:
from playwright.sync_api import sync_playwright
except Exception:
sys.exit(1)
def main() -> int:
with sync_playwright() as p:
try:
browser = p.chromium.launch(headless=True, timeout=30_000)
browser.close()
return 0
except Exception as e:
msg = str(e)
if "Executable doesn't exist" not in msg and "not found" not in msg.lower() and "找不到" not in msg:
return 1
for channel in ("chrome", "msedge"):
try:
browser = p.chromium.launch(channel=channel, headless=True, timeout=30_000)
browser.close()
return 0
except Exception:
continue
return 2
if __name__ == "__main__":
sys.exit(main())
PY
}
if [[ "$SKIP_BROWSER" == "1" ]]; then
:
elif [[ "$WITH_BROWSER" == "1" ]]; then
echo "🌐 Installing Playwright Chromium ..."
echo " PLAYWRIGHT_BROWSERS_PATH=$PLAYWRIGHT_BROWSERS_PATH"
if [[ -z "${PLAYWRIGHT_DOWNLOAD_HOST:-}" ]]; then
echo " Tip (CN): export PLAYWRIGHT_DOWNLOAD_HOST=https://npmmirror.com/mirrors/playwright"
fi
"$VENV_PY" -m playwright install chromium
else
set +e
check_playwright
PW_RC=$?
set -e
if [[ "$PW_RC" != "0" ]]; then
echo "🌐 Playwright browser not available yet; installing Chromium ..."
echo " PLAYWRIGHT_BROWSERS_PATH=$PLAYWRIGHT_BROWSERS_PATH"
if [[ -z "${PLAYWRIGHT_DOWNLOAD_HOST:-}" ]]; then
echo " Tip (CN): export PLAYWRIGHT_DOWNLOAD_HOST=https://npmmirror.com/mirrors/playwright"
fi
"$VENV_PY" -m playwright install chromium || true
fi
fi
echo
echo "🎉 Done."
echo "Start:"
echo " ./启动程序.sh"
echo " # or"
echo " $VENV_PY main.py"