예제 #1
0
def find_existing_installation(package_name: str, display_name: str):
    key = registry.get_uninstall_key(package_name)

    if key:
        return True
    else:
        key = registry.get_uninstall_key(display_name.lower())

        if key:
            return True

        else:
            key = registry.get_uninstall_key(display_name)

    if key:
        return True

    return False
예제 #2
0
 def test_get_uninstall_key(self):
     ans = registry.get_uninstall_key('sublime-text-3', 'Sublime Text 3')
     self.assertIsInstance(ans, list)
예제 #3
0
def uninstall(package_name: str, verbose: bool, debug: bool, no_color: bool,
              logfile: str, yes: bool, silent: bool, python: bool,
              no_cache: bool):

    metadata = generate_metadata(None, silent, verbose, debug, no_color, yes,
                                 logfile, None, None)

    super_cache = check_supercache_valid()

    if no_cache:
        super_cache = False

    if logfile:
        logfile = logfile.replace('.txt', '.log')
        createConfig(logfile, logging.INFO, 'Install')

    if python:

        flags = []

        package_names = package_name.split(',')

        for name in package_names:
            handle_python_package(name, 'uninstall', metadata)

        sys.exit()

    status = 'Initializing'
    setup_name = ''
    keyboard.add_hotkey('ctrl+c',
                        lambda: handle_exit(status, setup_name, metadata))

    packages = package_name.split(',')

    if super_cache:
        res, time = handle_cached_request()

    else:
        status = 'Networking'
        write_verbose('Sending GET Request To /rapidquery/packages', metadata)
        write_debug('Sending GET Request To /rapidquery/packages', metadata)
        log_info('Sending GET Request To /rapidquery/packages', logfile)
        res, time = send_req_all()
        res = json.loads(res)
    correct_names = get_correct_package_names(res)
    corrected_package_names = []

    for name in packages:
        if name in correct_names:
            corrected_package_names.append(name)
        else:
            corrections = difflib.get_close_matches(name, correct_names)
            if corrections:
                if silent:
                    click.echo(
                        click.style(
                            'Incorrect / Invalid Package Name Entered. Aborting Uninstallation.',
                            fg='red'))
                    log_info(
                        'Incorrect / Invalid Package Name Entered. Aborting Uninstallation',
                        logfile)
                    handle_exit(status, setup_name, metadata)

                if yes:
                    write(f'Autocorrecting To {corrections[0]}', 'green',
                          metadata)
                    log_info(f'Autocorrecting To {corrections[0]}', logfile)
                    write(f'Successfully Autocorrected To {corrections[0]}',
                          'green', metadata)
                    log_info(f'Successfully Autocorrected To {corrections[0]}',
                             logfile)
                    corrected_package_names.append(corrections[0])

                else:
                    write(f'Autocorrecting To {corrections[0]}',
                          'bright_magenta', metadata)
                    write_verbose(f'Autocorrecting To {corrections[0]}',
                                  metadata)
                    write_debug(f'Autocorrecting To {corrections[0]}',
                                metadata)
                    log_info(f'Autocorrecting To {corrections[0]}', logfile)
                    if click.prompt(
                            'Would You Like To Continue? [y/n]') == 'y':
                        package_name = corrections[0]
                        corrected_package_names.append(package_name)
                    else:
                        sys.exit()
            else:
                write(f'Could Not Find Any Packages Which Match {name}',
                      'bright_magenta', metadata)
                write_debug(f'Could Not Find Any Packages Which Match {name}',
                            metadata)
                write_verbose(
                    f'Could Not Find Any Packages Which Match {name}',
                    metadata)
                log_info(f'Could Not Find Any Packages Which Match {name}',
                         logfile)

    write_debug(install_debug_headers, metadata)
    for header in install_debug_headers:
        log_info(header, logfile)

    index = 0

    for package in corrected_package_names:
        pkg = res[package]
        packet = Packet(package, pkg['package-name'], pkg['win64'],
                        pkg['darwin'], pkg['debian'], pkg['win64-type'],
                        pkg['darwin-type'], pkg['debian-type'],
                        pkg['custom-location'], pkg['install-switches'],
                        pkg['uninstall-switches'], None)
        proc = None
        keyboard.add_hotkey('ctrl+c', lambda: kill_proc(proc, metadata))

        kill_running_proc(packet.json_name, metadata)

        if super_cache:
            write(
                f'Rapidquery Successfully SuperCached {packet.json_name} in {round(time, 6)}s',
                'bright_yellow', metadata)
            write_debug(
                f'Rapidquery Successfully SuperCached {packet.json_name} in {round(time, 9)}s',
                metadata)
            log_info(
                f'Rapidquery Successfully SuperCached {packet.json_name} in {round(time, 6)}s',
                metadata)
        else:
            write(
                f'Rapidquery Successfully Received {packet.json_name}.json in {round(time, 6)}s',
                'bright_green', metadata)
            log_info(
                f'Rapidquery Successfully Received {packet.json_name}.json in {round(time, 6)}s',
                logfile)

        # Getting UninstallString or QuietUninstallString From The Registry Search Algorithm
        write_verbose("Fetching uninstall key from the registry...", metadata)
        log_info("Fetching uninstall key from the registry...", logfile)

        start = timer()
        key = get_uninstall_key(packet.json_name)
        end = timer()

        # If The UninstallString Or QuietUninstallString Doesn't Exist
        # if not key:
        #     write_verbose('No registry keys found', verbose, no_color, silent)
        #     log_info('No registry keys found', logfile)
        #     if "uninstall-command" in pkg:
        #         if pkg['uninstall-command'] == '':
        #             write(
        #                 f'Could Not Find Any Existing Installations Of {packet.json_name}', 'yellow', no_color, silent)
        #             log_error(
        #                 f'Could Not Find Any Existing Installations Of {packet.json_name}', logfile)
        #             closeLog(logfile, 'Uninstall')
        #             index += 1
        #             continue
        #         else:
        #             write_verbose("Executing the uninstall command",
        #                           verbose, no_color, silent)
        #             log_info("Executing the uninstall command", logfile)

        #             try:
        #                 proc = Popen(shlex.split(
        #                     pkg['uninstall-command']), stdout=PIPE, stdin=PIPE, stderr=PIPE)
        #                 proc.wait()
        #                 if proc.returncode != 0:
        #                     write(f'Installation Failed, Make Sure You Accept All Prompts Asking For Admin permission', 'red', no_color, silent)
        #                     handle_exit(status, 'None', no_color, silent)
        #             except FileNotFoundError:

        #                 proc = Popen(shlex.split(
        #                     pkg['uninstall-command']), stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True)
        #                 proc.wait()
        #                 if proc.returncode != 0:
        #                     write(f'Installation Failed, Make Sure You Accept All Prompts Asking For Admin permission', 'red', no_color, silent)
        #                     handle_exit(status, 'None', no_color, silent)

        #             write(
        #                 f"Successfully Uninstalled {package_name}", "bright_magenta", no_color, silent)

        #             index += 1
        #             write_debug(
        #                 f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion', debug, no_color, silent)
        #             log_info(
        #                 f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion', logfile)
        #             closeLog(logfile, 'Uninstall')
        #             continue
        #     else:
        # write(
        #     f'Could Not Find Any Existing Installations Of {package_name}', 'yellow', no_color, silent)
        # closeLog(logfile, 'Uninstall')
        # index += 1
        # continue

        if not key:
            write(
                f'Could Not Find Any Existing Installations Of {packet.display_name}',
                'yellow', metadata)
            closeLog(logfile, 'Uninstall')
            index += 1
            continue

        write_verbose("Uninstall key found.", metadata)
        log_info("Uninstall key found.", logfile)

        write(
            f'Successfully Retrieved Uninstall Key In {round(end - start, 4)}s',
            'cyan', metadata)
        log_info(
            f'Successfully Retrieved Uninstall Key In {round(end - start, 4)}s',
            logfile)

        command = ''

        # Key Can Be A List Or A Dictionary Based On Results

        if isinstance(key, list):
            if key:
                key = key[0]

        write(f'Uninstalling {packet.display_name}', 'green', metadata)

        # If QuietUninstallString Exists (Preferable)
        if 'QuietUninstallString' in key:
            command = key['QuietUninstallString']
            command = command.replace('/I', '/X')
            command = command.replace('/quiet', '/passive')

            additional_switches = None
            if packet.uninstall_switches:
                if packet.uninstall_switches != []:
                    write_verbose("Adding additional uninstall switches",
                                  metadata)
                    log_info("Adding additional uninstall switches", logfile)
                    additional_switches = packet.uninstall_switches

            if additional_switches:
                for switch in additional_switches:
                    command += ' ' + switch

            write_verbose("Executing the quiet uninstall command", metadata)
            log_info("Executing the quiet uninstall command", logfile)

            run_cmd(command, metadata, 'uninstallation')

            write(f"Successfully Uninstalled {packet.display_name}",
                  "bright_magenta", metadata)

            write_verbose("Uninstallation completed.", metadata)
            log_info("Uninstallation completed.", logfile)

            index += 1
            write_debug(
                f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion',
                metadata)
            log_info(
                f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion',
                logfile)
            closeLog(logfile, 'Uninstall')

        # If Only UninstallString Exists (Not Preferable)
        if 'UninstallString' in key and 'QuietUninstallString' not in key:
            command = key['UninstallString']
            command = command.replace('/I', '/X')
            command = command.replace('/quiet', '/passive')
            command = f'"{command}"'
            for switch in packet.uninstall_switches:
                command += f' {switch}'

            # Run The UninstallString
            write_verbose("Executing the Uninstall Command", metadata)
            log_info("Executing the Uninstall Command", logfile)

            run_cmd(command, metadata, 'uninstallation')

            write(f'Successfully Uninstalled {packet.display_name}',
                  'bright_magenta', metadata)
            write_verbose("Uninstallation completed.", metadata)
            log_info("Uninstallation completed.", logfile)
            index += 1
            write_debug(
                f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion',
                metadata)
            log_info(
                f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion',
                logfile)
            closeLog(logfile, 'Uninstall')
예제 #4
0
def uninstall(package_name: str, verbose: bool, debug: bool, no_color: bool,
              logfile: str, yes: bool, silent: bool, python: bool,
              vscode: bool, node: bool, no_cache: bool):

    log_info('Generating metadata...', logfile)

    metadata = generate_metadata(None, silent, verbose, debug, no_color, yes,
                                 logfile, None, None, None)

    log_info('Successfully generated metadata.', logfile)

    log_info('Checking if supercache exists...', metadata.logfile)
    super_cache = check_supercache_valid()

    if super_cache:
        log_info('SuperCache detected.', metadata.logfile)

    if no_cache:
        log_info('Overriding SuperCache To FALSE', metadata.logfile)
        super_cache = False

    if logfile:
        logfile = logfile.replace('.txt', '.log')
        createConfig(logfile, logging.INFO, 'Install')

    if python:

        package_names = package_name.split(',')

        for name in package_names:
            handle_python_package(name, 'uninstall', metadata)

        sys.exit()

    if node:
        package_names = package_name.split(',')
        for name in package_names:
            handle_node_package(name, 'uninstall', metadata)

        sys.exit()

    if vscode:
        package_names = package_name.split(',')
        for name in package_names:
            handle_vscode_extension(name, 'uninstall', metadata)

        sys.exit()

    log_info('Setting up custom `ctrl+c` shortcut.', metadata.logfile)
    status = 'Initializing'
    setup_name = ''
    keyboard.add_hotkey('ctrl+c',
                        lambda: handle_exit(status, setup_name, metadata))

    packages = package_name.split(',')

    if super_cache:
        log_info('Handling SuperCache Request.', metadata.logfile)
        res, time = handle_cached_request()

    else:
        log_info('Handling Network Request...', metadata.logfile)
        status = 'Networking'
        write_verbose('Sending GET Request To /rapidquery/packages', metadata)
        write_debug('Sending GET Request To /rapidquery/packages', metadata)
        log_info('Sending GET Request To /rapidquery/packages',
                 metadata.logfile)
        res, time = send_req_all()
        res = json.loads(res)
        update_supercache(res)

    correct_names = get_correct_package_names(res)
    corrected_package_names = get_autocorrections(packages, correct_names,
                                                  metadata)

    write_debug(install_debug_headers, metadata)
    for header in install_debug_headers:
        log_info(header, metadata.logfile)

    index = 0

    for package in corrected_package_names:
        pkg = res[package]
        log_info('Generating Packet For Further Installation.',
                 metadata.logfile)
        packet = Packet(package, pkg['package-name'], pkg['win64'],
                        pkg['win64-type'], pkg['custom-location'],
                        pkg['install-switches'], pkg['uninstall-switches'],
                        None, pkg['dependencies'])
        proc = None
        keyboard.add_hotkey('ctrl+c', lambda: kill_proc(proc, metadata))

        kill_running_proc(packet.json_name, packet.display_name, metadata)

        if super_cache:
            write(
                f'Rapidquery Successfully SuperCached {packet.json_name} in {round(time, 6)}s',
                'bright_yellow', metadata)
            write_debug(
                f'Rapidquery Successfully SuperCached {packet.json_name} in {round(time, 9)}s',
                metadata)
            log_info(
                f'Rapidquery Successfully SuperCached {packet.json_name} in {round(time, 6)}s',
                metadata)
        else:
            write(
                f'Rapidquery Successfully Received {packet.json_name}.json in {round(time, 6)}s',
                'bright_green', metadata)
            log_info(
                f'Rapidquery Successfully Received {packet.json_name}.json in {round(time, 6)}s',
                metadata.logfile)

        # Getting UninstallString or QuietUninstallString From The Registry Search Algorithm
        write_verbose('Fetching uninstall key from the registry...', metadata)
        write_debug('Sending query (uninstall-string) to Registry', metadata)
        log_info('Fetching uninstall key from the registry...',
                 metadata.logfile)

        start = timer()
        key = get_uninstall_key(packet.json_name, packet.display_name)

        end = timer()

        # TODO: Add suggestions to uninstall bundled in dependencies

        # If The UninstallString Or QuietUninstallString Doesn't Exist
        # if not key:
        #     write_verbose('No registry keys found', verbose, no_color, silent)
        #     log_info('No registry keys found', logfile)
        #     if "uninstall-command" in pkg:
        #         if pkg['uninstall-command'] == '':
        #             write(
        #                 f'Could Not Find Any Existing Installations Of {packet.json_name}', 'yellow', no_color, silent)
        #             log_error(
        #                 f'Could Not Find Any Existing Installations Of {packet.json_name}', logfile)
        #             closeLog(logfile, 'Uninstall')
        #             index += 1
        #             continue
        #         else:
        #             write_verbose("Executing the uninstall command",
        #                           verbose, no_color, silent)
        #             log_info("Executing the uninstall command", logfile)

        #             try:
        #                 proc = Popen(shlex.split(
        #                     pkg['uninstall-command']), stdout=PIPE, stdin=PIPE, stderr=PIPE)
        #                 proc.wait()
        #                 if proc.returncode != 0:
        #                     write(f'Installation Failed, Make Sure You Accept All Prompts Asking For Admin permission', 'red', no_color, silent)
        #                     handle_exit(status, 'None', no_color, silent)
        #             except FileNotFoundError:

        #                 proc = Popen(shlex.split(
        #                     pkg['uninstall-command']), stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True)
        #                 proc.wait()
        #                 if proc.returncode != 0:
        #                     write(f'Installation Failed, Make Sure You Accept All Prompts Asking For Admin permission', 'red', no_color, silent)
        #                     handle_exit(status, 'None', no_color, silent)

        #             write(
        #                 f"Successfully Uninstalled {package_name}", "bright_magenta", no_color, silent)

        #             index += 1
        #             write_debug(
        #                 f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion', debug, no_color, silent)
        #             log_info(
        #                 f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion', logfile)
        #             closeLog(logfile, 'Uninstall')
        #             continue
        #     else:
        # write(
        #     f'Could Not Find Any Existing Installations Of {package_name}', 'yellow', no_color, silent)
        # closeLog(logfile, 'Uninstall')
        # index += 1
        # continue

        if not key:
            log_info(
                f'electric didn\'t detect any existing installations of => {packet.display_name}',
                metadata.logfile)
            write(
                f'Could Not Find Any Existing Installations Of {packet.display_name}',
                'yellow', metadata)
            closeLog(metadata.logfile, 'Uninstall')
            index += 1
            continue

        write_verbose('Uninstall key found.', metadata)
        log_info('Uninstall key found.', metadata.logfile)
        log_info(key, metadata.logfile)
        write_debug(
            'Successfully Recieved UninstallString from Windows Registry',
            metadata)

        write(
            f'Successfully Retrieved Uninstall Key In {round(end - start, 4)}s',
            'cyan', metadata)
        log_info(
            f'Successfully Retrieved Uninstall Key In {round(end - start, 4)}s',
            metadata.logfile)

        command = ''

        # Key Can Be A List Or A Dictionary Based On Results

        if isinstance(key, list):
            if key:
                key = key[0]

        write(f'Uninstalling {packet.display_name}...', 'green', metadata)

        # If QuietUninstallString Exists (Preferable)
        if 'QuietUninstallString' in key:
            command = key['QuietUninstallString']
            command = command.replace('/I', '/X')
            command = command.replace('/quiet', '/qn')

            additional_switches = None
            if packet.uninstall_switches:
                if packet.uninstall_switches != []:
                    write_verbose('Adding additional uninstall switches',
                                  metadata)
                    write_debug(
                        'Appending / Adding additional uninstallation switches',
                        metadata)
                    log_info('Adding additional uninstall switches',
                             metadata.logfile)
                    additional_switches = packet.uninstall_switches

            if additional_switches:
                for switch in additional_switches:
                    command += ' ' + switch

            write_verbose('Executing the quiet uninstall command', metadata)
            log_info(f'Executing the quiet uninstall command => {command}',
                     metadata.logfile)
            write_debug(f'Running silent uninstallation command', metadata)
            run_cmd(command, metadata, 'uninstallation', packet.display_name)

            write(f'Successfully Uninstalled {packet.display_name}',
                  'bright_magenta', metadata)

            write_verbose('Uninstallation completed.', metadata)
            log_info('Uninstallation completed.', metadata.logfile)

            index += 1
            write_debug(
                f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion',
                metadata)
            log_info(
                f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion',
                metadata.logfile)
            closeLog(metadata.logfile, 'Uninstall')

        # If Only UninstallString Exists (Not Preferable)
        if 'UninstallString' in key and 'QuietUninstallString' not in key:
            command = key['UninstallString']
            command = command.replace('/I', '/X')
            command = command.replace('/quiet', '/passive')
            # command = f'"{command}"'
            for switch in packet.uninstall_switches:
                command += f' {switch}'

            # Run The UninstallString
            write_verbose('Executing the Uninstall Command', metadata)
            log_info('Executing the silent Uninstall Command',
                     metadata.logfile)

            run_cmd(command, metadata, 'uninstallation', packet.display_name)

            write(f'Successfully Uninstalled {packet.display_name}',
                  'bright_magenta', metadata)
            write_verbose('Uninstallation completed.', metadata)
            log_info('Uninstallation completed.', metadata.logfile)
            index += 1
            write_debug(
                f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion',
                metadata)
            log_info(
                f'Terminated debugger at {strftime("%H:%M:%S")} on uninstall::completion',
                metadata.logfile)
            closeLog(metadata.logfile, 'Uninstall')