import os from subprocess import PIPE, Popen import ctypes import ssl import urllib2 import time # ------------------------------------------------------------ # Disable WOW64 filesystem redirection # ------------------------------------------------------------ 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) # ------------------------------------------------------------ # Download details # ------------------------------------------------------------ url = "https://cdn-patchportal-one.comodo.com/portal/packages/spm/Dell%20Command%20Update/x64/Dell-5.7.0.EXE" Down_path = os.environ['TEMP'] fileName = url.split('/')[-1] DownTo = os.path.join(Down_path, fileName) # ------------------------------------------------------------ # Execute command # ------------------------------------------------------------ def ecmd(command): with disable_file_system_redirection(): obj = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) obj.communicate() return obj.returncode # ------------------------------------------------------------ # Download installer # ------------------------------------------------------------ def downloadFile(DownTo, fromURL): headers = {'User-Agent': 'Mozilla/5.0'} context = ssl._create_unverified_context() request = urllib2.Request(fromURL, headers=headers) req = urllib2.urlopen(request, context=context) with open(DownTo, 'wb') as f: while True: chunk = req.read(1024 * 1024) if not chunk: break f.write(chunk) return os.path.isfile(DownTo) # ------------------------------------------------------------ # Check if any Dell product exists (after DCU removal) # ------------------------------------------------------------ def dell_products_exist(): cmd = ( 'wmic product where ' '"name like \'Dell %%\'" ' 'get name' ) output = os.popen(cmd).read() lines = [l.strip() for l in output.splitlines() if l.strip() and 'Name' not in l and 'Dell Core Services' not in l] return len(lines) > 0 # ------------------------------------------------------------ # Uninstall Dell Core Services ONLY if no Dell products exist # ------------------------------------------------------------ def uninstall_dell_core_services(): if dell_products_exist(): print("Dell products still present. Dell Core Services will NOT be removed.") return print("No Dell products found. Uninstalling Dell Core Services.") ecmd( 'wmic product where "name=\'Dell Core Services\'" ' 'call uninstall /nointeractive' ) # ------------------------------------------------------------ # Install # ------------------------------------------------------------ def spm_install(dir, args): with disable_file_system_redirection(): if downloadFile(DownTo, url): ecmd('"%s" /s' % DownTo) time.sleep(10) if os.path.exists(DownTo): os.remove(DownTo) # ------------------------------------------------------------ # Uninstall (Correct Order) # ------------------------------------------------------------ def spm_uninstall(dir, args): with disable_file_system_redirection(): # 1. Remove Dell Command | Update first ecmd( 'wmic product where ' '"name like \'Dell Command | Update%%\'" ' 'call uninstall /nointeractive' ) time.sleep(15) # 2. Decide about Dell Core Services uninstall_dell_core_services() # ------------------------------------------------------------ # Update # ------------------------------------------------------------ def spm_update(dir, args): with disable_file_system_redirection(): spm_uninstall(None, None) time.sleep(20) spm_install(None, None)