diff --git a/scripts/activate.py b/scripts/activate.py new file mode 100644 index 00000000..0affc19a --- /dev/null +++ b/scripts/activate.py @@ -0,0 +1,31 @@ +"""Wizard Kit: Activate Windows using a BIOS key""" +# vim: sts=2 sw=2 ts=2 + +import wk + + +def main(): + """Attempt to activate Windows and show result.""" + title = f'{wk.cfg.main.KIT_NAME_FULL}: Activation Tool' + try_print = wk.std.TryAndPrint() + wk.std.clear_screen() + wk.std.set_title(title) + wk.std.print_info(title) + print('') + + # Attempt activation + try_print.run('Attempting activation...', wk.os.win.activate_with_bios) + + # Done + print('') + 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/outer_scripts_to_review/activate.py b/scripts/outer_scripts_to_review/activate.py deleted file mode 100644 index fa54fa5d..00000000 --- a/scripts/outer_scripts_to_review/activate.py +++ /dev/null @@ -1,63 +0,0 @@ -# Wizard Kit: Activate Windows using various methods - -import os -import sys - -# Init -sys.path.append(os.path.dirname(os.path.realpath(__file__))) -from functions.activation import * -init_global_vars() -os.system('title {}: Windows Activation Tool'.format(KIT_NAME_FULL)) - -if __name__ == '__main__': - try: - stay_awake() - clear_screen() - print_info('{}: Windows Activation Tool\n'.format(KIT_NAME_FULL)) - # Bail early if already activated - if windows_is_activated(): - print_info('This system is already activated') - sleep(5) - exit_script() - other_results = { - 'Error': { - 'BIOSKeyNotFoundError': 'BIOS key not found.', - }} - - # Determine activation method - activation_methods = [ - {'Name': 'Activate with BIOS key', 'Function': activate_with_bios}, - ] - if global_vars['OS']['Version'] not in ('8', '8.1', '10'): - activation_methods[0]['Disabled'] = True - actions = [ - {'Name': 'Quit', 'Letter': 'Q'}, - ] - - while True: - selection = menu_select( - '{}: Windows Activation Menu'.format(KIT_NAME_FULL), - main_entries=activation_methods, action_entries=actions) - - if (selection.isnumeric()): - result = try_and_print( - message = activation_methods[int(selection)-1]['Name'], - function = activation_methods[int(selection)-1]['Function'], - other_results=other_results) - if result['CS']: - break - else: - sleep(2) - elif selection == 'Q': - exit_script() - - # Done - print_success('\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/wk/io.py b/scripts/wk/io.py index fa6110c8..cba8b3fa 100644 --- a/scripts/wk/io.py +++ b/scripts/wk/io.py @@ -63,7 +63,7 @@ def delete_item(path, force=False, ignore_errors=False): os.remove(path) -def non_clobbering_path(path): +def non_clobber_path(path): """Update path as needed to non-existing path, returns pathlib.Path.""" LOG.debug('path: %s', path) path = pathlib.Path(path) diff --git a/scripts/wk/os/__init__.py b/scripts/wk/os/__init__.py index 5e58dbc8..e3d66798 100644 --- a/scripts/wk/os/__init__.py +++ b/scripts/wk/os/__init__.py @@ -1,7 +1,9 @@ """WizardKit: os module init""" # vim: sts=2 sw=2 ts=2 -import os +import platform -if os.name == 'nt': +#if platform.system() == 'Darwin': +#if platform.system() == 'Linux': +if platform.system() == 'Windows': from wk.os import win diff --git a/scripts/wk/os/win.py b/scripts/wk/os/win.py index 8e00ec1a..b99a1e13 100644 --- a/scripts/wk/os/win.py +++ b/scripts/wk/os/win.py @@ -17,7 +17,7 @@ from wk.std import GenericError, GenericWarning, sleep # STATIC VARIABLES LOG = logging.getLogger(__name__) REG_MSISERVER = r'HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\MSIServer' -SLMGR = pathlib.Path(f'{os.environ("SYSTEMROOT")}/System32/slmgr.vbs') +SLMGR = pathlib.Path(f'{os.environ.get("SYSTEMROOT")}/System32/slmgr.vbs') # Functions @@ -41,6 +41,10 @@ def activate_with_bios(): if not bios_key: raise GenericError('BIOS key not found.') + # Check if activation is needed + if is_activated(): + raise GenericWarning('System already activated') + # Install Key cmd = ['cscript', '//nologo', SLMGR, '/ipk', bios_key] run_program(cmd, check=False) @@ -52,7 +56,7 @@ def activate_with_bios(): sleep(5) # Check status - if not windows_is_activated(): + if not is_activated(): raise GenericError('Activation Failed') @@ -96,6 +100,14 @@ def get_activation_string(): return act_str +def is_activated(): + """Check if Windows is activated via slmgr.vbs and return bool.""" + act_str = get_activation_string() + + # Check result. + return act_str and 'permanent' in act_str + + def run_sfc_scan(): """Run SFC and save results.""" cmd = ['sfc', '/scannow'] @@ -132,15 +144,5 @@ def run_sfc_scan(): raise GenericError -def windows_is_activated(): - """Check if Windows is activated via slmgr.vbs and return bool.""" - cmd = ['cscript', '//nologo', SLMGR, '/xpr'] - result = run_program(cmd, check=False) - act_str = result.stdout - - # Check result. - return bool(act_str and 'permanent' in act_str) - - if __name__ == '__main__': print("This file is not meant to be called directly.")