import os import subprocess 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' def spm_uninstall(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' def spm_update(dir, args): #It uninstalls the older version(s) which match the registry, then installs the new version #LEAP ships old installs as DisplayName "LEAP" and new installs as DisplayName "LEAP Desktop" - #both are matched here so an update always leaves exactly one entry behind unins=["LEAP", "LEAP Desktop"] import re import _winreg import time blacklist=[] for i in unins: blacklist.append(i) uninstallkey='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall' if 'PROGRAMFILES(X86)' in os.environ.keys(): reg_list=[(_winreg.HKEY_LOCAL_MACHINE,uninstallkey,_winreg.KEY_WOW64_32KEY | _winreg.KEY_ALL_ACCESS), (_winreg.HKEY_LOCAL_MACHINE,uninstallkey,_winreg.KEY_WOW64_64KEY | _winreg.KEY_ALL_ACCESS), (_winreg.HKEY_CURRENT_USER,uninstallkey,_winreg.KEY_WOW64_32KEY | _winreg.KEY_ALL_ACCESS), (_winreg.HKEY_CURRENT_USER,uninstallkey,_winreg.KEY_WOW64_64KEY | _winreg.KEY_ALL_ACCESS)] else: 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]) idx=0 while True: try: key_value=_winreg.EnumKey(reg,idx) 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 idx+=1 except: break for c in list: for i in range(0,len(blacklist)): #exact match, not substring - "LEAP" must NOT also catch "LEAP Desktop" if c[0].strip() == blacklist[i]: RawKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, c[1],0,Value) (value, type) = _winreg.QueryValueEx(RawKey,"UninstallString") strun.append(value) if 'msiexec' in value.lower(): #LEAP is MSI-based: UninstallString looks like MsiExec.exe /X{GUID}... #no quoted exe path to extract, and /S is not a valid msiexec switch - #pull the GUID and run a proper silent msiexec uninstall guid_match = re.search('\\{[0-9A-Fa-f\\-]+\\}', value) guid = guid_match.group(0) if guid_match else '' command = 'msiexec.exe /X' + guid + ' /qn /norestart' else: Uninstr=re.findall('".*"',value) unin_str = ''.join(Uninstr) if Uninstr else value command = unin_str + " /S" print command try: subprocess.check_call(command, shell=True) print blacklist[i] +' older Version are removed' except subprocess.CalledProcessError as e: print blacklist[i] +' older Version does not exist, uninstaller returncode:' + str(e.returncode); time.sleep(20) 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'