From 973dad32403227fc9c023daf170faf7ff7b817cc Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Thu, 29 Apr 2021 19:37:09 -0600 Subject: [PATCH] Add "auto_" windows updates functions --- scripts/auto_repairs.py | 8 ++++---- scripts/wk/io.py | 6 ++++++ scripts/wk/repairs/win.py | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/scripts/auto_repairs.py b/scripts/auto_repairs.py index 55aff945..aabb9ba6 100644 --- a/scripts/auto_repairs.py +++ b/scripts/auto_repairs.py @@ -56,13 +56,13 @@ BASE_MENUS = { MenuEntry('Enable System Restore', 'auto_system_restore_enable'), MenuEntry('Set System Restore Size', 'auto_system_restore_set_size'), MenuEntry('Create System Restore', 'auto_system_restore_create'), - MenuEntry('Backup Browsers', placeholder_function), + #MenuEntry('Backup Browsers', #TODO), MenuEntry('Backup Power Plans', 'auto_backup_power_plans'), MenuEntry('Backup Registry', 'auto_backup_registry'), ), 'Windows Repairs': ( - MenuEntry('Disable Windows Updates', placeholder_function), - MenuEntry('Reset Windows Updates', placeholder_function), + MenuEntry('Disable Windows Updates', 'auto_windows_updates_disable'), + MenuEntry('Reset Windows Updates', 'auto_windows_updates_reset'), MenuEntry('Reboot', placeholder_reboot), MenuEntry('CHKDSK', placeholder_function), MenuEntry('DISM RestoreHealth', 'auto_dism'), @@ -85,7 +85,7 @@ BASE_MENUS = { 'Manual Steps': ( MenuEntry('AdwCleaner', placeholder_function), MenuEntry('IO Bit Uninstaller', placeholder_function), - MenuEntry('Enable Windows Updates', placeholder_function), + MenuEntry('Enable Windows Updates', 'auto_windows_updates_enable'), ), }, 'Options': ( diff --git a/scripts/wk/io.py b/scripts/wk/io.py index f81d9a26..2cbbef46 100644 --- a/scripts/wk/io.py +++ b/scripts/wk/io.py @@ -192,5 +192,11 @@ def recursive_copy(source, dest, overwrite=False): raise FileExistsError(f'Refusing to delete file: {dest}') +def rename_item(path, new_path): + """Rename item, returns pathlib.Path.""" + path = pathlib.Path(path) + return path.rename(new_path) + + if __name__ == '__main__': print("This file is not meant to be called directly.") diff --git a/scripts/wk/repairs/win.py b/scripts/wk/repairs/win.py index eb080d95..206683ea 100644 --- a/scripts/wk/repairs/win.py +++ b/scripts/wk/repairs/win.py @@ -13,6 +13,7 @@ from subprocess import CalledProcessError, DEVNULL from wk.cfg.main import KIT_NAME_FULL from wk.exe import get_procs, run_program, popen_program, wait_for_procs +from wk.io import delete_folder, rename_item from wk.kit.tools import run_tool from wk.log import format_log_path, update_log_path from wk.os.win import ( @@ -516,6 +517,30 @@ def auto_system_restore_set_size(group, name): save_settings(group, name, result=result) +def auto_windows_updates_disable(group, name): + """Disable Windows Updates.""" + result = TRY_PRINT.run('Disable Windows Updates...', disable_windows_updates) + if result['Failed']: + # Reboot and try again? + reboot() + save_settings(group, name, result=result) + + +def auto_windows_updates_enable(group, name): + """Enable Windows Updates.""" + result = TRY_PRINT.run('Enable Windows Updates...', enable_windows_updates) + save_settings(group, name, result=result) + + +def auto_windows_updates_reset(group, name): + """Reset Windows Updates.""" + result = TRY_PRINT.run('Reset Windows Updates...', reset_windows_updates) + if result['Failed']: + # Reboot and try again? + reboot() + save_settings(group, name, result=result) + + # Misc Functions def set_backup_path(name, date=False): """Set backup path, returns pathlib.Path.""" @@ -551,8 +576,8 @@ def create_system_restore_point(): def disable_windows_updates(): """Disable and stop Windows Updates.""" - stop_service('wuauserv') disable_service('wuauserv') + stop_service('wuauserv') def enable_windows_updates(): @@ -601,6 +626,16 @@ def reboot(timeout=10): raise SystemExit +def reset_windows_updates(): + """Reset Windows Updates.""" + system_root = os.environ.get('SYSTEMROOT', 'C:/Windows') + rename_item( + f'{system_root}/SoftwareDistribution', + f'{system_root}/SoftwareDistribution.old', + ) + delete_folder(f'{system_root}/SoftwareDistribution.old', force=True) + + def run_chkdsk_offline(): """Set filesystem 'dirty bit' to force a CHKDSK during startup.""" cmd = ['fsutil', 'dirty', 'set', os.environ.get('SYSTEMDRIVE', 'C:')]