-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·277 lines (242 loc) · 12.2 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·277 lines (242 loc) · 12.2 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash
# setup.sh: OS-Specific Setup and Dependency Installation
# This script detects the operating system, installs necessary system dependencies (like gfortran),
# sets up a Python virtual environment, and builds the UCLCHEM chemical engine backend.
# It ensures that the environment is ready for running the GUI application.
# New users should run this script before trying to launch the GUI with run_gui.sh.
set -eu # Fail fast on errors or unbound variables
# ── Environment choice ──────────────────────────────────────────────────────
echo ""
echo "==> How would you like to set up the Python environment?"
echo " [1] conda — recommended for macOS (M1/M2/Intel) and Linux (uses conda-forge gfortran)"
echo " [2] venv — standard Python virtual environment (uses system/Homebrew gfortran)"
echo ""
read -p " Enter choice (1 or 2): " env_choice
if [ "$env_choice" = "1" ]; then
# Verify conda is installed
if ! command -v conda &> /dev/null; then
echo "Error: conda not found. Install Miniconda first:"
echo " https://docs.conda.io/en/latest/miniconda.html"
exit 1
fi
CONDA_ENV="uclchem_osx"
# Detect OS and architecture
_OS="$(uname -s)"
_ARCH="$(uname -m)"
# Reject native Windows — use WSL instead
case "${_OS}" in
CYGWIN*|MINGW*|MSYS*)
echo "Error: Native Windows is not supported. Please use WSL (Linux) instead."
exit 1
;;
esac
# Determine conda subdir (macOS only — Linux picks the right subdir automatically)
if [ "${_OS}" = "Darwin" ]; then
if [ "${_ARCH}" = "arm64" ]; then
CONDA_SUBDIR="osx-arm64"
else
CONDA_SUBDIR="osx-64"
fi
else
CONDA_SUBDIR=""
fi
# Source conda so activate works inside this script
# set +u because conda's own activation scripts use unbound variables
set +u
CONDA_BASE="$(conda info --base)"
source "${CONDA_BASE}/etc/profile.d/conda.sh"
# Create the environment only if it does not already exist
if conda env list | grep -qE "^${CONDA_ENV}[[:space:]]"; then
echo "==> Conda environment '${CONDA_ENV}' already exists — reusing it."
else
echo "==> Creating conda environment '${CONDA_ENV}'..."
conda create -n "${CONDA_ENV}" -y
fi
conda activate "${CONDA_ENV}"
set -u
# Pin the platform subdir (macOS only — Linux inherits the correct subdir automatically)
if [ -n "${CONDA_SUBDIR}" ]; then
_CUR_SUBDIR="$(conda config --env --show subdir 2>/dev/null | awk '{print $2}' || true)"
if [ "${_CUR_SUBDIR}" != "${CONDA_SUBDIR}" ]; then
echo "==> Setting conda subdir to ${CONDA_SUBDIR}..."
conda config --env --set subdir "${CONDA_SUBDIR}"
else
echo "==> conda subdir already set to ${CONDA_SUBDIR}."
fi
fi
# Install Python 3.12 if not already present in this env
# Check the env's own bin dir — avoids a false match against the base env's Python
if [ -f "${CONDA_BASE}/envs/${CONDA_ENV}/bin/python3.12" ]; then
echo "==> Python 3.12 already installed in '${CONDA_ENV}'."
else
echo "==> Installing Python 3.12 via conda..."
set +u
conda install python=3.12 -y
set -u
fi
# Install gfortran if not already present in this env
if [ -f "${CONDA_BASE}/envs/${CONDA_ENV}/bin/gfortran" ]; then
echo "==> gfortran already installed in '${CONDA_ENV}'."
else
echo "==> Installing gfortran via conda-forge..."
set +u
if [ "${_OS}" = "Darwin" ] && [ "${_ARCH}" = "arm64" ]; then
# Apple Silicon
conda install -c conda-forge gfortran -y
elif [ "${_OS}" = "Darwin" ]; then
# Apple Intel — also needs clang
conda install clang gfortran -y
else
# Linux — conda-forge gfortran only (no clang needed)
conda install -c conda-forge gfortran -y
fi
set -u
fi
# Check UCLCHEM directory
echo "==> Checking for UCLCHEM directory..."
if [ ! -d "UCLCHEM" ]; then
echo "Error: UCLCHEM directory not found. Please clone the UCLCHEM repository first as per the README."
echo "git clone https://github.com/uclchem/UCLCHEM.git"
exit 1
fi
# Install GUI dependencies into the conda env
echo "==> Upgrading pip and installing GUI dependencies..."
python -m pip install --upgrade pip
python -m pip install --upgrade -r requirements.txt
# Build UCLCHEM only if not already installed at the target version
UCLCHEM_TARGET="4.0.0rc4"
echo "==> Checking Backend Chemical Engine (UCLCHEM v4.0.0-rc4)..."
if pip show uclchem 2>/dev/null | grep -q "Version: ${UCLCHEM_TARGET}"; then
echo "==> UCLCHEM ${UCLCHEM_TARGET} already installed — skipping build."
else
echo "==> Building the Backend Chemical Engine (UCLCHEM v4.0.0-rc4)..."
cd UCLCHEM
if [ -n "$(git status --porcelain -uno)" ]; then
echo "==> Warning: UCLCHEM has uncommitted changes."
echo " These will be lost when switching to v4.0.0-rc4."
read -p " Continue and overwrite? (y/N) " confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
echo "==> Aborted. Please commit or stash your changes first."
exit 1
fi
fi
git checkout -f v4.0.0-rc4
python -m pip install --no-cache-dir .
cd ..
fi
echo "==> Setup complete!"
echo " In each new terminal, run: conda activate ${CONDA_ENV}"
echo " Then launch the app with: ./run_gui.sh (choose option 1)"
exit 0
fi
# ── END CONDA PATH — original venv code below is untouched ──────────────────
echo "==> Detecting Operating System..."
OS="$(uname -s)" # Detects the operating system type
# Handle OS-specific dependency installation
case "${OS}" in
# In case of Linux or WSL, we need to check for gfortran and install it if missing
# We also check for apt to ensure we can install dependencies on Debian/Ubuntu-based systems
Linux*)
echo "==> Detected Linux / WSL."
if ! command -v gfortran &> /dev/null; then # Check if gfortran is installed
if ! command -v apt &> /dev/null; then # Check if apt is available (for Debian/Ubuntu-based systems)
echo "Error: apt not found. Install make, python3-venv, and gfortran manually."
exit 1
fi
# If gfortran is missing, we need to install it along with other dependencies. This requires sudo privileges
echo "==> gfortran is missing. Installing system dependencies (requires sudo)..."
sudo apt update # Update package lists before installing
sudo apt install -y make python3-pip python3-venv gfortran # Install necessary system dependencies
else # If gfortran is already installed, we can skip the installation step
echo "==> System dependencies (gfortran) already installed."
fi
;;
# In case of macOS, we check for gfortran and install it via Homebrew if it's missing
# We also check for Homebrew itself before trying to use it
Darwin*)
echo "==> Detected macOS."
if ! command -v gfortran &> /dev/null; then # Check if gfortran is installed
echo "==> gfortran is missing. Installing via Homebrew..."
if ! command -v brew &> /dev/null; then # Check if Homebrew is installed
echo "Error: Homebrew is not installed. Please install Homebrew first."
exit 1
fi
brew install gcc # Install gfortran via Homebrew (gcc package includes gfortran)
else # If gfortran is already installed, we can skip the installation step
echo "==> System dependencies (gfortran) already installed."
fi
;;
# Detects native Windows environments and rejects them
# since UCLCHEM is not officially supported on native Windows
CYGWIN*|MINGW*|MSYS*)
echo "==> Detected Windows (Native)."
echo "Error: UCLCHEM is not officially supported on native Windows."
echo "Please install Windows Subsystem for Linux (WSL) and run this script from inside the WSL terminal."
exit 1
;;
*)
# If the OS is not recognized, we cannot proceed with the setup because we don't know how to handle dependencies
echo "Error: Unsupported Operating System: ${OS}"
exit 1
;;
esac
# ------------------------end of OS-specific handling------------------------
# After handling OS-specific dependencies, we proceed with the common setup steps
# that are the same across all supported operating systems
# Check for UCLCHEM directory before proceeding, since it's required for building the backend chemical engine
echo "==> Checking for UCLCHEM directory..."
if [ ! -d "UCLCHEM" ]; then
echo "Error: UCLCHEM directory not found. Please clone the UCLCHEM repository first as per the README."
echo "git clone https://github.com/uclchem/UCLCHEM.git" # command for cloning the UCLCHEM repository
exit 1
fi
# Check for Python 3.12 before proceeding, since it's required for the GUI and backend chemical engine
echo "==> Checking for Python 3.12..."
if command -v python3.12 &> /dev/null; then # Check if there is a python3.12 installation command
PYTHON_CMD="python3.12" # Install Python 3.12
elif command -v python3 &> /dev/null && python3 --version 2>&1 | grep -q " 3\.12"; then # Check if there is a python3 command that points to Python 3.12
PYTHON_CMD="python3" # Use python3 if it points to Python 3.12
else # The right Python version is not found, so we cannot proceed with the setup
echo "Error: Python 3.12 is required but not found."
exit 1
fi
# Set up the Python virtual environment and install dependencies
echo "==> Checking virtual environment..."
if [ ! -d "venv" ]; then # Check if the 'venv' is already created
echo "==> Creating virtual environment..."
$PYTHON_CMD -m venv venv # Create a new virtual environment
# If the 'venv' directory already exists, we assume the virtual environment is already set up and skip creating it again.
else
echo "==> Virtual environment already exists."
echo " Only upgrading packages. For a clean install, delete the 'venv' folder first."
fi
# Activate the virtual environment and install the required Python packages for the GUI and backend chemical engine
echo "==> Activating virtual environment..."
source venv/bin/activate # Activate the virtual environment
echo "==> Upgrading pip and installing GUI dependencies..."
python -m pip install --upgrade pip # Upgrade pip to the latest version to ensure we can install all dependencies properly
python -m pip install --upgrade -r requirements.txt # Install or upgrade the required Python packages for the GUI as specified in requirements.txt
# Build UCLCHEM only if not already installed at the target version
UCLCHEM_TARGET="4.0.0rc4"
echo "==> Checking Backend Chemical Engine (UCLCHEM v4.0.0-rc4)..."
if pip show uclchem 2>/dev/null | grep -q "Version: ${UCLCHEM_TARGET}"; then
echo "==> UCLCHEM ${UCLCHEM_TARGET} already installed — skipping build."
else
echo "==> Building the Backend Chemical Engine (UCLCHEM v4.0.0-rc4)..."
cd UCLCHEM
# Warn if uncommitted changes (-uno ignores untracked files)
if [ -n "$(git status --porcelain -uno)" ]; then # Check for uncommitted changes in the UCLCHEM repository
echo "==> Warning: UCLCHEM has uncommitted changes."
echo " These will be lost when switching to v4.0.0-rc4."
read -p " Continue and overwrite? (y/N) " confirm # Ask user to confirm if they want to continue and overwrite uncommitted changes
# If the user does not confirm, we abort the setup to prevent loss of uncommitted changes
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
echo "==> Aborted. Please commit or stash your changes first."
exit 1
fi
fi
git checkout -f v4.0.0-rc4
python -m pip install --no-cache-dir .
cd ..
fi
echo "==> Setup complete! You can now launch the application using ./run_gui.sh"