-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsfppy_studio.sh
More file actions
executable file
Β·242 lines (210 loc) Β· 6.51 KB
/
sfppy_studio.sh
File metadata and controls
executable file
Β·242 lines (210 loc) Β· 6.51 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
# =============================================================================
# SFPPy Studio Launcher
# Food Contact Migration Analysis Web Application
#
# Author: Olivier Vitrac, PhD, HDR
# Organization: INRAE + Generative Simulation
# =============================================================================
set -e
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
STUDIO_DIR="${SCRIPT_DIR}/studio"
DEFAULT_PORT=8002
DEFAULT_HOST="127.0.0.1"
# Print banner
print_banner() {
echo -e "${GREEN}"
echo " βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo " β β"
echo " β πβ©π SFPPy Studio - Food Contact Migration Analysis β"
echo " β β"
echo " βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo -e "${NC}"
}
# Print usage
usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -p, --port PORT Server port (default: ${DEFAULT_PORT})"
echo " -H, --host HOST Server host (default: ${DEFAULT_HOST})"
echo " -d, --debug Enable debug mode"
echo " -r, --reload Enable auto-reload for development"
echo " --check Check dependencies only"
echo " --kill Kill any running SFPPy Studio server"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Start on default port 8002"
echo " $0 -p 8080 # Start on port 8080"
echo " $0 -d -r # Start in debug mode with auto-reload"
echo " $0 --kill # Stop running server"
}
# Check Python dependencies
check_dependencies() {
echo -e "${BLUE}Checking dependencies...${NC}"
local missing=0
# Map package names to import names (some differ)
declare -A pkg_imports=(
["numpy"]="numpy"
["scipy"]="scipy"
["fastapi"]="fastapi"
["uvicorn"]="uvicorn"
["pydantic"]="pydantic"
["jinja2"]="jinja2"
["pyyaml"]="yaml" # pyyaml is imported as 'yaml'
)
for pkg in numpy scipy fastapi uvicorn pydantic jinja2 pyyaml; do
import_name="${pkg_imports[$pkg]}"
if python3 -c "import ${import_name}" 2>/dev/null; then
echo -e " β ${pkg}"
else
echo -e " ${RED}β ${pkg} (missing)${NC}"
missing=1
fi
done
# Check optional dependencies
for pkg in openpyxl reportlab pillow; do
if python3 -c "import ${pkg}" 2>/dev/null; then
echo -e " β ${pkg} (optional)"
else
echo -e " ${YELLOW}β ${pkg} (optional, for exports)${NC}"
fi
done
if [ $missing -eq 1 ]; then
echo -e "\n${RED}Missing required dependencies. Install with:${NC}"
echo " pip install numpy scipy fastapi uvicorn pydantic jinja2 pyyaml"
return 1
fi
echo -e "${GREEN}All required dependencies available.${NC}"
return 0
}
# Kill running server
kill_server() {
echo -e "${YELLOW}Stopping SFPPy Studio servers...${NC}"
# Find and kill processes
local pids=$(pgrep -f "uvicorn.*studio.app.main:app" 2>/dev/null || true)
if [ -n "$pids" ]; then
echo "Found processes: $pids"
kill $pids 2>/dev/null || true
sleep 1
# Force kill if still running
kill -9 $pids 2>/dev/null || true
echo -e "${GREEN}Server stopped.${NC}"
else
echo "No running SFPPy Studio server found."
fi
# Also kill by port
local port_pids=$(lsof -t -i:${DEFAULT_PORT} 2>/dev/null || true)
if [ -n "$port_pids" ]; then
echo "Killing process on port ${DEFAULT_PORT}: $port_pids"
kill $port_pids 2>/dev/null || true
fi
}
# Check if port is available
check_port() {
local port=$1
if lsof -i:${port} > /dev/null 2>&1; then
echo -e "${RED}Port ${port} is already in use.${NC}"
echo "Use --kill to stop existing server, or -p to specify another port."
return 1
fi
return 0
}
# Start the server
start_server() {
local host=$1
local port=$2
local debug=$3
local reload=$4
print_banner
echo -e "${BLUE}Starting SFPPy Studio...${NC}"
echo " Host: ${host}"
echo " Port: ${port}"
echo " Debug: ${debug}"
echo " Auto-reload: ${reload}"
echo ""
# Check port availability
check_port ${port} || exit 1
# Change to SFPPy root
cd "${SCRIPT_DIR}"
# Build uvicorn command
local cmd="python3 -m uvicorn studio.app.main:app --host ${host} --port ${port}"
if [ "${reload}" = "true" ]; then
cmd="${cmd} --reload --reload-dir studio"
fi
if [ "${debug}" = "true" ]; then
cmd="${cmd} --log-level debug"
else
cmd="${cmd} --log-level info"
fi
echo -e "${GREEN}Server starting at http://${host}:${port}${NC}"
echo -e "${YELLOW}Press Ctrl+C to stop${NC}"
echo ""
# Run the server
exec ${cmd}
}
# Parse arguments
HOST="${DEFAULT_HOST}"
PORT="${DEFAULT_PORT}"
DEBUG="false"
RELOAD="false"
CHECK_ONLY="false"
KILL_SERVER="false"
while [[ $# -gt 0 ]]; do
case $1 in
-p|--port)
PORT="$2"
shift 2
;;
-H|--host)
HOST="$2"
shift 2
;;
-d|--debug)
DEBUG="true"
shift
;;
-r|--reload)
RELOAD="true"
shift
;;
--check)
CHECK_ONLY="true"
shift
;;
--kill)
KILL_SERVER="true"
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
usage
exit 1
;;
esac
done
# Execute requested action
if [ "${KILL_SERVER}" = "true" ]; then
kill_server
exit 0
fi
if [ "${CHECK_ONLY}" = "true" ]; then
check_dependencies
exit $?
fi
# Normal startup
check_dependencies || exit 1
echo ""
start_server "${HOST}" "${PORT}" "${DEBUG}" "${RELOAD}"