1- import os , shutil , argparse
1+ import os , shutil , argparse , subprocess
22from gi .repository import GLib
33from savedesktop .globals import *
4+ from pathlib import Path
45
56# Helping functions
67def safe_copy (src : str , dst : str ):
@@ -17,7 +18,7 @@ def safe_copy(src: str, dst: str):
1718def safe_copytree (src : str , dst : str , ignore = None ):
1819 if os .path .isdir (src ):
1920 try :
20- shutil .copytree (src , dst , dirs_exist_ok = True , ignore = ignore )
21+ shutil .copytree (src , dst , dirs_exist_ok = True , symlinks = True , ignore = ignore )
2122 print (f"[OK] Copied dir: { src } → { dst } " )
2223 except Exception as e :
2324 print (f"[ERR] Dir copy failed { src } → { dst } : { e } " )
@@ -45,6 +46,9 @@ def safe_copytree(src: str, dst: str, ignore=None):
4546 ("xdg-data" , f"{ home } /.local/share" ),
4647]
4748
49+ # The ~/Desktop folder definition
50+ desktop_dir = Path (GLib .get_user_special_dir (GLib .UserDirectory .DIRECTORY_DESKTOP ))
51+
4852class Save :
4953 def __init__ (self ):
5054
@@ -71,24 +75,27 @@ def __init__(self):
7175 safe_copytree (f"{ home } /.local/share/backgrounds" , "backgrounds" )
7276 safe_copytree (f"{ home } /.local/share/wallpapers" , "xdg-data/wallpapers" )
7377 if settings ["save-icons" ]:
74- safe_copytree (f"{ home } /.icons" , ".icons" )
75- safe_copytree (f"{ home } /.local/share/icons" , "icons" )
78+ xdg_path = f"{ home } /.local/share"
79+ legacy_path = f"{ home } "
80+ if os .path .isdir (f"{ xdg_path } /icons" ) and os .listdir (f"{ xdg_path } /icons" ):
81+ subprocess .run (["tar" , "-czf" , "icon-themes.tgz" , "-C" , xdg_path , "icons" ])
82+ if os .path .isdir (f"{ legacy_path } /.icons" ) and os .listdir (f"{ legacy_path } /.icons" ):
83+ subprocess .run (["tar" , "-czf" , "icon-themes-legacy.tgz" , "-C" , legacy_path , ".icons" ])
84+ print ("[OK] Saving icons" )
7685
7786 # Desktop folder
7887 if settings ["save-desktop-folder" ]:
79- desktop_dir = GLib .get_user_special_dir (GLib .UserDirectory .DIRECTORY_DESKTOP )
80- if desktop_dir :
81- safe_copytree (desktop_dir , "Desktop" )
88+ subprocess .run (["tar" , "-czf" , "desktop-folder.tgz" , "-C" , str (desktop_dir .parent ), str (desktop_dir .name )])
8289 safe_copytree (f"{ home } /.local/share/gvfs-metadata" , "gvfs-metadata" )
90+ print ("[OK] Saving ~/Desktop folder" )
8391
8492 # Flatpak apps and their data
8593 if flatpak :
8694 if settings ["save-installed-flatpaks" ]:
87- print ("saving list of installed Flatpak apps" )
8895 os .system ("ls /var/lib/flatpak/app/ | awk '{print \" flatpak install --system \" $1 \" -y\" }' > ./installed_flatpaks.sh" )
8996 os .system ("ls ~/.local/share/flatpak/app | awk '{print \" flatpak install --user \" $1 \" -y\" }' > ./installed_user_flatpaks.sh" )
97+ print ("[OK] Saving list of installed Flatpak apps" )
9098 if settings ["save-flatpak-data" ]:
91- print ("saving user data of installed Flatpak apps" )
9299 self .save_flatpak_data ()
93100
94101 # Environment specific
@@ -116,24 +123,15 @@ def save_flatpak_data(self):
116123 gsettings = settings ["disabled-flatpak-apps-data" ]
117124 black_list = gsettings # convert GSettings property to a list
118125
119- os .makedirs (f"{ CACHE } /workspace/app" , exist_ok = True )
120- destdir = f"{ CACHE } /workspace/app"
121-
122- # copy Flatpak apps data
123- for item in os .listdir (f"{ home } /.var/app" ):
124- if item not in black_list and item != "cache" :
125- source_path = os .path .join (f"{ home } /.var/app" , item )
126- destination_path = os .path .join (destdir , item )
127- if os .path .isdir (source_path ):
128- try :
129- shutil .copytree (source_path , destination_path , ignore = shutil .ignore_patterns ('cache' ))
130- except Exception as e :
131- print (f"Error copying directory { source_path } : { e } " )
132- else :
133- try :
134- shutil .copy2 (source_path , destination_path )
135- except Exception as e :
136- print (f"Error copying file { source_path } : { e } " )
126+ cmd = ["tar" , "-czf" , "flatpak-apps-data.tgz" , "--exclude=*/cache" ]
127+
128+ for apps in black_list :
129+ cmd .append (f"--exclude={ apps } " )
130+
131+ cmd .extend (["-C" , f"{ home } /.var" , "app" ])
132+
133+ subprocess .run (cmd )
134+ print ("[OK] Saving Flatpak apps' data" )
137135
138136class Import :
139137 def __init__ (self ):
@@ -156,12 +154,11 @@ def __init__(self):
156154 safe_copytree ("gnome-shell" , f"{ home } /.local/share/gnome-shell" )
157155 safe_copytree ("cinnamon" , f"{ home } /.local/share/cinnamon" )
158156 safe_copytree ("plasma" , f"{ home } /.local/share/plasma" )
159- safe_copytree ("Desktop" , GLib .get_user_special_dir (GLib .UserDirectory .DIRECTORY_DESKTOP ))
160157 safe_copytree ("gvfs-metadata" , f"{ home } /.local/share/gvfs-metadata" )
161158 safe_copytree ("backgrounds" , f"{ home } /.local/share/backgrounds" )
162159 safe_copytree ("wallpapers" , f"{ home } /.local/share/wallpapers" )
163- safe_copytree ( "icons" , f" { home } /.local/share/icons" )
164- safe_copytree ( ".icons" , f" { home } /.icons" )
160+ self . import_desktop_folder ( )
161+ self . import_icons ( )
165162
166163 # Environment specific
167164 if environment :
@@ -177,9 +174,29 @@ def __init__(self):
177174 print (f"[WARN] Unknown DE: { environment_key } " )
178175
179176 if flatpak :
180- if any (os .path .exists (path ) for path in ["app" , "installed_flatpaks.sh" , "installed_user_flatpaks.sh" ]):
177+ if any (os .path .exists (path ) for path in ["app" , "flatpak-apps-data.tgz" , " installed_flatpaks.sh" , "installed_user_flatpaks.sh" ]):
181178 self .create_flatpak_autostart ()
182179
180+ # Extract an archive with the Desktop folder
181+ def import_desktop_folder (self ):
182+ if os .path .exists ("desktop-folder.tgz" ):
183+ subprocess .run (["tar" , "-xzvf" , "desktop-folder.tgz" , "-C" , str (desktop_dir .parent )])
184+ print ("[OK] Extracting a Desktop archive" )
185+ else :
186+ safe_copytree ("Desktop" , GLib .get_user_special_dir (GLib .UserDirectory .DIRECTORY_DESKTOP ))
187+
188+ # Extract an archive with icon themes
189+ def import_icons (self ):
190+ if os .path .exists (f"icon-themes.tgz" ):
191+ subprocess .run (["tar" , "-xzvf" , "icon-themes.tgz" , "-C" , f"{ home } /.local/share/" ])
192+ print ("[OK] Extracting a XDG icons archive" )
193+ if os .path .exists (f"icon-themes-legacy.tgz" ):
194+ print (f"[OK] Extracting a legacy icons archive" )
195+ subprocess .run (["tar" , "-xzvf" , "icon-themes-legacy.tgz" , "-C" , f"{ home } /" ])
196+ else :
197+ safe_copytree ("icons" , f"{ home } /.local/share/icons" )
198+ safe_copytree (".icons" , f"{ home } /.icons" )
199+
183200 # Create an autostart file to install Flatpaks from a list after the next login
184201 def create_flatpak_autostart (self ):
185202 os .system (f"cp /app/share/savedesktop/savedesktop/core/flatpaks_installer.py { CACHE } /workspace" )
0 commit comments