import os from subprocess import PIPE, Popen import ctypes import ssl import time import _winreg as reg # ---------------------------------------------------------------------- # Disable WOW64 redirection # ---------------------------------------------------------------------- class disable_file_system_redirection: _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection def __enter__(self): try: self.old_value = ctypes.c_long() self.success = self._disable(ctypes.byref(self.old_value)) except: self.success = False def __exit__(self, type, value, traceback): if self.success: self._revert(self.old_value) # ---------------------------------------------------------------------- # Reliable OS architecture detection # ---------------------------------------------------------------------- def is_system_64bit(): return os.path.exists(r"C:\Program Files") and os.path.exists(r"C:\Program Files (x86)") # ---------------------------------------------------------------------- # Your exact download URLs (kept unchanged) # ---------------------------------------------------------------------- URL_X64 = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Dropbox/x64/Dropbox%20237.4.5655%20Offline%20Installer.x64.exe" URL_X86 = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Dropbox/x86/Dropbox%20237.4.5655%20Offline%20Installer.x86.exe" # ---------------------------------------------------------------------- # CMD executor # ---------------------------------------------------------------------- def ecmd(command): with disable_file_system_redirection(): obj = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) out, err = obj.communicate() if out: return out.strip() if err: return err.strip() return "" # ---------------------------------------------------------------------- # Kill Dropbox Processes / UI # ---------------------------------------------------------------------- def kill_dropbox(): processes = [ "Dropbox.exe", "DropboxOEM.exe", "DropboxUpdate.exe", "DropboxInstall.exe", ] for p in processes: ecmd('taskkill /F /IM "%s" 2>NUL' % p) # ---------------------------------------------------------------------- # Get uninstall string from registry # ---------------------------------------------------------------------- def get_dropbox_uninstall(): paths = [] if is_system_64bit(): paths.append(r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Dropbox") paths.append(r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Dropbox") for p in paths: try: with disable_file_system_redirection(): key = reg.OpenKey(reg.HKEY_LOCAL_MACHINE, p, 0, reg.KEY_READ) val, _ = reg.QueryValueEx(key, "UninstallString") if "/S" not in val.upper(): return val + " /S" return val except: pass return None # ---------------------------------------------------------------------- # Curl-based downloader (bypasses Python DNS issues) # ---------------------------------------------------------------------- def downloadFile(DownTo, fromURL): print("Downloading via cURL...") cmd = 'curl -L --silent --show-error -o "%s" "%s"' % (DownTo, fromURL) out = ecmd(cmd) if os.path.isfile(DownTo): size = os.path.getsize(DownTo) / 1024 return "Downloaded: %s (%.0f KB)" % (DownTo, size) else: return "Download failed: %s" % out # ---------------------------------------------------------------------- # INSTALL # ---------------------------------------------------------------------- def spm_install(dir, args): # Pick correct installer if is_system_64bit(): url = URL_X64 else: url = URL_X86 Down_path = os.environ.get('TEMP', r'C:\Windows\Temp') fileName = url.split('/')[-1] DownTo = os.path.join(Down_path, fileName) print(downloadFile(DownTo, url)) print("Running installer...") print(ecmd('"%s" /S' % DownTo)) kill_dropbox() try: os.remove(DownTo) except: pass # ---------------------------------------------------------------------- # UNINSTALL # ---------------------------------------------------------------------- def spm_uninstall(dir, args): uninstall_cmd = get_dropbox_uninstall() if uninstall_cmd: print("Uninstalling Dropbox...") print(ecmd(uninstall_cmd)) kill_dropbox() else: print("Dropbox uninstall entry not found. Skipping.") # ---------------------------------------------------------------------- # UPDATE (uninstall → install) # ---------------------------------------------------------------------- def spm_update(dir, args): spm_uninstall(None, None) time.sleep(30) spm_install(None, None)