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__}}"
[Version]$Version = "{{__VERSION__}}"
[String]$PythonExecName = "{{__PYTHON_EXEC_NAME__}}"
[String] $Architecture = "{{__ARCHITECTURE__}}"
[Version] $Version = "{{__VERSION__}}"
[String] $PythonExecName = "{{__PYTHON_EXEC_NAME__}}"
function Get-ArchitectureFilter {
param(
[Parameter (Mandatory = $true)]
[String]$Architecture
function Get-ArchitectureFilter
{
param
(
[Parameter (Mandatory)]
[String] $Architecture
)
if ($Architecture -eq 'x86') {
if ($Architecture -eq 'x86')
{
"32-bit"
} else {
}
else
{
"64-bit"
}
}
function Uninstall-Python {
param(
[Parameter (Mandatory = $true)]
[String]$Architecture,
[Parameter (Mandatory = $true)]
[Int32]$MajorVersion,
[Parameter (Mandatory = $true)]
[Int32]$MinorVersion
function Uninstall-Python
{
param
(
[Parameter (Mandatory)]
[String] $Architecture,
[Parameter (Mandatory)]
[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"
$regKeys = Get-ChildItem -Path Registry::$regPath -Recurse
foreach ($regKey in $regKeys)
{
foreach ($propKey in $regKey)
@ -35,7 +54,7 @@ function Uninstall-Python {
if ($propKey.Property -eq "DisplayName")
{
$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
}
@ -46,12 +65,14 @@ function Uninstall-Python {
}
}
function Delete-PythonVersion {
param(
[Parameter (Mandatory = $true)]
[System.IO.FileSystemInfo]$InstalledVersion,
[Parameter (Mandatory = $true)]
[String]$Architecture
function Delete-PythonVersion
{
param
(
[Parameter (Mandatory)]
[System.IO.FileSystemInfo] $InstalledVersion,
[Parameter (Mandatory)]
[String] $Architecture
)
Remove-Item -Path "$($InstalledVersion.FullName)/$Architecture" -Recurse -Force
@ -59,22 +80,27 @@ function Delete-PythonVersion {
}
function Get-ExecParams {
param(
[Parameter (Mandatory = $true)]
[Boolean]$IsMSI,
[Parameter (Mandatory = $true)]
[String]$PythonArchPath
param
(
[Parameter (Mandatory)]
[Boolean] $IsMSI,
[Parameter (Mandatory)]
[String] $PythonArchPath
)
if ($IsMSI) {
if ($IsMSI)
{
"TARGETDIR=$PythonArchPath ALLUSERS=1"
} else {
}
else
{
"DefaultAllUsersTargetDir=$PythonArchPath InstallAllUsers=1"
}
}
$ToolcacheRoot = $env:AGENT_TOOLSDIRECTORY
if ([string]::IsNullOrEmpty($ToolcacheRoot)) {
if ([string]::IsNullOrEmpty($ToolcacheRoot))
{
# GitHub images don't have `AGENT_TOOLSDIRECTORY` variable
$ToolcacheRoot = $env:RUNNER_TOOL_CACHE
}
@ -88,22 +114,29 @@ $MajorVersion = $Version.Major
$MinorVersion = $Version.Minor
Write-Host "Check if Python hostedtoolcache folder exist..."
if (-Not (Test-Path $PythonToolcachePath)) {
if (-Not (Test-Path $PythonToolcachePath))
{
Write-Host "Create Python toolcache folder"
New-Item -ItemType Directory -Path $PythonToolcachePath | Out-Null
} else {
}
else
{
Write-Host "Check if current Python version is installed..."
$InstalledVersion = Get-ChildItem -Path $PythonToolcachePath -Filter "$MajorVersion.$MinorVersion.*"
if ($InstalledVersion -ne $null) {
if ($InstalledVersion -ne $null)
{
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 "Delete Python$MajorVersion.$MinorVersion $Architecture"
Delete-PythonVersion -InstalledVersion $InstalledVersion -Architecture $Architecture
}
} else {
}
else
{
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
cmd.exe /c "cd $PythonArchPath && call $PythonExecName $ExecParams /quiet"
if ($LASTEXITCODE -ne 0) {
if ($LASTEXITCODE -ne 0)
{
Throw "Error happened during Python installation"
}