-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStart_Linux.sh
More file actions
executable file
·184 lines (162 loc) · 6.42 KB
/
Copy pathStart_Linux.sh
File metadata and controls
executable file
·184 lines (162 loc) · 6.42 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env bash
# First run: installs everything into a local .venv, checks USB/serial
# perms (one-time sudo prompts), then launches.
# Every run after: sees .venv already exists, skips straight to launch.
#
# Usage:
# bash Start_Linux.sh - normal launch, log panel + terminal output
# bash Start_Linux.sh --no-log - skip writing to logs/, terminal output only
set -e
cd "$(dirname "${BASH_SOURCE[0]}")"
VENV_DIR=".venv"
APP_DIR=".app_internal"
REQ_FILE="$APP_DIR/requirements.txt"
if [ ! -f "$APP_DIR/main.py" ]; then
echo "[ERROR] $APP_DIR/main.py not found."
echo "This launcher must stay in the same folder as the $APP_DIR folder."
exit 1
fi
install() {
echo "===================================================="
echo "Multiplex Solar Simulator - First-Time Setup"
echo "===================================================="
echo "This only happens once. Please wait..."
echo ""
# --- Python check ---
PYEXE=""
if command -v python3 &>/dev/null; then
PYEXE="python3"
elif command -v python &>/dev/null; then
PYEXE="python"
fi
if [ -z "$PYEXE" ]; then
echo "[ERROR] No Python interpreter found (tried 'python3' and 'python')."
echo "Install Python 3.10+ and re-run."
exit 1
fi
PY_VERSION=$("$PYEXE" -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
echo "[INFO] Using interpreter: $PYEXE (Python $PY_VERSION)"
# --- venv module check ---
if ! "$PYEXE" -c "import venv" &>/dev/null; then
echo "[ERROR] The 'venv' module is not available for $PYEXE."
echo "On Debian/Ubuntu, install it with:"
echo " sudo apt install python3-venv"
echo "Then re-run this script."
exit 1
fi
# --- Virtual environment ---
if [ ! -d "$VENV_DIR" ]; then
echo "[INFO] Creating local virtual environment..."
"$PYEXE" -m venv "$VENV_DIR"
fi
if [ ! -f "$VENV_DIR/bin/activate" ]; then
echo "[ERROR] Virtual environment creation failed ($VENV_DIR/bin/activate not found)."
echo "Try running this command manually to see the actual error:"
echo " $PYEXE -m venv $VENV_DIR"
exit 1
fi
source "$VENV_DIR/bin/activate"
# --- Python packages ---
echo "[INFO] Installing Python dependencies. This may take a few minutes..."
pip install --upgrade pip -q
if ! pip install -r "$REQ_FILE" -q; then
echo "[ERROR] Failed to install Python dependencies."
exit 1
fi
# --- Hardware access check (one-time, interactive) ---
echo ""
echo "=== Hardware Backend Check ==="
CURRENT_USER="$(id -un)"
IN_DIALOUT=0
if groups "$CURRENT_USER" | grep -qw dialout; then
IN_DIALOUT=1
fi
if [ "$IN_DIALOUT" -eq 1 ]; then
echo "[INFO] '$CURRENT_USER' is in the 'dialout' group -- relay serial access OK."
else
echo "[INFO] '$CURRENT_USER' is NOT in the 'dialout' group."
echo " Without this, the Numato relay will fail with a permission error."
read -p "Add '$CURRENT_USER' to the 'dialout' group now? (requires sudo, one-time) (y/n): " add_dialout
if [[ "$add_dialout" =~ ^[Yy]$ ]]; then
sudo usermod -aG dialout "$CURRENT_USER"
echo "[INFO] Added. Log out and back in (or run 'newgrp dialout') for it to take effect."
else
echo "[INFO] Skipped. See DEPLOYMENT.md to do this manually later."
fi
fi
echo ""
LIBUSB_FOUND=0
if ldconfig -p 2>/dev/null | grep -q "libusb-1.0.so"; then
LIBUSB_FOUND=1
elif [ -f "/usr/lib/x86_64-linux-gnu/libusb-1.0.so.0" ] || [ -f "/usr/local/lib/libusb-1.0.so" ]; then
LIBUSB_FOUND=1
fi
if [ "$LIBUSB_FOUND" -eq 1 ]; then
echo "[INFO] libusb-1.0 found -- Keithley USB access via pyvisa-py should work."
else
echo "[INFO] libusb-1.0 was not found in standard system locations."
read -p "Install libusb-1.0-0 now via apt? (requires sudo) (y/n): " install_libusb
if [[ "$install_libusb" =~ ^[Yy]$ ]]; then
sudo apt install -y libusb-1.0-0
else
echo "[INFO] Skipped. Install manually later with: sudo apt install libusb-1.0-0"
fi
fi
UDEV_RULE_PATH="/etc/udev/rules.d/99-keithley.rules"
echo ""
if [ -f "$UDEV_RULE_PATH" ]; then
echo "[INFO] Keithley udev rule already present at $UDEV_RULE_PATH."
else
echo "[INFO] No udev rule found for the Keithley 2460."
read -p "Create the udev rule now? (requires sudo, one-time) (y/n): " add_udev
if [[ "$add_udev" =~ ^[Yy]$ ]]; then
echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="05e6", ATTRS{idProduct}=="2460", MODE="0666"' \
| sudo tee "$UDEV_RULE_PATH" >/dev/null
sudo udevadm control --reload-rules
sudo udevadm trigger
echo "[INFO] Rule installed. Unplug and replug the Keithley's USB cable."
else
echo "[INFO] Skipped. See DEPLOYMENT.md to do this manually later."
fi
fi
mkdir -p logs
echo ""
echo "===================================================="
echo "[SUCCESS] Installation complete. Launching the app now..."
echo "===================================================="
}
# ========================================================================
# HEALTH CHECK -- only install if the venv is missing
# ========================================================================
if [ ! -f "$VENV_DIR/bin/activate" ]; then
install
else
echo "[INFO] Environment already set up -- skipping install."
source "$VENV_DIR/bin/activate"
fi
# ========================================================================
# LAUNCH
# ========================================================================
PYEXE=""
if command -v python3 &>/dev/null; then
PYEXE="python3"
elif command -v python &>/dev/null; then
PYEXE="python"
fi
if [ -z "$PYEXE" ]; then
echo "[ERROR] Python not found in the virtual environment."
echo "Delete the .venv folder and re-run this script."
exit 1
fi
echo "[INFO] Launching Multiplex Solar Simulator..."
pushd "$APP_DIR" >/dev/null
if [ "$1" = "--no-log" ]; then
shift
"$PYEXE" -u main.py "$@"
else
mkdir -p ../logs
LOGFILE="../logs/run_$(date +%Y%m%d_%H%M%S).log"
echo "[INFO] Logging this session to logs/$(basename "$LOGFILE")"
"$PYEXE" -u main.py "$@" 2>&1 | tee "$LOGFILE"
fi
popd >/dev/null