120 lines
4.6 KiB
Python
120 lines
4.6 KiB
Python
"""Installed software on Windows as packages (NetOrk/netork#2890).
|
|
|
|
:class:`WindowsPackagesMixin` gives the driver napalm-device-types' ``get_packages``
|
|
(``PackageDict``) from the machine-wide Uninstall keys, the list "Apps & features" and
|
|
``winget list`` read too:
|
|
|
|
* ``HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall``: the host's native
|
|
programs (``x64``, ``arm64`` on ARM, ``x86`` on a 32-bit Windows);
|
|
* ``HKLM\\SOFTWARE\\WOW6432Node\\...\\Uninstall``: the 32-bit programs of a 64-bit
|
|
Windows (``x86``).
|
|
|
|
A 32-bit and a 64-bit install of one program stay two packages, told apart by
|
|
``architecture``. Left out: keys without a ``DisplayName``, ``SystemComponent=1``
|
|
(parts Windows or a suite hides from the list) and entries with a ``ParentKeyName``
|
|
(updates listed under their product; Windows' own updates are the update history's).
|
|
``source`` is ``msi`` for a Windows Installer package (``WindowsInstaller=1``), else
|
|
``exe``; ``size`` is ``EstimatedSize`` (KiB) in bytes, 0 where none is given;
|
|
``publisher`` is the vendor the program names.
|
|
|
|
Not read: per-user installs (a user's own ``HKU`` hive -- over WinRM ``HKCU`` is the
|
|
connecting account's, and the hives of whoever is logged on would make the list change
|
|
with them), Store and MSIX apps, and winget, which is a per-user App Execution Alias
|
|
that does not work reliably in a WinRM session. Nothing is installed or removed.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from napalm_device_types.models import PackageDict
|
|
|
|
from napalm_windows._powershell import _as_list
|
|
|
|
_UNINSTALL = r"Microsoft\Windows\CurrentVersion\Uninstall"
|
|
|
|
_PS_PACKAGES = rf"""
|
|
$ErrorActionPreference = 'Stop'
|
|
$native = switch ($env:PROCESSOR_ARCHITECTURE) {{
|
|
'ARM64' {{ 'arm64' }}
|
|
'x86' {{ 'x86' }}
|
|
default {{ 'x64' }}
|
|
}}
|
|
$keys = @(
|
|
[pscustomobject]@{{ path = 'HKLM:\SOFTWARE\{_UNINSTALL}'; arch = $native }},
|
|
[pscustomobject]@{{ path = 'HKLM:\SOFTWARE\WOW6432Node\{_UNINSTALL}'; arch = 'x86' }}
|
|
)
|
|
ConvertTo-Json -Compress -Depth 3 -InputObject @(
|
|
foreach ($key in $keys) {{
|
|
if (Test-Path -LiteralPath $key.path) {{
|
|
Get-ChildItem -LiteralPath $key.path -ErrorAction SilentlyContinue | ForEach-Object {{
|
|
[pscustomobject]@{{
|
|
key = [string]$_.PSChildName
|
|
arch = $key.arch
|
|
name = [string]$_.GetValue('DisplayName')
|
|
version = [string]$_.GetValue('DisplayVersion')
|
|
publisher = [string]$_.GetValue('Publisher')
|
|
system = [string]$_.GetValue('SystemComponent')
|
|
parent = [string]$_.GetValue('ParentKeyName')
|
|
msi = [string]$_.GetValue('WindowsInstaller')
|
|
size = [string]$_.GetValue('EstimatedSize')
|
|
}}
|
|
}}
|
|
}}
|
|
}}
|
|
)
|
|
"""
|
|
|
|
|
|
class WindowsPackageDict(PackageDict):
|
|
"""A program from the Uninstall keys, with whose it is and which build."""
|
|
|
|
publisher: str
|
|
architecture: str # x64, x86 or arm64
|
|
|
|
|
|
def _size(value: Any) -> int:
|
|
text = str(value or "").strip()
|
|
return int(text) * 1024 if text.isdigit() else 0
|
|
|
|
|
|
def _listed(entry: dict[str, Any]) -> bool:
|
|
"""A program the host lists as installed: named, not hidden, not an update."""
|
|
return bool(
|
|
str(entry.get("name") or "").strip()
|
|
and str(entry.get("system") or "") != "1"
|
|
and not str(entry.get("parent") or "").strip()
|
|
)
|
|
|
|
|
|
def _package(entry: dict[str, Any]) -> WindowsPackageDict:
|
|
return {
|
|
"name": str(entry["name"]).strip(),
|
|
"version": str(entry.get("version") or "").strip(),
|
|
"installed": True,
|
|
"description": "",
|
|
"size": _size(entry.get("size")),
|
|
"source": "msi" if str(entry.get("msi") or "") == "1" else "exe",
|
|
"publisher": str(entry.get("publisher") or "").strip(),
|
|
"architecture": str(entry.get("arch") or ""),
|
|
}
|
|
|
|
|
|
def parse_packages(raw: Any) -> list[PackageDict]:
|
|
"""``_PS_PACKAGES``'s JSON as packages, by name; see the module."""
|
|
packages = [_package(e) for e in _as_list(raw) if isinstance(e, dict) and _listed(e)]
|
|
packages.sort(key=lambda p: (p["name"].lower(), p["architecture"]))
|
|
return list(packages)
|
|
|
|
|
|
class WindowsPackagesMixin:
|
|
"""``get_packages`` from the Uninstall keys; the driver carries the script."""
|
|
|
|
if TYPE_CHECKING: # pragma: no cover - declared for type checkers only
|
|
|
|
def _run_ps(self, script: str) -> Any: ...
|
|
|
|
def get_packages(self) -> list[PackageDict]:
|
|
"""The programs installed for the whole machine; see the module."""
|
|
return parse_packages(self._run_ps(_PS_PACKAGES))
|