Fixed GDM reinstallation, extracted gresource and update-alternatives logic

This commit is contained in:
Vladyslav Hroshev
2025-04-04 20:25:57 +03:00
parent 4494e98080
commit 19cdad3041
3 changed files with 296 additions and 227 deletions

View File

@@ -0,0 +1,86 @@
import functools
import subprocess
from typing import TypeAlias
from scripts.utils.console import Console
PathString: TypeAlias = str | bytes
class AlternativesUpdater:
"""
Manages update-alternatives for Ubuntu.
Ignores errors if update-alternatives not found.
"""
@staticmethod
def ubuntu_specific(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
return result
except FileNotFoundError as e:
if not "update-alternatives" in str(e):
raise
return None
return wrapper
@staticmethod
@ubuntu_specific
def install_and_set(link: str, name: str, path: PathString, priority: int = 0):
AlternativesUpdater.install(link, name, path, priority)
AlternativesUpdater.set(name, path)
@staticmethod
@ubuntu_specific
def install(link: str, name: str, path: PathString, priority: int = 0):
"""
Add an alternative to the system
:param link: Absolute path with file name where the link will be created
:param name: Name of the alternative
:param path: An absolute path to the file that will be linked
:param priority: Priority of the alternative; Higher number means higher priority
Example:
install(/usr/share/gnome-shell/gdm-theme.gresource,
gdm-theme.gresource, /usr/share/gnome-shell/gnome-shell-theme.gresource)
"""
subprocess.run([
"update-alternatives", "--quiet", "--install",
link, name, str(path), str(priority)
], check=True)
Console.Line().success(f"Installed {name} alternative.")
@staticmethod
@ubuntu_specific
def set(name: str, path: PathString):
"""
Set path as alternative to name in system
:param name: Name of the alternative
:param path: An absolute path to the file that will be linked
Example:
set(gdm-theme.gresource, /usr/share/gnome-shell/gnome-shell-theme.gresource)
"""
subprocess.run([
"update-alternatives", "--quiet", "--set",
name, str(path)
], check=True)
@staticmethod
@ubuntu_specific
def remove(name: str, path: PathString):
"""
Remove alternative from system
:param name: Name of the alternative
:param path: An absolute path to the file that will be linked
Example:
remove(gdm-theme.gresource, /usr/share/gnome-shell/gnome-shell-theme.gresource)
"""
subprocess.run([
"update-alternatives", "--quiet", "--remove",
name, str(path)
], check=True)
Console.Line().success(f"Removed {name} alternative.")