Skip to content

Instantly share code, notes, and snippets.

@lpinner
Last active October 22, 2020 22:28
Show Gist options
  • Save lpinner/fd70cf6c4a45f76c7a4e6b339c4a785a to your computer and use it in GitHub Desktop.
Save lpinner/fd70cf6c4a45f76c7a4e6b339c4a785a to your computer and use it in GitHub Desktop.
Copy user or system GTK themes to flatpak runtimes
#!/usr/bin/python3
#licensed under the Apache License, Version 2.0 https://www.apache.org/licenses/LICENSE-2.0
""" Copy GTK themes to flatpak runtimes """
import sys
import shutil
import subprocess
from pathlib import Path
encoding = sys.getfilesystemencoding()
# Current theme
theme = subprocess.check_output(
('gsettings', 'get', 'org.gnome.desktop.interface', 'gtk-theme'),
encoding=encoding).strip().strip("'")
# All user flatpaks
flatpaks = subprocess.check_output(
('flatpak', 'list', '--columns=ref', '--user'),
encoding=encoding).split('\n')
# Gnome runtimes
platforms = [f for f in flatpaks if 'org.gnome.Platform' in f]
# Flatpak themes
themes = [f for f in flatpaks if 'org.gtk.Gtk3theme' in f]
# Theme paths
usertheme = Path.home() / ".local/share/themes" / theme
systemtheme = Path("/usr/share/themes") / theme
# Don't need to do anything if theme is also available as a flatpak
if any([f'.{theme}/' in f for f in themes]):
print(f'Flatpak theme exists for {theme}, nothing to do')
sys.exit(0)
# Where is the theme?
if usertheme.is_dir():
themedir = usertheme
elif systemtheme.is_dir():
themedir = systemtheme
else:
print(f'{theme} not found in "~/.local/share/themes" or "/usr/share/themes/"')
sys.exit(1)
# Copy theme to gnome runtimes
for p in platforms:
print(f"Copying {theme} to {p}")
platformdir = Path.home() / ".local/share/flatpak/runtime" / p / "active/files/share/themes"
shutil.copytree(themedir, platformdir / theme, dirs_exist_ok=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment