Add uninstallation workaround for python 2.* (x86)

This commit is contained in:
Maksim Petrov 2020-04-23 15:52:21 +03:00
parent f5a4030daa
commit 3abece508a

View File

@ -1,33 +1,52 @@
[String]$Architecture = "{{__ARCHITECTURE__}}" [String] $Architecture = "{{__ARCHITECTURE__}}"
[Version]$Version = "{{__VERSION__}}" [Version] $Version = "{{__VERSION__}}"
[String]$PythonExecName = "{{__PYTHON_EXEC_NAME__}}" [String] $PythonExecName = "{{__PYTHON_EXEC_NAME__}}"
function Get-ArchitectureFilter { function Get-ArchitectureFilter
param( {
[Parameter (Mandatory = $true)] param
[String]$Architecture (
[Parameter (Mandatory)]
[String] $Architecture
) )
if ($Architecture -eq 'x86') { if ($Architecture -eq 'x86')
{
"32-bit" "32-bit"
} else { }
else
{
"64-bit" "64-bit"
} }
} }
function Uninstall-Python { function Uninstall-Python
param( {
[Parameter (Mandatory = $true)] param
[String]$Architecture, (
[Parameter (Mandatory = $true)] [Parameter (Mandatory)]
[Int32]$MajorVersion, [String] $Architecture,
[Parameter (Mandatory = $true)] [Parameter (Mandatory)]
[Int32]$MinorVersion [Int32] $MajorVersion,
[Parameter (Mandatory)]
[Int32] $MinorVersion
) )
$ArchFilter = Get-ArchitectureFilter -Architecture $Architecture
$archFilter = Get-ArchitectureFilter -Architecture $Architecture
### Python 2.7 x86 have no architecture postfix
$versionFilter = if (($Architecture -eq "x86") -and ($MajorVersion -eq 2))
{
"Python $MajorVersion.$MinorVersion.\d+$"
}
else
{
"Python $MajorVersion.$MinorVersion.*($archFilter)"
}
$regPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products" $regPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products"
$regKeys = Get-ChildItem -Path Registry::$regPath -Recurse $regKeys = Get-ChildItem -Path Registry::$regPath -Recurse
foreach ($regKey in $regKeys) foreach ($regKey in $regKeys)
{ {
foreach ($propKey in $regKey) foreach ($propKey in $regKey)
@ -35,7 +54,7 @@ function Uninstall-Python {
if ($propKey.Property -eq "DisplayName") if ($propKey.Property -eq "DisplayName")
{ {
$prop = Get-ItemProperty -Path Registry::$($propKey.Name) $prop = Get-ItemProperty -Path Registry::$($propKey.Name)
if ($prop.DisplayName -match "Python $MajorVersion.$MinorVersion.*($ArchFilter)") if ($prop.DisplayName -match $versionFilter)
{ {
Remove-Item -Path Registry::$regKey -Recurse -Force Remove-Item -Path Registry::$regKey -Recurse -Force
} }
@ -46,12 +65,14 @@ function Uninstall-Python {
} }
} }
function Delete-PythonVersion { function Delete-PythonVersion
param( {
[Parameter (Mandatory = $true)] param
[System.IO.FileSystemInfo]$InstalledVersion, (
[Parameter (Mandatory = $true)] [Parameter (Mandatory)]
[String]$Architecture [System.IO.FileSystemInfo] $InstalledVersion,
[Parameter (Mandatory)]
[String] $Architecture
) )
Remove-Item -Path "$($InstalledVersion.FullName)/$Architecture" -Recurse -Force Remove-Item -Path "$($InstalledVersion.FullName)/$Architecture" -Recurse -Force
@ -59,22 +80,27 @@ function Delete-PythonVersion {
} }
function Get-ExecParams { function Get-ExecParams {
param( param
[Parameter (Mandatory = $true)] (
[Boolean]$IsMSI, [Parameter (Mandatory)]
[Parameter (Mandatory = $true)] [Boolean] $IsMSI,
[String]$PythonArchPath [Parameter (Mandatory)]
[String] $PythonArchPath
) )
if ($IsMSI) { if ($IsMSI)
{
"TARGETDIR=$PythonArchPath ALLUSERS=1" "TARGETDIR=$PythonArchPath ALLUSERS=1"
} else { }
else
{
"DefaultAllUsersTargetDir=$PythonArchPath InstallAllUsers=1" "DefaultAllUsersTargetDir=$PythonArchPath InstallAllUsers=1"
} }
} }
$ToolcacheRoot = $env:AGENT_TOOLSDIRECTORY $ToolcacheRoot = $env:AGENT_TOOLSDIRECTORY
if ([string]::IsNullOrEmpty($ToolcacheRoot)) { if ([string]::IsNullOrEmpty($ToolcacheRoot))
{
# GitHub images don't have `AGENT_TOOLSDIRECTORY` variable # GitHub images don't have `AGENT_TOOLSDIRECTORY` variable
$ToolcacheRoot = $env:RUNNER_TOOL_CACHE $ToolcacheRoot = $env:RUNNER_TOOL_CACHE
} }
@ -88,22 +114,29 @@ $MajorVersion = $Version.Major
$MinorVersion = $Version.Minor $MinorVersion = $Version.Minor
Write-Host "Check if Python hostedtoolcache folder exist..." Write-Host "Check if Python hostedtoolcache folder exist..."
if (-Not (Test-Path $PythonToolcachePath)) { if (-Not (Test-Path $PythonToolcachePath))
{
Write-Host "Create Python toolcache folder" Write-Host "Create Python toolcache folder"
New-Item -ItemType Directory -Path $PythonToolcachePath | Out-Null New-Item -ItemType Directory -Path $PythonToolcachePath | Out-Null
} else { }
else
{
Write-Host "Check if current Python version is installed..." Write-Host "Check if current Python version is installed..."
$InstalledVersion = Get-ChildItem -Path $PythonToolcachePath -Filter "$MajorVersion.$MinorVersion.*" $InstalledVersion = Get-ChildItem -Path $PythonToolcachePath -Filter "$MajorVersion.$MinorVersion.*"
if ($InstalledVersion -ne $null) { if ($InstalledVersion -ne $null)
{
Uninstall-Python -Architecture $Architecture -MajorVersion $MajorVersion -MinorVersion $MinorVersion Uninstall-Python -Architecture $Architecture -MajorVersion $MajorVersion -MinorVersion $MinorVersion
if (Test-Path -Path "$($InstalledVersion.FullName)/$Architecture") { if (Test-Path -Path "$($InstalledVersion.FullName)/$Architecture")
{
Write-Host "Python$MajorVersion.$MinorVersion/$Architecture was found in $PythonToolcachePath" Write-Host "Python$MajorVersion.$MinorVersion/$Architecture was found in $PythonToolcachePath"
Write-Host "Delete Python$MajorVersion.$MinorVersion $Architecture" Write-Host "Delete Python$MajorVersion.$MinorVersion $Architecture"
Delete-PythonVersion -InstalledVersion $InstalledVersion -Architecture $Architecture Delete-PythonVersion -InstalledVersion $InstalledVersion -Architecture $Architecture
} }
} else { }
else
{
Write-Host "No Python$MajorVersion.$MinorVersion.* found" Write-Host "No Python$MajorVersion.$MinorVersion.* found"
} }
} }
@ -118,7 +151,8 @@ Write-Host "Install Python $Version in $PythonToolcachePath..."
$ExecParams = Get-ExecParams -IsMSI $IsMSI -PythonArchPath $PythonArchPath $ExecParams = Get-ExecParams -IsMSI $IsMSI -PythonArchPath $PythonArchPath
cmd.exe /c "cd $PythonArchPath && call $PythonExecName $ExecParams /quiet" cmd.exe /c "cd $PythonArchPath && call $PythonExecName $ExecParams /quiet"
if ($LASTEXITCODE -ne 0) { if ($LASTEXITCODE -ne 0)
{
Throw "Error happened during Python installation" Throw "Error happened during Python installation"
} }