import os import subprocess # dir: string -> directory where installer exist # args: string -> which includes all parameters with space delimiter def spm_install(dir, args): os.chdir(dir) argslist = args.split() try: subprocess.check_call(argslist) print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode' # dir: string -> directory where installer exist # args: string -> which includes all parameters with space delimiter def spm_uninstall(dir, args): os.chdir(dir) argslist = args.split() try: subprocess.check_call(argslist) print 'retcode' + str(0) + 'retcode' except: #It uninstalls the older version which will match with the registry try: task=os.popen('Tasklist |findstr "Google"').read() task=task.split() for i in task: if i.endswith (".exe"): kill=os.popen("Taskkill /IM "+i+" /F").read() else: pass except: pass unins="Google Chrome" import re import _winreg import time blacklist=[] blacklist.append(unins) if 'PROGRAMFILES(X86)' in os.environ.keys(): uninstallkey=['SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall','SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall'] for i in uninstallkey: reg_list=[(_winreg.HKEY_LOCAL_MACHINE,i,_winreg.KEY_WOW64_32KEY | _winreg.KEY_ALL_ACCESS), (_winreg.HKEY_LOCAL_MACHINE,i,_winreg.KEY_WOW64_64KEY | _winreg.KEY_ALL_ACCESS), (_winreg.HKEY_CURRENT_USER,i,_winreg.KEY_WOW64_32KEY | _winreg.KEY_ALL_ACCESS), (_winreg.HKEY_CURRENT_USER,i,_winreg.KEY_WOW64_64KEY | _winreg.KEY_ALL_ACCESS)] strun=[] list=[] for i in reg_list: reg_key=i[0] sub_key=i[1] Value=i[2] reg =_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, i[1], 0, i[2]) i=0 while True: try: key_value=_winreg.EnumKey(reg,i) path=os.path.join(sub_key,key_value) Hkey=_winreg.OpenKey(reg_key,path,0,Value) try: key,dis_name=_winreg.QueryValueEx(Hkey,'DisplayName') inlist=[key.strip(), path, sub_key] list.append(inlist) except: pass i+=1 except: break for c in list: for i in range(0,len(blacklist)): try: if blacklist[0] in c[0]: RawKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, c[1],0,Value) (value, type) = _winreg.QueryValueEx(RawKey,"UninstallString") unin_str=''.join(value) if "C:\Program Files (x86)" in value: strun.append(value) Uninstr=re.findall('".*"',value) unin_str=''.join(Uninstr) command=unin_str+" --uninstall --force-uninstall --system-level --verbose-logging" unarglist= command.splitlines(True) unarglist2= ''.join(unarglist) else: command=value+" /qn" unarglist2 = command.split() try: subprocess.check_call(unarglist2) time.sleep(30) print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: k=(str(e.returncode)) print k if k=="19": returncode=0 print 'retcode' + str(returncode) + 'retcode' elif k==19: returncode=0 print 'retcode' + str(returncode) + 'retcode' else: print 'retcode' + str(e.returncode) + 'retcode' except: pass else: uninstallkey='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall' reg_list=[(_winreg.HKEY_LOCAL_MACHINE,uninstallkey,_winreg.KEY_READ,_winreg.KEY_ALL_ACCESS), (_winreg.HKEY_CURRENT_USER,uninstallkey,_winreg.KEY_READ,_winreg.KEY_ALL_ACCESS)] strun=[] list=[] for i in reg_list: reg_key=i[0] sub_key=i[1] Value=i[2] reg =_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, i[1], 0, i[2]) i=0 while True: try: key_value=_winreg.EnumKey(reg,i) path=os.path.join(sub_key,key_value) Hkey=_winreg.OpenKey(reg_key,path,0,Value) try: key,dis_name=_winreg.QueryValueEx(Hkey,'DisplayName') inlist=[key.strip(), path, sub_key] list.append(inlist) except: pass i+=1 except: break for c in list: for i in range(0,len(blacklist)): try: if blacklist[0] in c[0]: RawKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, c[1],0,Value) (value, type) = _winreg.QueryValueEx(RawKey,"UninstallString") unin_str=''.join(value) if "C:\Program Files" in value: strun.append(value) Uninstr=re.findall('".*"',value) unin_str=''.join(Uninstr) command=unin_str+" --uninstall --force-uninstall --system-level --verbose-logging" unarglist= command.splitlines(True) unarglist2= ''.join(unarglist) else: command=value+" /qn" unarglist2 = command.split() try: subprocess.check_call( unarglist2) time.sleep(30) print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: k=(str(e.returncode)) if k=="19": returncode=0 print 'retcode' + str(returncode) + 'retcode' elif k==19: returncode=0 print 'retcode' + str(returncode) + 'retcode' else: print 'retcode' + str(e.returncode) + 'retcode' except: pass # dir: string -> directory where installer exist # args: string -> which includes all parameters with space delimiter def spm_update(dir, args): os.chdir(dir) argslist = args.split() try: subprocess.check_call(argslist) print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode'