DeployConcept/src/ConceptDeploy/Initialize-Python.ps1

165 lines
5.8 KiB
PowerShell
Raw Normal View History

2024-06-07 20:21:38 +03:00
function Initialize-Python {
[CmdletBinding()]
Param(
[string] $minimalVersion = '3.12.2',
[string] $installPath = 'C:\Tools\Python312-venv',
[string] $pythonInstall = 'C:\Tools\Python312',
[string] $pythonDistr = ''
)
$python = SearchInstalledEnvironment -TargetPath $installPath -MinimalVersion $minimalVersion
if ($python -ne '') {
return $python
}
$python = SearchLocalInstallation -TargetPath $pythonInstall -MinimalVersion $minimalVersion
if ($python -eq '') {
Write-Host "Failed to locate local installation of python"
$python = SearchUserPath -MinimalVersion $minimalVersion
}
if ($python -eq '') {
Write-Host "Failed to locate python in PATH variable"
$python = SearchUserRegistry -MinimalVersion $minimalVersion
}
if ($python -eq '') {
Write-Host "Failed to locate python in Windows registry"
$python = InstallLocalPython -Version $minimalVersion -TargetPath $pythonInstall -PythonDistr $pythonDistr
}
if ($python -eq '') {
return ''
}
CreateVirtualEnvironment -Python $python -TargetPath $installPath
return "$installPath\Scripts\python.exe"
}
function SearchInstalledEnvironment($targetPath, $minimalVersion) {
$python = "$targetPath\Scripts\python.exe"
if (-not (Test-Path -Path $python -PathType Leaf)) {
Write-Host "Failed to locate Python venv at $targetPath"
return ''
}
$installedVersion = GetPythonVersion $python
if(!$installedVersion -or
$installedVersion -eq '' -or
[System.Version] $installedVersion -lt [System.Version] $minimalVersion) {
Write-Host "Removing invalid python env: $targetPath" -ForegroundColor DarkRed
Remove-Item $targetPath -Recurse
return ''
} else {
Write-Host "Found Python environment $installedVersion : $python"
return $python
}
}
function SearchLocalInstallation($targetPath, $minimalVersion) {
$python = "$targetPath\python.exe"
if (-not (Test-Path -Path $python -PathType Leaf)) {
return ''
}
$installedVersion = GetPythonVersion $python
if(!$installedVersion -or
$installedVersion -eq '' -or
[System.Version] $installedVersion -ne [System.Version] $minimalVersion) {
return ''
}
Write-Host "Found installed python $installedVersion : $python"
return $python
}
function SearchUserPath($minimalVersion) {
$globalVersion = GetPythonVersion 'python'
if(!$globalVersion -or
$globalVersion -eq '' -or
[System.Version] $globalVersion -ne [System.Version] $minimalVersion) {
return ''
}
Write-Host "Found globally installed python: $globalVersion"
return 'python'
}
function SearchUserRegistry($minimalVersion) {
if ([Environment]::Is64BitProcess) {
$pythonRegistry = 'HKCU:\SOFTWARE\Python\PythonCore\3.12\InstallPath'
if(!$pythonRegistry) {
$pythonRegistry = 'HKLM:\SOFTWARE\Python\PythonCore\3.12\InstallPath'
}
} else {
$pythonRegistry = 'HKCU:\SOFTWARE\WOW6432Node\Python\PythonCore\3.12-32\InstallPath'
if(!$pythonRegistry) {
$pythonRegistry = 'HKLM:\SOFTWARE\WOW6432Node\Python\PythonCore\3.12-32\InstallPath'
}
}
$python = Get-ItemPropertyValue -Path $pythonRegistry -Name 'ExecutablePath' -ErrorAction 'SilentlyContinue'
if (-not $python) {
return ''
}
$version = GetPythonVersion $python
if(!$version -or
$version -eq '' -or
[System.Version] $version -lt [System.Version] $minimalVersion) {
return ''
}
Write-Host "Found installed local python: $python" -ForegroundColor DarkGreen
return $python
}
function InstallLocalPython($version, $targetPath, $pythonDistr = '') {
if ($pythonDistr -eq '') {
$installer = DowloadPython $version
} else {
$installer = $pythonDistr
}
$arguments = "/quiet InstallAllUsers=0 Include_test=0 Include_doc=0 Include_launcher=0 TargetDir=$targetPath"
$process = (Start-Process -FilePath $installer -ArgumentList $arguments -PassThru -NoNewWindow)
$process.WaitForExit()
if ($pythonDistr -eq '') {
Remove-Item $installer -ErrorAction SilentlyContinue
}
$python = "$targetPath\python.exe"
if (-not (Test-Path -Path $python -PathType Leaf)) {
Write-Error "Failed to install local python: $installer"
return ''
} else {
Write-Host "Installed local python: $targetPath" -ForegroundColor DarkGreen
return $python
}
}
function GetPythonVersion($pythonExe) {
$eap = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
$version = & $pythonExe --version
$ErrorActionPreference = $eap
if (!$version -or $version -eq '' -or $LASTEXITCODE -ne 0) {
return ''
} else {
return $version.Split(' ')[1]
}
}
function DowloadPython($version) {
$pythonUrl = GetPythonURL $version
$pythonTemp = "$Env:TEMP\installer-python-$version.exe"
Write-Host "Downloading python installer to $pythonTemp"
try {
(New-Object System.Net.WebClient).DownloadFile($pythonUrl, $pythonTemp)
} catch [System.Net.WebException] {
Write-Error "An exception was caught when trying to download Python: $($_.Exception.Message)"
Exit
}
return $pythonTemp
}
function CreateVirtualEnvironment($python, $targetPath) {
& $python -m venv $targetPath
Write-Host "Created virtual environment $targetPath" -ForegroundColor DarkGreen
}
function GetPythonURL($version) {
if ([Environment]::Is64BitProcess) {
return "https://www.python.org/ftp/python/$version/python-$version-amd64.exe"
} else {
return = "https://www.python.org/ftp/python/$version/python-$version.exe"
}
}