-
-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathstart.command
More file actions
118 lines (101 loc) · 3.9 KB
/
Copy pathstart.command
File metadata and controls
118 lines (101 loc) · 3.9 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
#!/usr/bin/env bash
# =========================================================
# Move to script directory
# =========================================================
cd "$(dirname "$0")" || { echo "[ERROR] Failed to switch to the script directory."; exit 1; }
# =========================================================
# Config
# =========================================================
APP_FILE="app.py"
REQUIREMENTS_FILE="requirements.txt"
VENV_DIR=".venv"
PORT=5001
PIP_MIRROR="https://pypi.tuna.tsinghua.edu.cn/simple"
PY_CMD=""
# =========================================================
# Detect Python
# =========================================================
echo "[INFO] Detecting Python..."
for cmd in python3 python; do
if command -v "$cmd" &>/dev/null; then
version=$("$cmd" -c "import sys; print(sys.version_info.major)" 2>/dev/null)
if [ "$version" = "3" ]; then
PY_CMD="$cmd"
break
fi
fi
done
if [ -z "$PY_CMD" ]; then
echo "[ERROR] Python 3.8+ is required."
echo "[TIP] Please install Python from: https://www.python.org/downloads/"
open "https://www.python.org/downloads/"
exit 1
fi
echo "[INFO] Python command: $PY_CMD"
"$PY_CMD" --version
if [ $? -ne 0 ]; then
echo "[ERROR] Python is detected but cannot run."
exit 1
fi
# =========================================================
# Create venv if missing
# =========================================================
if [ ! -f "$VENV_DIR/bin/python" ]; then
echo "[INFO] Creating virtual environment..."
"$PY_CMD" -m venv "$VENV_DIR" || { echo "[ERROR] Failed to create virtual environment."; exit 1; }
fi
if [ ! -f "$VENV_DIR/bin/activate" ]; then
echo "[ERROR] Virtual environment activation file missing."
exit 1
fi
source "$VENV_DIR/bin/activate" || { echo "[ERROR] Failed to activate virtual environment."; exit 1; }
PY_CMD="python"
# =========================================================
# Upgrade pip if needed
# =========================================================
if ! "$PY_CMD" -m pip --version &>/dev/null; then
echo "[INFO] Bootstrapping pip..."
"$PY_CMD" -m ensurepip --upgrade || { echo "[ERROR] pip is not available and cannot be bootstrapped."; exit 1; }
fi
# =========================================================
# Install dependencies
# =========================================================
echo "[INFO] Checking and installing dependencies..."
if [ -f "$REQUIREMENTS_FILE" ]; then
"$PY_CMD" -m pip install --upgrade pip
[ $? -ne 0 ] && echo "[WARN] pip upgrade failed, continuing..."
"$PY_CMD" -m pip install -r "$REQUIREMENTS_FILE"
if [ $? -ne 0 ]; then
echo "[WARN] Retry installing dependencies with mirror..."
"$PY_CMD" -m pip install -r "$REQUIREMENTS_FILE" -i "$PIP_MIRROR"
[ $? -ne 0 ] && { echo "[ERROR] Failed to install dependencies from requirements.txt."; exit 1; }
fi
else
echo "[WARN] requirements.txt not found. No dependency install performed."
fi
# =========================================================
# Check app file
# =========================================================
if [ ! -f "$APP_FILE" ]; then
echo "[ERROR] Entry file not found: $APP_FILE"
exit 1
fi
# =========================================================
# Check port
# =========================================================
if lsof -iTCP:"$PORT" -sTCP:LISTEN &>/dev/null; then
PID=$(lsof -ti TCP:"$PORT" -sTCP:LISTEN)
echo "[ERROR] Port $PORT is already in use. PID=$PID"
echo "[TIP] Use: ps -p $PID to inspect the process"
exit 1
fi
# =========================================================
# Start app
# =========================================================
echo "[INFO] Starting application..."
echo "[INFO] Open: http://127.0.0.1:$PORT"
"$PY_CMD" "$APP_FILE"
RET=$?
[ $RET -ne 0 ] && echo "[ERROR] Application exited with code $RET"
read -rp "Press Enter to exit..."
exit $RET