Skip to content

Commit 8092b4b

Browse files
committed
Install: Add *nix shell scripts to install conda and mhkit-python
1 parent c8ac987 commit 8092b4b

6 files changed

Lines changed: 543 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
# MHKiT-MATLAB {{ script_title }}
4+
# Generated from template - DO NOT EDIT DIRECTLY
5+
# Report issues: {{ support.github_issues }}
6+
7+
set -e # Exit on any error
8+
9+
# Script configuration
10+
CONDA_ENV_NAME="{{ conda.environment_name }}"
11+
PYTHON_VERSION="{{ python.install_version }}"
12+
MHKIT_VERSION="{{ mhkit_python.version }}"
13+
GITHUB_ISSUES="{{ support.github_issues }}"
14+
15+
# Logging functions
16+
log_info() {
17+
echo "[INFO] $1"
18+
}
19+
20+
log_success() {
21+
echo "[SUCCESS] $1"
22+
}
23+
24+
log_warning() {
25+
echo "[WARNING] $1"
26+
}
27+
28+
log_error() {
29+
echo "[ERROR] $1"
30+
echo "[ERROR] Please report this issue: $GITHUB_ISSUES"
31+
}
32+
33+
# Function to expand path with environment variables and ~
34+
expand_path() {
35+
local path="$1"
36+
# Expand ~ to $HOME
37+
path="${path/#\~/$HOME}"
38+
# Expand environment variables
39+
path=$(eval echo "$path")
40+
echo "$path"
41+
}
42+
43+
# Function to test if a conda installation works
44+
test_conda_installation() {
45+
local conda_path="$1"
46+
log_info "Testing conda at: $conda_path"
47+
48+
# Test if the conda executable exists and is executable
49+
if [[ -x "$conda_path" ]]; then
50+
# Test if conda info works (this is the most reliable test)
51+
if "$conda_path" info >/dev/null 2>&1; then
52+
local conda_version=$("$conda_path" --version 2>/dev/null || echo "unknown")
53+
local conda_location=$("$conda_path" info --base 2>/dev/null || echo "unknown")
54+
log_success "Working conda found!"
55+
log_info " Version: $conda_version"
56+
log_info " Location: $conda_location"
57+
return 0
58+
else
59+
log_warning "Conda executable found but 'conda info' failed"
60+
return 1
61+
fi
62+
else
63+
return 1
64+
fi
65+
}
66+
67+
# OS Detection
68+
OS_TYPE=$(uname)
69+
ARCH_TYPE=$(uname -m)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{% set script_title = "Step 1: Conda Detection" %}
2+
{% include "_header.sh.j2" %}
3+
4+
# Function to check if conda is available
5+
detect_conda() {
6+
log_info "Starting comprehensive conda detection..."
7+
8+
# Step 1: Check if conda is already in PATH and working
9+
if command -v conda >/dev/null 2>&1; then
10+
log_info "Found conda in PATH"
11+
if test_conda_installation "$(command -v conda)"; then
12+
echo "CONDA_DETECTED=true"
13+
echo "CONDA_PATH=$(command -v conda)"
14+
return 0
15+
else
16+
log_warning "Conda in PATH is not working properly"
17+
fi
18+
fi
19+
20+
# Step 2: Check environment variables
21+
{% for env_var in conda.detection.environment_variables %}
22+
if [[ -n "${{ '${' }}{{ env_var }}:-}" ]]; then
23+
log_info "Checking environment variable {{ env_var }}: ${{ '${' }}{{ env_var }}}"
24+
local conda_from_env=""
25+
if [[ "{{ env_var }}" == "CONDA_EXE" ]]; then
26+
conda_from_env="${{ '${' }}{{ env_var }}}"
27+
elif [[ "{{ env_var }}" == "CONDA_PREFIX" ]]; then
28+
conda_from_env="${{ '${' }}{{ env_var }}}/bin/conda"
29+
fi
30+
31+
if [[ -n "$conda_from_env" ]]; then
32+
conda_from_env=$(expand_path "$conda_from_env")
33+
if test_conda_installation "$conda_from_env"; then
34+
echo "CONDA_DETECTED=true"
35+
echo "CONDA_PATH=$conda_from_env"
36+
return 0
37+
fi
38+
fi
39+
fi
40+
{% endfor %}
41+
42+
# Step 3: Check all common installation paths systematically
43+
log_info "Checking common conda installation paths..."
44+
local paths_to_check=(
45+
{% for path in conda.detection.common_paths.unix %}
46+
"{{ path }}"
47+
{% endfor %}
48+
)
49+
50+
for path in "${paths_to_check[@]}"; do
51+
local expanded_path=$(expand_path "$path")
52+
if test_conda_installation "$expanded_path"; then
53+
echo "CONDA_DETECTED=true"
54+
echo "CONDA_PATH=$expanded_path"
55+
return 0
56+
fi
57+
done
58+
59+
log_info "No working conda installation found"
60+
echo "CONDA_DETECTED=false"
61+
return 1
62+
}
63+
64+
main() {
65+
log_info "Step 1: Detecting conda installation..."
66+
detect_conda
67+
}
68+
69+
main "$@"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{% set script_title = "Step 2: Install Miniconda" %}
2+
{% include "_header.sh.j2" %}
3+
4+
# Function to install miniconda
5+
install_conda() {
6+
log_info "Installing miniconda..."
7+
8+
if [[ "$OS_TYPE" == "Darwin" ]]; then
9+
if [[ "$ARCH_TYPE" == "arm64" ]]; then
10+
MINICONDA_URL="{{ conda.miniconda_urls.mac_arm }}"
11+
else
12+
MINICONDA_URL="{{ conda.miniconda_urls.mac_intel }}"
13+
fi
14+
elif [[ "$OS_TYPE" == "Linux" ]]; then
15+
MINICONDA_URL="{{ conda.miniconda_urls.linux }}"
16+
else
17+
log_error "Unsupported operating system: $OS_TYPE"
18+
exit 1
19+
fi
20+
21+
# Create miniconda directory
22+
mkdir -p ~/miniconda3
23+
24+
# Download miniconda installer
25+
log_info "Downloading miniconda installer..."
26+
if command -v curl >/dev/null 2>&1; then
27+
curl -L "$MINICONDA_URL" -o ~/miniconda3/miniconda.sh
28+
elif command -v wget >/dev/null 2>&1; then
29+
wget "$MINICONDA_URL" -O ~/miniconda3/miniconda.sh
30+
else
31+
log_error "Neither curl nor wget found. Please install one of them first."
32+
exit 1
33+
fi
34+
35+
# Install miniconda
36+
log_info "Installing miniconda to ~/miniconda3..."
37+
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
38+
39+
# Remove installer
40+
rm ~/miniconda3/miniconda.sh
41+
42+
# Initialize conda for future shells
43+
~/miniconda3/bin/conda init bash >/dev/null 2>&1 || true
44+
45+
log_success "Miniconda installed successfully"
46+
47+
# Output conda path for next steps
48+
echo "CONDA_PATH=$HOME/miniconda3/bin/conda"
49+
}
50+
51+
main() {
52+
log_info "Step 2: Installing miniconda..."
53+
install_conda
54+
}
55+
56+
main "$@"
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{% set script_title = "Step 3: Create Conda Environment" %}
2+
{% include "_header.sh.j2" %}
3+
4+
# Function to check if conda environment exists
5+
check_conda_env() {
6+
local conda_path="$1"
7+
if "$conda_path" env list | grep -q "^$CONDA_ENV_NAME "; then
8+
log_success "Found existing conda environment: $CONDA_ENV_NAME"
9+
echo "ENV_EXISTS=true"
10+
return 0
11+
else
12+
log_info "Conda environment '$CONDA_ENV_NAME' not found, will create it"
13+
echo "ENV_EXISTS=false"
14+
return 1
15+
fi
16+
}
17+
18+
# Function to create conda environment
19+
create_conda_env() {
20+
local conda_path="$1"
21+
log_info "Creating conda environment '$CONDA_ENV_NAME' with Python $PYTHON_VERSION..."
22+
23+
# Accept conda Terms of Service to avoid interactive prompts
24+
log_info "Accepting conda Terms of Service..."
25+
"$conda_path" config --set solver libmamba 2>/dev/null || true
26+
27+
# Try to accept TOS (may fail on older conda versions, that's OK)
28+
"$conda_path" tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main 2>/dev/null || true
29+
"$conda_path" tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r 2>/dev/null || true
30+
31+
# Create environment using conda-forge to minimize TOS issues
32+
"$conda_path" create -n "$CONDA_ENV_NAME" python="$PYTHON_VERSION" -c conda-forge -y
33+
34+
if [ $? -eq 0 ]; then
35+
log_success "Conda environment created successfully"
36+
echo "ENV_CREATED=true"
37+
else
38+
log_error "Failed to create conda environment"
39+
echo "ENV_CREATED=false"
40+
exit 1
41+
fi
42+
}
43+
44+
# Function to verify Python version in environment
45+
verify_python_version() {
46+
local conda_path="$1"
47+
log_info "Verifying Python version in environment..."
48+
49+
local py_version=$("$conda_path" run -n "$CONDA_ENV_NAME" python --version 2>&1 | cut -d' ' -f2)
50+
local major_minor=$(echo "$py_version" | cut -d'.' -f1,2)
51+
local expected_major_minor=$(echo "$PYTHON_VERSION" | cut -d'.' -f1,2)
52+
53+
if [[ "$major_minor" == "$expected_major_minor" ]]; then
54+
log_success "Python version verified: $py_version"
55+
echo "PYTHON_VERSION_OK=true"
56+
return 0
57+
else
58+
log_warning "Python version mismatch. Expected: $PYTHON_VERSION, Got: $py_version"
59+
log_info "Environment will need to be recreated..."
60+
echo "PYTHON_VERSION_OK=false"
61+
return 1
62+
fi
63+
}
64+
65+
main() {
66+
local conda_path="$1"
67+
if [[ -z "$conda_path" ]]; then
68+
log_error "No conda path provided. Usage: $0 <conda_path>"
69+
exit 1
70+
fi
71+
72+
log_info "Step 3: Managing conda environment..."
73+
log_info "Using conda at: $conda_path"
74+
75+
# Add conda to PATH for this session
76+
local conda_dir=$(dirname "$conda_path")
77+
export PATH="$conda_dir:$PATH"
78+
79+
if ! check_conda_env "$conda_path"; then
80+
create_conda_env "$conda_path"
81+
fi
82+
83+
if ! verify_python_version "$conda_path"; then
84+
log_info "Recreating environment with correct Python version..."
85+
"$conda_path" remove -n "$CONDA_ENV_NAME" --all -y >/dev/null 2>&1 || true
86+
create_conda_env "$conda_path"
87+
verify_python_version "$conda_path"
88+
fi
89+
}
90+
91+
main "$@"

0 commit comments

Comments
 (0)