diff --git a/scripts/outer_scripts_to_review/sfc_scan.py b/scripts/outer_scripts_to_review/sfc_scan.py deleted file mode 100644 index ec85836a..00000000 --- a/scripts/outer_scripts_to_review/sfc_scan.py +++ /dev/null @@ -1,40 +0,0 @@ -# Wizard Kit: Check, and possibly repair, system file health via SFC - -import os -import sys - -# Init -sys.path.append(os.path.dirname(os.path.realpath(__file__))) -from functions.repairs import * -init_global_vars() -os.system('title {}: SFC Tool'.format(KIT_NAME_FULL)) -set_log_file('SFC Tool.log') - -if __name__ == '__main__': - try: - stay_awake() - clear_screen() - print_info('{}: SFC Tool\n'.format(KIT_NAME_FULL)) - other_results = { - 'Error': { - 'CalledProcessError': 'Unknown Error', - }, - 'Warning': { - 'GenericRepair': 'Repaired', - }} - if ask('Run a SFC scan now?'): - try_and_print(message='SFC scan...', - function=run_sfc_scan, other_results=other_results) - else: - abort() - - # Done - print_standard('\nDone.') - pause('Press Enter to exit...') - exit_script() - except SystemExit as sys_exit: - exit_script(sys_exit.code) - except: - major_exception() - -# vim: sts=2 sw=2 ts=2 diff --git a/scripts/sfc_scan.py b/scripts/sfc_scan.py new file mode 100644 index 00000000..02d84f77 --- /dev/null +++ b/scripts/sfc_scan.py @@ -0,0 +1,35 @@ +"""Wizard Kit: Check, and possibly repair, system file health via SFC""" +# vim: sts=2 sw=2 ts=2 + +import wk + + +def main(): + """Run SFC and report result.""" + title = f'{wk.cfg.main.KIT_NAME_FULL}: SFC Tool' + try_print = wk.std.TryAndPrint() + wk.std.clear_screen() + wk.std.set_title(title) + wk.std.print_info(title) + print('') + + # Ask + if not wk.std.ask('Run a SFC scan now?'): + wk.std.abort() + print('') + + # Run + try_print.run('SFC scan...', wk.os.win.run_sfc_scan) + + # Done + print('Done') + wk.std.pause('Press Enter to exit...') + + +if __name__ == '__main__': + try: + main() + except SystemExit: + raise + except: #pylint: disable=bare-except + wk.std.major_exception() diff --git a/scripts/wk/os/win.py b/scripts/wk/os/win.py index 85073e46..2f3731b7 100644 --- a/scripts/wk/os/win.py +++ b/scripts/wk/os/win.py @@ -1,9 +1,22 @@ """WizardKit: Windows Functions""" # vim: sts=2 sw=2 ts=2 -from wk.std import run_program +import logging +import os +import pathlib +import re +import time + +from wk import cfg +from wk.io import non_clobber_path +from wk.std import ( + GenericError, + GenericWarning, + run_program, + ) # STATIC VARIABLES +LOG = logging.getLogger(__name__) REG_MSISERVER = r'HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\MSIServer' @@ -38,5 +51,41 @@ def enable_safemode_msi(): run_program(cmd) +def run_sfc_scan(): + """Run SFC and save results.""" + cmd = ['sfc', '/scannow'] + log_path = pathlib.Path( + '{drive}/{short}/Logs/{date}/Tools/SFC.log'.format( + drive=os.environ.get('SYSTEMDRIVE', 'C:'), + short=cfg.main.KIT_NAME_SHORT, + date=time.strftime('%Y-%m-%d'), + )) + err_path = log_path.with_suffix('.err') + + # Run SFC + proc = run_program(cmd, check=False) + + # Fix paths + log_path = non_clobber_path(log_path) + err_path = non_clobber_path(err_path) + + # Save output + output = proc.stdout.replace('\0', '') + errors = proc.stderr.replace('\0', '') + os.makedirs(log_path.parent, exist_ok=True) + with open(log_path, 'w') as _f: + _f.write(output) + with open(err_path, 'w') as _f: + _f.write(errors) + + # Check result + if re.findall(r'did\s+not\s+find\s+any\s+integrity\s+violations', output): + pass + elif re.findall(r'successfully\s+repaired\s+them', output): + raise GenericWarning('Repaired') + else: + raise GenericError + + if __name__ == '__main__': print("This file is not meant to be called directly.")