Added support for installing GDM theming on Ubuntu

- From now GlobalTheme use another method to check does GDM support light/dark mode or not
- Structurized GlobalTheme methods
- Lowered Python version from 3.12 to 3.10
This commit is contained in:
Vladyslav Hroshev
2025-04-03 21:21:28 +03:00
parent 8a377455ea
commit 2ea1dd2d2d
5 changed files with 225 additions and 130 deletions

View File

@@ -45,4 +45,5 @@ if __name__ == "__main__":
try: try:
main() main()
finally: finally:
shutil.rmtree(config.temp_folder, ignore_errors=True) pass
# shutil.rmtree(config.temp_folder, ignore_errors=True)

View File

@@ -1,3 +1,4 @@
import os.path
from tempfile import gettempdir from tempfile import gettempdir
# folder definitions # folder definitions
@@ -13,6 +14,7 @@ scripts_folder = "scripts"
# GDM definitions # GDM definitions
global_gnome_shell_theme = "/usr/share/gnome-shell" global_gnome_shell_theme = "/usr/share/gnome-shell"
gnome_shell_gresource = "gnome-shell-theme.gresource" gnome_shell_gresource = "gnome-shell-theme.gresource"
ubuntu_gresource_link = "gtk-theme.gresource"
extracted_gdm_folder = "theme" extracted_gdm_folder = "theme"
# files definitions # files definitions

View File

@@ -1,35 +1,39 @@
import functools
import os import os
import subprocess import subprocess
from typing import Optional, TypeAlias
from .theme import Theme from .theme import Theme
from .utils import remove_properties, remove_keywords, gnome from .utils import remove_properties, remove_keywords
from . import config from . import config
from .utils.console import Console, Color, Format from .utils.console import Console, Color, Format
from .utils.files_labeler import FilesLabeler from .utils.files_labeler import FilesLabeler
Path: TypeAlias = str | bytes
class ThemePrepare: class ThemePrepare:
""" """
Theme object prepared for installation Theme object prepared for installation
""" """
def __init__(self, theme: Theme, theme_file, should_label=False): def __init__(self, theme: Theme, theme_file, label: Optional[str] = None):
self.theme = theme self.theme = theme
self.theme_file = theme_file self.theme_file = theme_file
self.should_label = should_label self.label = label
class GlobalTheme: class GlobalTheme:
def __init__(self, colors_json, theme_folder, destination_folder, destination_file, temp_folder, def __init__(self, colors_json, theme_folder, destination_folder, destination_file, temp_folder: str,
mode=None, is_filled=False): mode=None, is_filled=False):
""" """
Initialize GlobalTheme class Initialize GlobalTheme class
:param colors_json: location of a json file with colors :param colors_json: location of a JSON file with color values
:param theme_folder: raw theme location :param theme_folder: raw theme location
:param destination_folder: folder where themes will be installed :param destination_folder: folder where themes will be installed
:param temp_folder: folder where files will be collected :param temp_folder: folder where files will be collected
:param mode: theme mode (light or dark). applied only for gnome-shell < 44 :param mode: theme mode (light or dark)
:param is_filled: if True, theme will be filled :param is_filled: if True, the theme will be filled
""" """
self.colors_json = colors_json self.colors_json = colors_json
@@ -39,172 +43,100 @@ class GlobalTheme:
self.temp_folder = temp_folder self.temp_folder = temp_folder
self.backup_file = f"{self.destination_file}.backup" self.backup_file = f"{self.destination_file}.backup"
self.backup_trigger = "\n/* Marble theme */\n" # trigger to check if theme is installed self.backup_trigger = "\n/* Marble theme */\n"
self.extracted_theme: str = os.path.join(self.temp_folder, config.extracted_gdm_folder) self.extracted_theme: str = os.path.join(self.temp_folder, config.extracted_gdm_folder)
self.gst = os.path.join(self.destination_folder, self.destination_file) # use backup file if theme is installed self.gresource_file = os.path.join(self.destination_folder, self.destination_file)
self.themes: list[ThemePrepare] = [] self.themes: list[ThemePrepare] = []
self.is_filled = is_filled self.is_filled = is_filled
self.mode = mode self.mode = mode
def prepare(self):
def __create_theme(self, theme_type: str, mode=None, should_label=False, is_filled=False): self.__extract()
"""Helper to create theme objects""" self.__find_themes()
theme = Theme(theme_type, self.colors_json, self.theme_folder,
self.extracted_theme, self.temp_folder,
mode=mode, is_filled=is_filled)
theme.prepare()
theme_file = os.path.join(self.extracted_theme, f"{theme_type}.css")
return ThemePrepare(theme=theme, theme_file=theme_file, should_label=should_label)
def __is_installed(self):
"""
Check if theme is installed
:return: True if theme is installed, False otherwise
"""
with open(self.gst, "rb") as f:
return self.backup_trigger.encode() in f.read()
def __extract(self): def __extract(self):
""" """Extract gresource files to temp folder"""
Extract gresource files to temp folder
"""
extract_line = Console.Line() extract_line = Console.Line()
extract_line.update("Extracting gresource files...") extract_line.update("Extracting gresource files...")
resources = subprocess.getoutput(f"gresource list {self.gst}").split("\n") resources_list_response = subprocess.run(
prefix = "/org/gnome/shell/" ["gresource", "list", self.gresource_file],
capture_output=True, text=True, check=False
)
if resources_list_response.stderr:
extract_line.error(f"Failed to extract resources: {resources_list_response.stderr.strip()}")
raise Exception(f"gresource could not process the theme file: {self.gresource_file}")
resources = resources_list_response.stdout.strip().split("\n")
prefix = "/org/gnome/shell/theme/"
try: try:
for resource in resources: for resource in resources:
resource_path = resource.replace(prefix, "") resource_path = resource.replace(prefix, "")
dir_path = os.path.join(self.temp_folder, os.path.dirname(resource_path)) output_path = os.path.join(self.extracted_theme, resource_path)
output_path = os.path.join(self.temp_folder, resource_path) os.makedirs(os.path.dirname(output_path), exist_ok=True)
os.makedirs(dir_path, exist_ok=True)
with open(output_path, 'wb') as f: with open(output_path, 'wb') as f:
subprocess.run(["gresource", "extract", self.gst, resource], stdout=f, check=True) subprocess.run(["gresource", "extract", self.gresource_file, resource], stdout=f, check=True)
extract_line.success("Extracted gresource files.") extract_line.success("Extracted gresource files.")
except FileNotFoundError as e: except FileNotFoundError as e:
if "gresource" in str(e): if "gresource" in str(e):
self.__raise_gresource_error(e)
raise
@staticmethod
def __raise_gresource_error(e: Exception):
print("Error: 'gresource' command not found.") print("Error: 'gresource' command not found.")
print("Please install the glib2-devel package:") print("Please install the glib2-devel package:")
print(" - For Fedora/RHEL: sudo dnf install glib2-devel") print(" - For Fedora/RHEL: sudo dnf install glib2-devel")
print(" - For Ubuntu/Debian: sudo apt install libglib2.0-dev") print(" - For Ubuntu/Debian: sudo apt install libglib2.0-dev")
print(" - For Arch: sudo pacman -S glib2-devel") print(" - For Arch: sudo pacman -S glib2-devel")
raise Exception("Missing required dependency: glib2-devel") from e raise Exception("Missing required dependency: glib2-devel") from e
raise
def __add_gnome_styles(self, theme): def __find_themes(self):
""" extracted_theme_files = os.listdir(self.extracted_theme)
Add gnome styles to the start of the file
:param theme: Theme object
"""
with open(f"{self.extracted_theme}/{theme.theme_type}.css", 'r') as gnome_theme: allowed_modes = ("dark", "light")
gnome_styles = gnome_theme.read() + self.backup_trigger allowed_css = ("gnome-shell-dark", "gnome-shell-light", "gnome-shell")
theme.add_to_start(gnome_styles)
def __generate_themes(self, hue, color, sat=None): for style_name in allowed_css:
""" style_file = style_name + ".css"
Generate theme files for gnome-shell-theme.gresource.xml if style_file in extracted_theme_files:
:param hue: color hue last_mode = style_name.split("-")[-1]
:param color: color name mode = last_mode if last_mode in allowed_modes else None
:param sat: color saturation self.__append_theme(style_name, mode=mode or self.mode, label=mode)
"""
for theme_prepare in self.themes: def __append_theme(self, theme_type: str, mode=None, label: Optional[str] = None):
if theme_prepare.should_label: """Helper to create theme objects"""
temp_folder = theme_prepare.theme.temp_folder theme = Theme(theme_type, self.colors_json, self.theme_folder,
main_styles = theme_prepare.theme.main_styles self.extracted_theme, self.temp_folder,
FilesLabeler(temp_folder, main_styles).append_label("light") mode=mode, is_filled=self.is_filled)
theme.prepare()
remove_keywords(theme_prepare.theme_file, "!important") theme_file = os.path.join(self.extracted_theme, f"{theme_type}.css")
remove_properties(theme_prepare.theme_file, "background-color", "color", "box-shadow", "border-radius") self.themes.append(ThemePrepare(theme=theme, theme_file=theme_file, label=label))
self.__add_gnome_styles(theme_prepare.theme)
theme_prepare.theme.install(hue, color, sat, destination=self.extracted_theme)
def __backup(self):
if self.__is_installed():
return
backup_line = Console.Line()
backup_line.update("Backing up default theme...")
subprocess.run(["cp", "-aT", self.gst, f"{self.gst}.backup"], cwd=self.destination_folder, check=True)
backup_line.success("Backed up default theme.")
def __generate_gresource_xml(self):
# list of files to add to gnome-shell-theme.gresource.xml
files = [f"<file>{file}</file>" for file in os.listdir(self.extracted_theme)]
nl = "\n" # fstring doesn't support newline character
return f"""<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gnome/shell/theme">
{nl.join(files)}
</gresource>
</gresources>"""
def prepare(self):
try:
gnome_version = gnome.gnome_version()
gnome_major = gnome_version.split(".")[0]
if int(gnome_major) >= 44:
self.themes += [
self.__create_theme("gnome-shell-light", mode='light', should_label=True, is_filled=self.is_filled),
self.__create_theme("gnome-shell-dark", mode='dark', is_filled=self.is_filled)
]
except Exception as e:
print(f"Error: {e}")
print("Using single theme.")
if not self.themes:
self.themes.append(
self.__create_theme(
"gnome-shell", mode=self.mode if self.mode else 'dark', is_filled=self.is_filled))
def install(self, hue, sat=None): def install(self, hue, sat=None):
""" """Install theme globally"""
Install theme globally
:param hue: color hue
:param sat: color saturation
"""
if os.geteuid() != 0: if os.geteuid() != 0:
raise Exception("Root privileges required to install GDM theme") raise Exception("Root privileges required to install GDM theme")
if self.__is_installed(): if self.__is_installed():
print("Theme is installed. Reinstalling...") Console.Line().info("Theme is installed. Reinstalling...")
self.gst += ".backup" self.gresource_file += ".backup"
self.__extract()
# generate theme files for global theme
self.__generate_themes(hue, 'Marble', sat) self.__generate_themes(hue, 'Marble', sat)
self.__backup()
# generate gnome-shell-theme.gresource.xml # generate gnome-shell-theme.gresource.xml
with open(f"{self.extracted_theme}/{self.destination_file}.xml", 'w') as gresource_xml: with open(f"{self.extracted_theme}/{self.destination_file}.xml", 'w') as gresource_xml:
generated_xml = self.__generate_gresource_xml() generated_xml = self.__generate_gresource_xml()
gresource_xml.write(generated_xml) gresource_xml.write(generated_xml)
self.__compile_resources()
# compile gnome-shell-theme.gresource.xml
compile_line = Console.Line()
compile_line.update("Compiling gnome-shell theme...")
subprocess.run(["glib-compile-resources" , f"{self.destination_file}.xml"],
cwd=self.extracted_theme, check=True)
compile_line.success("Theme compiled.")
# backup installed theme
self.__backup()
# install theme
install_line = Console.Line() install_line = Console.Line()
install_line.update("Moving compiled theme to system folder...") install_line.update("Moving compiled theme to system folder...")
subprocess.run(["sudo", "cp", "-f", subprocess.run(["sudo", "cp", "-f",
@@ -213,13 +145,86 @@ class GlobalTheme:
check=True) check=True)
install_line.success("Theme moved to system folder.") install_line.success("Theme moved to system folder.")
self.__update_alternatives()
def __is_installed(self) -> bool:
with open(self.gresource_file, "rb") as f:
return self.backup_trigger.encode() in f.read()
def __generate_themes(self, hue: int, color: str, sat: Optional[int]=None):
"""Generate theme files for gnome-shell-theme.gresource.xml"""
for theme_prepare in self.themes:
if theme_prepare.label is not None:
temp_folder = theme_prepare.theme.temp_folder
main_styles = theme_prepare.theme.main_styles
FilesLabeler(temp_folder, main_styles).append_label(theme_prepare.label)
remove_keywords(theme_prepare.theme_file, "!important")
remove_properties(theme_prepare.theme_file, "background-color", "color", "box-shadow", "border-radius")
self.__add_gnome_styles(theme_prepare.theme)
theme_prepare.theme.install(hue, color, sat, destination=self.extracted_theme)
def __add_gnome_styles(self, theme: Theme):
"""Add gnome styles to the start of the file"""
with open(f"{theme.destination_folder}/{theme.theme_type}.css", 'r') as gnome_theme:
gnome_styles = gnome_theme.read() + self.backup_trigger
theme.add_to_start(gnome_styles)
def __backup(self):
if self.__is_installed():
return
backup_line = Console.Line()
backup_line.update("Backing up default theme...")
subprocess.run(["cp", "-aT", self.gresource_file, f"{self.gresource_file}.backup"],
cwd=self.destination_folder, check=True)
backup_line.success("Backed up default theme.")
def __generate_gresource_xml(self):
files_to_include = []
for root, dirs, filenames in os.walk(self.extracted_theme):
for filename in filenames:
# Get a path relative to extracted_theme directory
full_path = os.path.join(root, filename)
rel_path = os.path.relpath(full_path, self.extracted_theme)
files_to_include.append(f"<file>{rel_path}</file>")
nl = "\n" # fstring doesn't support newline character
return f"""<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gnome/shell/theme">
{nl.join(files_to_include)}
</gresource>
</gresources>"""
def __compile_resources(self):
compile_line = Console.Line()
compile_line.update("Compiling gnome-shell theme...")
try:
subprocess.run(["glib-compile-resources",
"--sourcedir", self.extracted_theme,
"--target", f"{self.extracted_theme}/{self.destination_file}",
f"{self.destination_file}.xml"
],
cwd=self.extracted_theme, check=True)
except FileNotFoundError as e:
if "glib-compile-resources" in str(e):
self.__raise_gresource_error(e)
raise
compile_line.success("Theme compiled.")
def __update_alternatives(self):
link = os.path.join(self.destination_folder, config.ubuntu_gresource_link)
name = config.ubuntu_gresource_link
path = os.path.join(self.destination_folder, self.destination_file)
AlternativesUpdater.install_and_set(link, name, path)
def remove(self): def remove(self):
"""
Remove installed theme
"""
# use backup file if theme is installed
if self.__is_installed(): if self.__is_installed():
removing_line = Console.Line() removing_line = Console.Line()
removing_line.update("Theme is installed. Removing...") removing_line.update("Theme is installed. Removing...")
@@ -236,4 +241,88 @@ class GlobalTheme:
else: else:
Console.Line().error("Theme is not installed. Nothing to remove.") Console.Line().error("Theme is not installed. Nothing to remove.")
Console.Line().update("If theme is still installed globally, try reinstalling gnome-shell package.", icon="⚠️") Console.Line().warn("If theme is still installed globally, try reinstalling gnome-shell package.")
def __remove_alternatives(self):
name = config.ubuntu_gresource_link
path = os.path.join(self.destination_folder, self.destination_file)
AlternativesUpdater.remove(name, path)
class AlternativesUpdater:
@staticmethod
def ubuntu_specific(func):
"""
Decorator for Ubuntu-specific functionality.
Silently ignores errors when not running on Ubuntu systems.
"""
@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: Path, priority: int = 0):
AlternativesUpdater.install(link, name, path, priority)
AlternativesUpdater.set(name, path)
@staticmethod
@ubuntu_specific
def install(link: str, name: str, path: Path, 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: Path):
"""
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: Path):
"""
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.")

View File

@@ -43,6 +43,9 @@ class Console:
def warn(self, message): def warn(self, message):
self.update(message, "⚠️") self.update(message, "⚠️")
def info(self, message):
self.update(message, " ")
def _reserve_line(self): def _reserve_line(self):
"""Reserve a line for future updates""" """Reserve a line for future updates"""
with Console._print_lock: with Console._print_lock:

View File

@@ -1,7 +1,7 @@
import os import os
from typing import Tuple from typing import Tuple, TypeAlias
type LabeledFileGroup = Tuple[str, str] LabeledFileGroup: TypeAlias = Tuple[str, str]
class FilesLabeler: class FilesLabeler:
def __init__(self, directory: str, *args: str): def __init__(self, directory: str, *args: str):