-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathflatpaks_installer.py
More file actions
132 lines (109 loc) · 5.5 KB
/
flatpaks_installer.py
File metadata and controls
132 lines (109 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
import os, subprocess, shutil, json
from pathlib import Path
CACHE_FLATPAK = f"{Path.home()}/.var/app/io.github.vikdevelop.SaveDesktop/cache/tmp/workspace"
# Check Desktop Environment
desktop_env = os.getenv("XDG_CURRENT_DESKTOP")
if desktop_env in ['GNOME', 'ubuntu:GNOME', 'zorin:GNOME']:
environment = 'GNOME'
else:
environment = None
# Activate gsettings property
if environment:
if not subprocess.getoutput("gsettings get org.gnome.shell disable-user-extensions") == "false":
os.system("gsettings set org.gnome.shell disable-user-extensions false")
# Check if the required directories exist in the cache directory
if os.path.exists(CACHE_FLATPAK):
dest_dir = CACHE_FLATPAK
else:
dest_dir = None
if dest_dir:
os.chdir(dest_dir)
# Check Flatpak user preferences
with open(f"flatpak-prefs.json") as fl:
j = json.load(fl)
copy_data = j["copy-data"]
install_flatpaks = j["install-flatpaks"]
disabled_flatpaks = j["disabled-flatpaks"]
keep_flatpaks = j["keep-flatpaks"]
# Restore Flatpak user data archive
if copy_data:
print("[DEBUG] Copying the Flatpak's user data to ~/.var/app")
if os.path.exists(f"app"):
print("Copying the Flatpak apps' user data to the ~/.var/app directory (backward compatibility mode)")
os.system(f"cp -au ./app/ ~/.var/")
archive_file = "Flatpak_Apps/flatpak-apps-data.tgz" if os.path.exists("Flatpak_Apps/flatpak-apps-data.tgz") else "flatpak-apps-data.tgz"
if os.path.exists(archive_file):
tar_cmd = ["tar", "-xzvf", archive_file, "-C", f"{Path.home()}/.var"]
for d_app in disabled_flatpaks:
tar_cmd.extend([f"--exclude={d_app}", f"--exclude=app/{d_app}"])
subprocess.run(tar_cmd)
# Load Flatpaks from bash scripts
if install_flatpaks:
print("[DEBUG] Scanning the Bash scripts...")
if os.path.exists("Flatpak_Apps"):
installed_flatpaks_files = ['Flatpak_Apps/installed_flatpaks.sh', 'Flatpak_Apps/installed_user_flatpaks.sh']
else:
installed_flatpaks_files = ['installed_flatpaks.sh', 'installed_user_flatpaks.sh']
desired_flatpaks = {}
for file in installed_flatpaks_files:
if os.path.exists(file):
print(f"[DEBUG] Reading: {file}")
with open(file, 'r') as f:
for line in f:
if 'flatpak' in line and 'install' in line:
parts = line.split()
app_id = None
for part in parts:
if "." in part and not part.startswith("-"):
app_id = part
break
if not app_id:
continue
if app_id in disabled_flatpaks:
print(f"[SKIP] Disabled app: {app_id}")
continue
method = "--user" if "--user" in parts else "--system"
desired_flatpaks[app_id] = method
print(f"[DEBUG] Added to the installation queue: {app_id} ({method})")
if not desired_flatpaks:
print("[DEBUG] The installation queue is empty. It couldn't find any valid Flatpaks to install.")
# Get currently installed Flatpaks
system_flatpak_dir = '/var/lib/flatpak/app'
user_flatpak_dir = os.path.expanduser('~/.local/share/flatpak/app')
installed_apps = {}
for directory, method in [(system_flatpak_dir, '--system'), (user_flatpak_dir, '--user')]:
if os.path.exists(directory):
for app in os.listdir(directory):
if os.path.isdir(os.path.join(directory, app)):
installed_apps[app] = method
for app, method in desired_flatpaks.items():
if app not in installed_apps:
print(f"[INSTALL] Installing {app} ({method})")
if method == '--user':
os.system("flatpak remote-add --user --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo")
subprocess.run(['flatpak', 'install', method, 'flathub', app, '-y'])
else:
print(f"[INFO] {app} is available in the system.")
# Remove Flatpaks, which are not in the list (only if it's allowed by the user)
if not keep_flatpaks:
for app, method in installed_apps.items():
if app not in desired_flatpaks:
print(f"[REMOVE] {method.title()} Flatpak: {app}")
subprocess.run(['flatpak', 'uninstall', method, app, '--delete-data', '-y'])
# Remove orphaned ~/.var/app directories
user_var_app = Path.home() / ".var/app"
if user_var_app.exists():
for app_dir in user_var_app.iterdir():
if app_dir.is_dir() and app_dir.name not in desired_flatpaks:
print(f"[REMOVE] Orphaned Flatpak user data: {app_dir.name}")
shutil.rmtree(app_dir)
else:
print("Nothing to do.")
# Remove the autostart file after finishing the operations
autostart_file = f"{Path.home()}/.config/autostart/io.github.vikdevelop.SaveDesktop.Flatpak.desktop"
if os.path.exists(autostart_file):
os.remove(autostart_file)
# Remove the cache dir after finishing the operations
if os.path.exists(CACHE_FLATPAK):
shutil.rmtree(CACHE_FLATPAK)