import os import subprocess import ctypes import ssl import time import platform from urllib2 import Request, urlopen # Foxit PDF Reader installer URLs FOXIT_INSTALLER_URL_X64 = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Foxit%20Reader/x64/FoxitPDFReader20252_L10N_Setup_x64.msi" FOXIT_INSTALLER_URL_X86 = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Foxit%20Reader/x86/FoxitPDFReader20252_L10N_Setup.msi" # Replace if 32-bit differs def get_installer_url(): arch = platform.machine().lower() if "64" in arch: return FOXIT_INSTALLER_URL_X64 return FOXIT_INSTALLER_URL_X86 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_foxit_guid(): cmd = 'wmic product where "Name=\'Foxit PDF Reader\'" get IdentifyingNumber' result = ecmd(cmd) lines = result.splitlines() guids = [line.strip() for line in lines if line.strip().startswith("{") and line.strip().endswith("}")] if guids: return guids[0] return None def spm_uninstall(dir, args): guid = get_foxit_guid() if not guid: return ecmd('msiexec /x %s /qn /norestart' % guid) 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") installer_url = get_installer_url() file_name = installer_url.split("/")[-1] local_path = os.path.join(temp_path, file_name) if not download_installer(local_path, installer_url): return ecmd('msiexec /i "%s" /qn /norestart' % local_path) try: os.remove(local_path) except: pass time.sleep(5) def spm_update(dir, args): spm_uninstall(None, None) time.sleep(200) spm_install(None, None)