|
| 1 | +{% set script_title = "Step 3: Create Conda Environment" %} |
| 2 | +{% include "_header.ps1.j2" %} |
| 3 | + |
| 4 | +# Function to check if conda environment exists |
| 5 | +function Test-CondaEnv { |
| 6 | + param([string]$CondaPath) |
| 7 | + |
| 8 | + $envList = & "$CondaPath" env list 2>$null |
| 9 | + if ($envList -match "^$CONDA_ENV_NAME ") { |
| 10 | + Write-Success "Found existing conda environment: $CONDA_ENV_NAME" |
| 11 | + Write-Output "ENV_EXISTS=true" |
| 12 | + return $true |
| 13 | + } else { |
| 14 | + Write-Info "Conda environment '$CONDA_ENV_NAME' not found, will create it" |
| 15 | + Write-Output "ENV_EXISTS=false" |
| 16 | + return $false |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +# Function to create conda environment |
| 21 | +function New-CondaEnv { |
| 22 | + param([string]$CondaPath) |
| 23 | + |
| 24 | + Write-Info "Creating conda environment '$CONDA_ENV_NAME' with Python $PYTHON_VERSION..." |
| 25 | + |
| 26 | + try { |
| 27 | + # Accept conda Terms of Service to avoid interactive prompts |
| 28 | + Write-Info "Accepting conda Terms of Service..." |
| 29 | + & "$CondaPath" config --set solver libmamba 2>$null |
| 30 | + |
| 31 | + # Create environment using conda-forge to minimize TOS issues |
| 32 | + & "$CondaPath" create -n $CONDA_ENV_NAME python=$PYTHON_VERSION -c conda-forge -y |
| 33 | + |
| 34 | + Write-Success "Conda environment created successfully" |
| 35 | + Write-Output "ENV_CREATED=true" |
| 36 | + } catch { |
| 37 | + Write-Error "Failed to create conda environment: $_" |
| 38 | + Write-Output "ENV_CREATED=false" |
| 39 | + exit 1 |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +# Function to verify Python version in environment |
| 44 | +function Test-PythonVersion { |
| 45 | + param([string]$CondaPath) |
| 46 | + |
| 47 | + Write-Info "Verifying Python version in environment..." |
| 48 | + |
| 49 | + try { |
| 50 | + $pyVersionOutput = & "$CondaPath" run -n $CONDA_ENV_NAME python --version 2>&1 |
| 51 | + $pyVersion = ($pyVersionOutput -split ' ')[1] |
| 52 | + $majorMinor = ($pyVersion -split '\.')[0,1] -join '.' |
| 53 | + $expectedMajorMinor = ($PYTHON_VERSION -split '\.')[0,1] -join '.' |
| 54 | + |
| 55 | + if ($majorMinor -eq $expectedMajorMinor) { |
| 56 | + Write-Success "Python version verified: $pyVersion" |
| 57 | + Write-Output "PYTHON_VERSION_OK=true" |
| 58 | + return $true |
| 59 | + } else { |
| 60 | + Write-Warning "Python version mismatch. Expected: $PYTHON_VERSION, Got: $pyVersion" |
| 61 | + Write-Info "Environment will need to be recreated..." |
| 62 | + Write-Output "PYTHON_VERSION_OK=false" |
| 63 | + return $false |
| 64 | + } |
| 65 | + } catch { |
| 66 | + Write-Error "Failed to verify Python version: $_" |
| 67 | + Write-Output "PYTHON_VERSION_OK=false" |
| 68 | + exit 1 |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +function Main { |
| 73 | + param([string]$CondaPath) |
| 74 | + |
| 75 | + if (-not $CondaPath) { |
| 76 | + Write-Error "No conda path provided. Usage: script.ps1 <conda_path>" |
| 77 | + exit 1 |
| 78 | + } |
| 79 | + |
| 80 | + Write-Info "Step 3: Managing conda environment..." |
| 81 | + Write-Info "Using conda at: $CondaPath" |
| 82 | + |
| 83 | + if (-not (Test-CondaEnv $CondaPath)) { |
| 84 | + New-CondaEnv $CondaPath |
| 85 | + } |
| 86 | + |
| 87 | + if (-not (Test-PythonVersion $CondaPath)) { |
| 88 | + Write-Info "Recreating environment with correct Python version..." |
| 89 | + & "$CondaPath" remove -n $CONDA_ENV_NAME --all -y 2>$null |
| 90 | + New-CondaEnv $CondaPath |
| 91 | + Test-PythonVersion $CondaPath |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +# Run main function with arguments |
| 96 | +Main $args[0] |
0 commit comments