From 6c229392e0ca19c6badb34838a062df8a7af6a12 Mon Sep 17 00:00:00 2001 From: leminlimez <59540996+leminlimez@users.noreply.github.com> Date: Thu, 13 Mar 2025 22:01:50 -0400 Subject: [PATCH] error handling over whole apply statement --- devicemanagement/device_manager.py | 230 ++++++++++++++--------------- 1 file changed, 115 insertions(+), 115 deletions(-) diff --git a/devicemanagement/device_manager.py b/devicemanagement/device_manager.py index d894f9b..ca3073f 100644 --- a/devicemanagement/device_manager.py +++ b/devicemanagement/device_manager.py @@ -286,134 +286,134 @@ class DeviceManager: ## APPLYING OR REMOVING TWEAKS AND RESTORING def apply_changes(self, resetting: bool = False, update_label=lambda x: None): - # set the tweaks and apply - # first open the file in read mode - update_label("Applying changes to files...") - gestalt_plist = None - if self.data_singleton.gestalt_path != None: - with open(self.data_singleton.gestalt_path, 'rb') as in_fp: - gestalt_plist = plistlib.load(in_fp) - # create the other plists - flag_plist: dict = {} - eligibility_files = None - ai_file = None - basic_plists: dict = {} - basic_plists_ownership: dict = {} - files_data: dict = {} - uses_domains: bool = False - # create the restore file list - files_to_restore: dict[FileToRestore] = [ - ] - tmp_pb_dir = None # temporary directory for unzipping pb files + try: + # set the tweaks and apply + # first open the file in read mode + update_label("Applying changes to files...") + gestalt_plist = None + if self.data_singleton.gestalt_path != None: + with open(self.data_singleton.gestalt_path, 'rb') as in_fp: + gestalt_plist = plistlib.load(in_fp) + # create the other plists + flag_plist: dict = {} + eligibility_files = None + ai_file = None + basic_plists: dict = {} + basic_plists_ownership: dict = {} + files_data: dict = {} + uses_domains: bool = False + # create the restore file list + files_to_restore: dict[FileToRestore] = [ + ] + tmp_pb_dir = None # temporary directory for unzipping pb files - # set the plist keys - if not resetting: - for tweak_name in tweaks: - tweak = tweaks[tweak_name] - if isinstance(tweak, FeatureFlagTweak): - flag_plist = tweak.apply_tweak(flag_plist) - elif isinstance(tweak, EligibilityTweak): - eligibility_files = tweak.apply_tweak() - elif isinstance(tweak, AITweak): - ai_file = tweak.apply_tweak() - elif isinstance(tweak, BasicPlistTweak) or isinstance(tweak, RdarFixTweak) or isinstance(tweak, AdvancedPlistTweak): - basic_plists = tweak.apply_tweak(basic_plists, self.allow_risky_tweaks) - basic_plists_ownership[tweak.file_location] = tweak.owner - if tweak.enabled and tweak.owner == 0: - uses_domains = True - elif isinstance(tweak, NullifyFileTweak): - tweak.apply_tweak(files_data) - if tweak.enabled and tweak.file_location.value.startswith("/var/mobile/"): - uses_domains = True - elif isinstance(tweak, PosterboardTweak): - tmp_pb_dir = TemporaryDirectory() - tweak.apply_tweak(files_to_restore=files_to_restore, output_dir=tmp_pb_dir.name) - else: - if gestalt_plist != None: - gestalt_plist = tweak.apply_tweak(gestalt_plist) - elif tweak.enabled: - # no mobilegestalt file provided but applying mga tweaks, give warning - show_error_msg("No mobilegestalt file provided! Please select your file to apply mobilegestalt tweaks.") - update_label("Failed.") - return - # set the custom gestalt keys - if gestalt_plist != None: - gestalt_plist = CustomGestaltTweaks.apply_tweaks(gestalt_plist) - - gestalt_data = None - if resetting: - gestalt_data = b"" - elif gestalt_plist != None: - gestalt_data = plistlib.dumps(gestalt_plist) - - # Generate backup - update_label("Generating backup...") - self.concat_file( - contents=plistlib.dumps(flag_plist), - path="/var/preferences/FeatureFlags/Global.plist", - files_to_restore=files_to_restore - ) - self.add_skip_setup(files_to_restore, uses_domains) - if gestalt_data != None: + # set the plist keys + if not resetting: + for tweak_name in tweaks: + tweak = tweaks[tweak_name] + if isinstance(tweak, FeatureFlagTweak): + flag_plist = tweak.apply_tweak(flag_plist) + elif isinstance(tweak, EligibilityTweak): + eligibility_files = tweak.apply_tweak() + elif isinstance(tweak, AITweak): + ai_file = tweak.apply_tweak() + elif isinstance(tweak, BasicPlistTweak) or isinstance(tweak, RdarFixTweak) or isinstance(tweak, AdvancedPlistTweak): + basic_plists = tweak.apply_tweak(basic_plists, self.allow_risky_tweaks) + basic_plists_ownership[tweak.file_location] = tweak.owner + if tweak.enabled and tweak.owner == 0: + uses_domains = True + elif isinstance(tweak, NullifyFileTweak): + tweak.apply_tweak(files_data) + if tweak.enabled and tweak.file_location.value.startswith("/var/mobile/"): + uses_domains = True + elif isinstance(tweak, PosterboardTweak): + tmp_pb_dir = TemporaryDirectory() + tweak.apply_tweak(files_to_restore=files_to_restore, output_dir=tmp_pb_dir.name) + else: + if gestalt_plist != None: + gestalt_plist = tweak.apply_tweak(gestalt_plist) + elif tweak.enabled: + # no mobilegestalt file provided but applying mga tweaks, give warning + show_error_msg("No mobilegestalt file provided! Please select your file to apply mobilegestalt tweaks.") + update_label("Failed.") + return + # set the custom gestalt keys + if gestalt_plist != None: + gestalt_plist = CustomGestaltTweaks.apply_tweaks(gestalt_plist) + + gestalt_data = None + if resetting: + gestalt_data = b"" + elif gestalt_plist != None: + gestalt_data = plistlib.dumps(gestalt_plist) + + # Generate backup + update_label("Generating backup...") self.concat_file( - contents=gestalt_data, - path="/var/containers/Shared/SystemGroup/systemgroup.com.apple.mobilegestaltcache/Library/Caches/com.apple.MobileGestalt.plist", + contents=plistlib.dumps(flag_plist), + path="/var/preferences/FeatureFlags/Global.plist", files_to_restore=files_to_restore ) - if eligibility_files: - new_eligibility_files: dict[FileToRestore] = [] - if not self.get_current_device_supported(): - # update the files - for file in eligibility_files: - self.concat_file( - contents=file.contents, - path=file.restore_path, - files_to_restore=new_eligibility_files - ) - else: - new_eligibility_files = eligibility_files - files_to_restore += new_eligibility_files - if ai_file != None: - self.concat_file( - contents=ai_file.contents, - path=ai_file.restore_path, - files_to_restore=files_to_restore - ) - for location, plist in basic_plists.items(): - ownership = basic_plists_ownership[location] - self.concat_file( - contents=plistlib.dumps(plist), - path=location.value, - files_to_restore=files_to_restore, - owner=ownership, group=ownership - ) - for location, data in files_data.items(): - self.concat_file( - contents=data, - path=location.value, - files_to_restore=files_to_restore, - owner=ownership, group=ownership - ) - # reset basic tweaks - if resetting: - empty_data = plistlib.dumps({}) - for location in FileLocationsList: + self.add_skip_setup(files_to_restore, uses_domains) + if gestalt_data != None: self.concat_file( - contents=empty_data, - path=location.value, + contents=gestalt_data, + path="/var/containers/Shared/SystemGroup/systemgroup.com.apple.mobilegestaltcache/Library/Caches/com.apple.MobileGestalt.plist", files_to_restore=files_to_restore ) - if self.allow_risky_tweaks: - for location in RiskyFileLocationsList: + if eligibility_files: + new_eligibility_files: dict[FileToRestore] = [] + if not self.get_current_device_supported(): + # update the files + for file in eligibility_files: + self.concat_file( + contents=file.contents, + path=file.restore_path, + files_to_restore=new_eligibility_files + ) + else: + new_eligibility_files = eligibility_files + files_to_restore += new_eligibility_files + if ai_file != None: + self.concat_file( + contents=ai_file.contents, + path=ai_file.restore_path, + files_to_restore=files_to_restore + ) + for location, plist in basic_plists.items(): + ownership = basic_plists_ownership[location] + self.concat_file( + contents=plistlib.dumps(plist), + path=location.value, + files_to_restore=files_to_restore, + owner=ownership, group=ownership + ) + for location, data in files_data.items(): + self.concat_file( + contents=data, + path=location.value, + files_to_restore=files_to_restore, + owner=ownership, group=ownership + ) + # reset basic tweaks + if resetting: + empty_data = plistlib.dumps({}) + for location in FileLocationsList: self.concat_file( contents=empty_data, path=location.value, files_to_restore=files_to_restore ) + if self.allow_risky_tweaks: + for location in RiskyFileLocationsList: + self.concat_file( + contents=empty_data, + path=location.value, + files_to_restore=files_to_restore + ) - # restore to the device - update_label("Restoring to device...") - try: + # restore to the device + update_label("Restoring to device...") restore_files(files=files_to_restore, reboot=self.auto_reboot, lockdown_client=self.data_singleton.current_device.ld) if tmp_pb_dir != None: tmp_pb_dir.cleanup()