import os import subprocess import ctypes import ssl import time from urllib2 import Request, urlopen LINPHONE_INSTALLER_URL = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Linphone/x64/Linphone-6.1.1-win64.exe" class disable_file_system_redirection: _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection def __enter__(self): self.old_value = ctypes.c_long() self.success = self._disable(ctypes.byref(self.old_value)) def __exit__(self, type, value, traceback): if self.success: self._revert(self.old_value) def ecmd(command): with disable_file_system_redirection(): process = subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) out, err = process.communicate() return out.strip() + err.strip() def get_linphone_uninstall(): reg_paths = [ r'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', r'HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall' ] for reg_path in reg_paths: cmd = 'reg query "%s" /s /f "Linphone"' % reg_path result = ecmd(cmd) if not result: continue lines = result.splitlines() uninstall_string = None for line in lines: line = line.strip() if "UninstallString" in line and "REG_SZ" in line: parts = line.split("REG_SZ") if len(parts) > 1: uninstall_string = parts[1].strip() if "uninstall.exe" in uninstall_string.lower(): return uninstall_string return None def kill_linphone_process(): processes = [ "linphone.exe", "linphonetray.exe" ] for process in processes: cmd = 'taskkill /F /IM "%s"' % process ecmd(cmd) time.sleep(5) def remove_old_registry(): reg_keys = [ r'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Linphone', r'HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Linphone' ] for key in reg_keys: cmd = 'reg delete "%s" /f' % key ecmd(cmd) def wait_for_uninstall(): for i in range(60): uninstall_string = get_linphone_uninstall() if not uninstall_string: break time.sleep(2) def spm_uninstall(dir, args): kill_linphone_process() uninstall_string = get_linphone_uninstall() if not uninstall_string: return if uninstall_string.startswith('"'): uninstall_exe = uninstall_string.split('"')[1] else: uninstall_exe = uninstall_string.split(" ")[0] uninstall_cmd = ( '"%s" /S' ) % uninstall_exe ecmd(uninstall_cmd) wait_for_uninstall() time.sleep(10) remove_old_registry() time.sleep(5) def download_installer(file_path, url): headers = { 'User-Agent': 'Mozilla/5.0' } request = Request(url, headers=headers) try: context = ssl._create_unverified_context() response = urlopen(request, context=context) with open(file_path, 'wb') as f: while True: chunk = response.read(1024 * 100) if not chunk: break f.write(chunk) return True except: return False def spm_install(dir, args): temp_path = os.environ.get("TEMP", "C:\\Windows\\Temp") file_name = LINPHONE_INSTALLER_URL.split("/")[-1] local_path = os.path.join(temp_path, file_name) if not download_installer(local_path, LINPHONE_INSTALLER_URL): return install_cmd = ( '"%s" /S' ) % local_path ecmd(install_cmd) time.sleep(60) try: os.remove(local_path) except: pass def spm_update(dir, args): spm_uninstall(None, None) time.sleep(15) spm_install(None, None)