import os import subprocess import ctypes import ssl import time from urllib2 import Request, urlopen FOTOSIZER_INSTALLER_URL = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Fotosizer/x64/fsSetup321.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_fotosizer_uninstall_string(): reg_paths = [ r'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', r'HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall' ] uninstall_strings = [] for reg_path in reg_paths: cmd = 'reg query "%s" /s /f "Fotosizer" | findstr "UninstallString"' % reg_path result = ecmd(cmd) lines = result.splitlines() for line in lines: line = line.strip() if "uninst" in line.lower(): parts = line.split("REG_SZ") if len(parts) > 1: uninstall_string = parts[1].strip() if uninstall_string not in uninstall_strings: uninstall_strings.append(uninstall_string) return uninstall_strings def spm_uninstall(dir, args): uninstall_strings = get_fotosizer_uninstall_string() for uninstall_string in uninstall_strings: try: if uninstall_string.startswith('"'): uninstall_exe = uninstall_string.split('"')[1] else: uninstall_exe = uninstall_string.split(" ")[0] uninstall_cmd = '"%s" /VERYSILENT /SUPPRESSMSGBOXES /NORESTART' % uninstall_exe ecmd(uninstall_cmd) time.sleep(20) except: pass ecmd(r'reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Fotosizer" /f') time.sleep(10) 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 = "fsSetup321.exe" local_path = os.path.join(temp_path, file_name) if not os.path.exists(local_path): download_success = False for i in range(3): if download_installer(local_path, FOTOSIZER_INSTALLER_URL): download_success = True break time.sleep(10) if not download_success: raise Exception("Failed to download installer") install_cmd = 'start /wait "" "%s" /S' % local_path exit_code = subprocess.call( install_cmd, shell=True ) if exit_code != 0: raise Exception("Installation failed. Exit code=%s" % exit_code) time.sleep(60) ecmd(r'reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Fotosizer" /f') fotosizer_exe = r"C:\Program Files\Fotosizer\Fotosizer.exe" if not os.path.exists(fotosizer_exe): raise Exception("Fotosizer.exe not found after installation") try: os.remove(local_path) except: pass time.sleep(20) def spm_update(dir, args): spm_uninstall(None, None) time.sleep(20) spm_install(None, None)