Clean up additional registry branches when uninstalling Python on Windows (#11)

1) Clean up `Uninstall` section in registry
2) Remove x86 version of tool when install x64
3) Check registry always
This commit is contained in:
Maxim Lobanov 2020-04-27 13:26:01 +03:00 committed by GitHub
parent 2f4785cda3
commit 1a48032432
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,26 @@
[Version] $Version = "{{__VERSION__}}"
[String] $PythonExecName = "{{__PYTHON_EXEC_NAME__}}"
function Get-RegistryVersionFilter {
param
(
[Parameter(Mandatory)][String] $Architecture,
[Parameter(Mandatory)][Int32] $MajorVersion,
[Parameter(Mandatory)][Int32] $MinorVersion
)
$archFilter = if ($Architecture -eq 'x86') { "32-bit" } else { "64-bit" }
### Python 2.7 x86 have no architecture postfix
if (($Architecture -eq "x86") -and ($MajorVersion -eq 2))
{
"Python $MajorVersion.$MinorVersion.\d+$"
}
else
{
"Python $MajorVersion.$MinorVersion.*($archFilter)"
}
}
function Remove-RegistryEntries
{
param
@ -11,17 +31,8 @@ function Remove-RegistryEntries
[Parameter(Mandatory)][Int32] $MinorVersion
)
$archFilter = if ($Architecture -eq 'x86') { "32-bit" } else { "64-bit" }
### 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)"
}
$versionFilter = Get-RegistryVersionFilter -Architecture $Architecture -MajorVersion $MajorVersion -MinorVersion $MinorVersion
$regPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products"
$regKeys = Get-ChildItem -Path Registry::$regPath -Recurse | Where-Object Property -Ccontains DisplayName
foreach ($key in $regKeys)
@ -33,12 +44,22 @@ function Remove-RegistryEntries
}
$regPath = "HKEY_CLASSES_ROOT\Installer\Products"
Get-ChildItem -Path Registry::$regPath | Where-Object {
$productName = $_.GetValue("ProductName")
return $productName -and $productName -match $versionFilter
} | ForEach-Object {
Get-ChildItem -Path Registry::$regPath | Where-Object { $_.GetValue("ProductName") -match $versionFilter } | ForEach-Object {
Remove-Item Registry::$_ -Recurse -Force -Verbose
}
$uninstallRegistrySections = @(
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall", # current user, x64
"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall", # all users, x64
"HKEY_CURRENT_USER\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", # current user, x86
"HKEY_LOCAL_MACHINE\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" # all users, x86
)
$uninstallRegistrySections | Where-Object { Test-Path -Path Registry::$_ } | ForEach-Object {
Get-ChildItem -Path Registry::$_ | Where-Object { $_.getValue("DisplayName") -match $versionFilter } | ForEach-Object {
Remove-Item Registry::$_ -Recurse -Force -Verbose
}
}
}
function Get-ExecParams {
@ -78,29 +99,20 @@ if (-Not (Test-Path $PythonToolcachePath))
{
Write-Host "Create Python toolcache folder"
New-Item -ItemType Directory -Path $PythonToolcachePath | Out-Null
}
else
{
Write-Host "Check if current Python version is installed..."
$InstalledVersion = Get-ChildItem -Path $PythonToolcachePath -Filter "$MajorVersion.$MinorVersion.*"
}
Write-Host "Remove registry entries for Python ${MajorVersion}.${MinorVersion}(${Architecture})..."
Remove-RegistryEntries -Architecture $Architecture -MajorVersion $MajorVersion -MinorVersion $MinorVersion
Write-Host "Check if current Python version is installed..."
$InstalledVersion = Get-ChildItem -Path $PythonToolcachePath -Filter "$MajorVersion.$MinorVersion.*"
if ($null -ne $InstalledVersion)
{
if (Test-Path -Path "$($InstalledVersion.FullName)/$Architecture")
{
Write-Host "Python$MajorVersion.$MinorVersion/$Architecture was found in $PythonToolcachePath"
Write-Host "Delete Python$MajorVersion.$MinorVersion $Architecture"
Remove-Item -Path "$($InstalledVersion.FullName)/$Architecture" -Recurse -Force
Remove-Item -Path "$($InstalledVersion.FullName)/$Architecture.complete" -Force
}
}
else
{
Write-Host "No Python$MajorVersion.$MinorVersion.* found"
}
Write-Host "Remove registry entries for Python ${MajorVersion}.${MinorVersion}(${Architecture})..."
Remove-RegistryEntries -Architecture $Architecture -MajorVersion $MajorVersion -MinorVersion $MinorVersion
if (($null -ne $InstalledVersion) -and (Test-Path -Path $InstalledVersion.FullName)) {
Write-Host "Python$MajorVersion.$MinorVersion was found in $PythonToolcachePath"
Write-Host "Deleting $($InstalledVersion.FullName)..."
Remove-Item -Path $InstalledVersion.FullName -Recurse -Force
} else {
Write-Host "No Python$MajorVersion.$MinorVersion.* found"
}
Write-Host "Create Python $Version folder in $PythonToolcachePath"