File size: 1,941 Bytes
c4d4675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Detect if the script is being sourced
(return 0 2>/dev/null) && sourced=1 || sourced=0

if [ $sourced -eq 0 ]; then
    echo "Error: This script needs to be sourced. Run:"
    echo "    source activate-env.sh"
    echo "    or"
    echo "    . activate-env.sh"
    exit 1
fi

# Check if Miniconda is installed
if ! command -v conda &> /dev/null; then
    echo "Miniconda is not installed. Please install Miniconda and try again."
    exit 1
fi

# Check if environment.yml exists
if [ ! -f "environment.yml" ]; then
    echo "environment.yml not found in the current directory. Please provide an environment.yml file."
    exit 1
fi

# Extract environment name from environment.yml
env_name=$(grep "^name:" environment.yml | awk '{print $2}')

if [ -z "$env_name" ]; then
    echo "Environment name not found in environment.yml. Please ensure the file has a 'name' field."
    exit 1
fi

# Check if Conda is initialized
if ! conda info &> /dev/null; then
    echo "Conda is not initialized. Run 'conda init' and restart your shell."
    exit 1
fi

# Check if the environment already exists
if conda env list | grep -q "^$env_name\s"; then
    echo "Activating existing environment: $env_name"
else
    echo "Environment $env_name not found. Creating it from environment.yml..."
    # Initialize conda for the shell
    eval "$(conda shell.bash hook)"
    
    conda env create -f environment.yml

    if [ $? -ne 0 ]; then
        echo "Failed to create the environment. Check your environment.yml for errors."
        exit 1
    fi

    echo "Environment $env_name created successfully."
fi

# Initialize conda and activate environment
. $(conda info --base)/etc/profile.d/conda.sh
conda activate "$env_name"

# Confirm activation (but don't exit since we're sourcing)
if [ "$CONDA_DEFAULT_ENV" = "$env_name" ]; then
    echo "Environment $env_name is now active."
else
    echo "Failed to activate environment $env_name."
    return 1
fi