Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove unnecessary trash from nvidia GPU driver installer
# Remove unnecessary trash from nvidia GPU driver installer
# Modifies the setup.cfg XML file and removes garbage directories as well
# First unpack the installer .exe with 7zip and then run:
# this_script.ps1 <unpack directory>
# (c) Pekka Järvinen 2018-
param (
[Parameter(Mandatory=$true)][ValidateScript({Test-Path $_})] [string]$unpackDir
)
function Read-Choice(
[Parameter(Mandatory)][string]$Message,
[Parameter(Mandatory)][string[]]$Choices,
[Parameter(Mandatory)][string]$DefaultChoice,
[Parameter()][string]$Question='Are you sure you want to proceed?'
) {
$defaultIndex = $Choices.IndexOf($DefaultChoice)
if ($defaultIndex -lt 0) {
throw "$DefaultChoice not found in choices"
}
$choiceObj = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
foreach($c in $Choices) {
$choiceObj.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList $c))
}
$decision = $Host.UI.PromptForChoice($Message, $Question, $choiceObj, $defaultIndex)
return $Choices[$decision]
}
$unpackDir = Resolve-Path $unpackDir
Push-Location -Path "$unpackDir"
if (! (Test-Path "setup.cfg.orig")) {
Copy-Item "setup.cfg" "setup.cfg.orig"
}
if (! (Test-Path "_trash")) {
New-Item -ItemType directory -Path "_trash"
}
$xpathprefix = "/setup/install/sub-package"
$removeXpath = @(
"$xpathprefix/properties/bool[@name='IsGFExperienceComponent' and @value='true']/../.."
"$xpathprefix/dependencies/package[@type='requires' and @package='Display.GFExperience']/../.."
"$xpathprefix[@name='Display.GFExperience']"
)
[xml]$src = Get-Content "setup.cfg.orig"
$rmdirs = @(
"nodejs"
"ShieldWirelessController"
"Display.NView"
"Display.Optimus"
"NV3DVision"
"NV3DVisionUSB.Driver"
"NvBackend"
"NvCamera"
"GFExperience"
)
foreach ($xp in $removeXpath) {
$remove = $src.SelectNodes($xp)
foreach ($pkg in $remove) {
$rmdirs += $pkg.name
foreach ($p in $pkg.SelectNodes("//package[@type='installs']")) {
$rmdirs += $p.package
}
}
$remove | foreach {$_.ParentNode.RemoveChild($_)}
}
foreach ($dir in $rmdirs) {
if (Test-Path "$dir") {
Move-Item "$dir" -Destination "_trash"
}
}
if (! (Test-Path "GFExperience")) {
New-Item -ItemType directory -Path "GFExperience"
}
Push-Location -Path "_trash"
Push-Location -Path "GFExperience"
$cpFiles = @()
foreach ($f in (Get-ChildItem -File -Recurse -Include "*.htm","*.html","*.txt" -Path .)) {
$base = Join-Path -Resolve $f.PSDrive.Root $f.PSDrive.CurrentLocation
$cpFiles += Join-Path . $f.FullName.Remove(0, $base.Length)
}
Pop-Location
Pop-Location
Get-Location
Push-Location -Path "GFExperience"
foreach($f in $cpFiles) {
$from = [io.path]::GetFullPath("$pwd/../_trash/GFExperience/$f")
$dest = [io.path]::GetFullPath("$pwd/$f")
[IO.Directory]::CreateDirectory([IO.Path]::GetDirectoryName($dest))
Copy-Item $from $dest
}
Pop-Location
Remove-Item -Force -Recurse "_trash"
$src.OuterXml | Out-File -encoding "UTF8" "setup.cfg"
$r = Read-Choice 'Start installer?' '&yes','&no' '&no'
switch($r) {
'&yes' {
& "$unpackDir/setup.exe" -noeula
}
}
Pop-Location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment