import os import subprocess from subprocess import PIPE, Popen import ssl import urllib2 import time import _winreg # Determine system architecture arch = os.popen("wmic os get OSArchitecture").read() # Dialpad installer URL based on system architecture if '64' in arch: url = "https://packages.one.comodo.com/portal/packages/spm/Dialpad/x64/DialpadSetup_x64_2410.3.0.exe" else: url = "https://packages.one.comodo.com/portal/packages/spm/Dialpad/x86/DialpadSetup_x86_2410.3.0.exe" Down_path = os.environ['TEMP'] fileName = url.split('/')[-1] DownTo = os.path.join(Down_path, fileName) def ecmd(command): obj = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) out, err = obj.communicate() ret = obj.returncode if ret == 0: return out.strip() if out else ret else: return err.strip() if err else ret def downloadFile(DownTo, fromURL): headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'} context = ssl._create_unverified_context() request = urllib2.Request(fromURL, headers=headers) req = urllib2.urlopen(request, context=context) try: with open(DownTo, 'wb') as f: while True: chunk = req.read(100 * 1024) # Read 100 KB at a time if chunk: f.write(chunk) else: break if os.path.isfile(DownTo): return '{} - {} KB downloaded'.format(DownTo, os.path.getsize(DownTo) / 1024) except Exception as e: return 'Error downloading file: {}'.format(e) def spm_install(dir, args): print(downloadFile(DownTo, url)) print(ecmd('"%s" -s' % (DownTo))) # Using -s for silent install time.sleep(5) # Wait a few seconds to ensure file is released try: os.remove(DownTo) print("Installer removed successfully.") except WindowsError as e: print("Error removing installer file: {}".format(e)) print("Retrying file removal after a short delay...") time.sleep(5) try: os.remove(DownTo) print("Installer removed on retry.") except WindowsError as e: print("Final attempt to remove installer failed: {}".format(e)) def spm_uninstall(dir, args): # Define the registry path where the uninstallation info is stored registry_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\dialpad" try: # Open the registry key for reading (HKEY_CURRENT_USER) reg_key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, registry_path) # Get the uninstall string from the registry (it should be a string with the uninstall command) uninstall_string, _ = _winreg.QueryValueEx(reg_key, "QuietUninstallString") # Print the uninstall command print("Uninstall command: {}".format(uninstall_string)) # Run the uninstall command result = subprocess.call(uninstall_string, shell=True) if result.returncode == 0: print("Dialpad uninstalled successfully.") else: print("Uninstallation failed with error: {}".format(result.stderr.decode())) # Close the registry key _winreg.CloseKey(reg_key) except IOError: print("Dialpad not found in registry. It may not be installed or registry path is incorrect.") except Exception as e: print("An error occurred: {}".format(e)) def spm_update(dir, args): spm_uninstall(None,None) time.sleep(10) spm_install(None,None)