11import os , sys , shutil , subprocess , argparse , re
22from savedesktop .globals import *
3+ from savedesktop .core .password_store import PasswordStore
34
45parser = argparse .ArgumentParser ()
56parser .add_argument ("-c" , "--create" , help = "Create archive" , type = str )
67parser .add_argument ("-u" , "--unpack" , help = "Unpack archive" , type = str )
78args = parser .parse_args ()
89
10+ TEMP_CACHE = f"{ CACHE } /workspace"
11+
12+ # Cleanup the cache dir before saving
13+ def cleanup_cache_dir ():
14+ print ("Cleaning up the cache directory" )
15+ try :
16+ shutil .rmtree (TEMP_CACHE )
17+ except :
18+ pass
19+ os .makedirs (TEMP_CACHE , exist_ok = True )
20+ os .chdir (TEMP_CACHE )
21+
922# Get password entered in the "Create a new password" dialog from the temporary file
1023def get_password ():
1124 temp_file = f"{ CACHE } /temp_file"
1225 if os .path .exists (temp_file ):
1326 with open (temp_file ) as tmp :
1427 return tmp .read ().strip ()
1528 else :
16- return None
29+ p = PasswordStore ()
30+ return p .password
1731
1832# Remove above temporary file
1933def remove_temp_file ():
20- try :
34+ if os . path . exists ( f" { CACHE } /temp_file" ) :
2135 os .remove (f"{ CACHE } /temp_file" )
22- except FileNotFoundError :
23- pass
2436
2537class Create :
2638 def __init__ (self ):
2739 self .start_saving ()
2840
2941 def start_saving (self ):
30- self . _cleanup_cache_dir ()
42+ cleanup_cache_dir ()
3143 subprocess .run ([sys .executable , "-m" , "savedesktop.core.config" , "--save" ], check = True , env = {** os .environ , "PYTHONPATH" : f"{ app_prefix } " })
3244
3345 print ("Creating and moving the configuration archive or folder to the user-defined directory" )
3446
35- if settings ["save-without-archive" ]:
47+ # In the periodic saving mode, it's not allowed to save the
48+ # configuration without creating the archive
49+ if settings ["save-without-archive" ] and not args .create == settings ["periodic-saving-folder" ]:
3650 self ._copy_config_to_folder ()
3751 else :
3852 self ._create_archive ()
@@ -41,25 +55,14 @@ def start_saving(self):
4155 print ("Configuration saved successfully." )
4256 remove_temp_file ()
4357
44- # Cleanup the cache dir before saving
45- def _cleanup_cache_dir (self ):
46- print ("Cleaning up the cache directory" )
47- save_cache_dir = f"{ CACHE } /save_config"
48- try :
49- shutil .rmtree (save_cache_dir )
50- except :
51- pass
52- os .makedirs (save_cache_dir , exist_ok = True )
53- os .chdir (save_cache_dir )
54-
5558 # Copy the configuration folder to the user-defined directory
5659 def _copy_config_to_folder (self ):
57- open (f"{ CACHE } /save_config/ .folder.sd" , "w" ).close ()
60+ open (f".folder.sd" , "w" ).close ()
5861
5962 if os .path .exists (args .create ):
6063 shutil .rmtree (args .create )
6164
62- shutil .move (f" { CACHE } /save_config" , f"{ args .create } " )
65+ shutil .move (TEMP_CACHE , f"{ args .create } " )
6366
6467 # Create a new ZIP archive with 7-Zip
6568 def _create_archive (self ):
@@ -85,31 +88,15 @@ def start_importing(self):
8588 self .import_file = args .unpack
8689 self .import_folder = args .unpack
8790
88- self . _cleanup_cache_dir ()
91+ cleanup_cache_dir ()
8992 self ._check_config_type ()
9093
9194 self ._replace_home_in_files ("." , home )
9295 subprocess .run ([sys .executable , "-m" , "savedesktop.core.config" , "--import_" ], check = True , env = {** os .environ , "PYTHONPATH" : f"{ app_prefix } " })
9396
94- self ._remove_status_file ()
95-
9697 print ("Configuration imported successfully." )
9798 remove_temp_file ()
9899
99- def _cleanup_cache_dir (self ):
100- # Cleanup the cache dir before importing
101- print ("Cleaning up the cache directory" )
102- imp_cache_dir = f"{ CACHE } /import_config"
103- try :
104- shutil .rmtree (imp_cache_dir )
105- except :
106- pass
107- os .makedirs (imp_cache_dir , exist_ok = True )
108- os .chdir (imp_cache_dir )
109-
110- # Create a txt file to prevent removing the cache's content after closing the app window
111- open ("import_status" , "w" ).close ()
112-
113100 # Check, if the input is archive or folder
114101 def _check_config_type (self ):
115102 if self .import_file .endswith (".sd.zip" ) or self .import_file .endswith (".sd.tar.gz" ):
@@ -130,7 +117,7 @@ def _check_config_type(self):
130117
131118 # Copy the user-defined folder to the cache directory
132119 def _copy_folder_to_cache (self ):
133- shutil .copytree (self .import_folder , f" { CACHE } /import_config" , dirs_exist_ok = True , ignore_dangling_symlinks = True )
120+ shutil .copytree (self .import_folder , TEMP_CACHE , dirs_exist_ok = True , ignore_dangling_symlinks = True )
134121
135122 # Unpack the ZIP archive with 7-Zip
136123 def _unpack_zip_archive (self ):
@@ -148,13 +135,13 @@ def _unpack_zip_archive(self):
148135 print ("Checking password is completed." )
149136
150137 subprocess .run (
151- ['7z' , 'x' , '-y' , f'-p{ password } ' , self .import_file , f'-o{ CACHE } /import_config ' ],
138+ ['7z' , 'x' , '-y' , f'-p{ password } ' , self .import_file , f'-o{ TEMP_CACHE } ' ],
152139 capture_output = False , text = True , check = True
153140 )
154141
155142 # Unpack a legacy archive with Tarball (for backward compatibility)
156143 def _unpack_tar_archive (self ):
157- subprocess .run (["tar" , "-xzf" , self .import_file , "-C" , f"{ CACHE } /import_config " ],capture_output = True , text = True , check = True )
144+ subprocess .run (["tar" , "-xzf" , self .import_file , "-C" , f"{ TEMP_CACHE } " ],capture_output = True , text = True , check = True )
158145
159146 # Replace original /home/$USER path with actual path in the dconf-settings.ini file and other XML files
160147 def _replace_home_in_files (self , root , home , patterns = (".xml" , ".ini" )):
@@ -171,15 +158,6 @@ def _replace_home_in_files(self, root, home, patterns=(".xml", ".ini")):
171158 f .write (new_text )
172159 print (f"Updated /home/$USER path in: { path } " )
173160
174- # Remove the "import_status" file if the condition is met
175- def _remove_status_file (self ):
176- if all (not os .path .exists (p ) for p in [
177- f"{ CACHE } /import_config/app" ,
178- f"{ CACHE } /import_config/installed_flatpaks.sh" ,
179- f"{ CACHE } /import_config/installed_user_flatpaks.sh"
180- ]):
181- os .remove ("import_status" )
182-
183161if args .create :
184162 Create ()
185163elif args .unpack :
0 commit comments