import os import subprocess import time import re try: import _winreg except ImportError: import winreg as _winreg APP_NAME = "QNAP Qfinder Pro" PROCESS_NAME = "QfinderPro.exe" def kill_qfinder(): try: subprocess.call( 'taskkill /F /IM "%s"' % PROCESS_NAME, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) except: pass def execute_uninstall(uninstall_string): if not uninstall_string: return 0 uninstall_string = uninstall_string.strip() try: if "msiexec" in uninstall_string.lower(): # Find MSI Product Code match = re.search( r'\{[0-9A-Fa-f\-]+\}', uninstall_string ) if match: product_code = match.group(0) command = [ "msiexec.exe", "/x", product_code, "/qn", "/norestart" ] return subprocess.call(command) if "/S" not in uninstall_string.upper(): uninstall_string += " /S" return subprocess.call( uninstall_string, shell=True ) except: return 1 def uninstall_old_qfinder(): kill_qfinder() registry_locations = [] # 64-bit Windows if "PROGRAMFILES(X86)" in os.environ: registry_locations = [ ( _winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", _winreg.KEY_READ | _winreg.KEY_WOW64_64KEY ), ( _winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", _winreg.KEY_READ | _winreg.KEY_WOW64_32KEY ), ( _winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", _winreg.KEY_READ | _winreg.KEY_WOW64_64KEY ), ( _winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", _winreg.KEY_READ | _winreg.KEY_WOW64_32KEY ) ] # 32-bit Windows else: registry_locations = [ ( _winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", _winreg.KEY_READ ), ( _winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", _winreg.KEY_READ ) ] for root, path, access in registry_locations: try: reg = _winreg.OpenKey( root, path, 0, access ) except: continue index = 0 while True: try: subkey_name = _winreg.EnumKey(reg, index) index += 1 except: break try: subkey = _winreg.OpenKey( reg, subkey_name ) try: display_name = _winreg.QueryValueEx( subkey, "DisplayName" )[0] except: display_name = "" if APP_NAME.lower() in display_name.lower(): try: uninstall_string = _winreg.QueryValueEx( subkey, "QuietUninstallString" )[0] except: try: uninstall_string = _winreg.QueryValueEx( subkey, "UninstallString" )[0] except: uninstall_string = "" if uninstall_string: kill_qfinder() ret = execute_uninstall( uninstall_string ) # MSI success / reboot required if ret == 0 or ret == 3010: time.sleep(10) kill_qfinder() except: pass return 0 def spm_install(dir, args): try: os.chdir(dir) kill_qfinder() argslist = args.split() subprocess.check_call(argslist) time.sleep(10) # Qfinder may automatically start after installation kill_qfinder() print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: # 3010 = success, reboot required if e.returncode == 3010: print 'retcode' + str(0) + 'retcode' else: print 'retcode' + str(e.returncode) + 'retcode' except: print 'retcode' + str(1) + 'retcode' def spm_uninstall(dir, args): try: os.chdir(dir) kill_qfinder() argslist = args.split() subprocess.check_call(argslist) time.sleep(10) kill_qfinder() print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: if e.returncode == 3010: print 'retcode' + str(0) + 'retcode' else: print 'retcode' + str(e.returncode) + 'retcode' except: print 'retcode' + str(1) + 'retcode' def spm_update(dir, args): try: kill_qfinder() uninstall_old_qfinder() time.sleep(10) kill_qfinder() os.chdir(dir) argslist = args.split() subprocess.check_call(argslist) time.sleep(10) kill_qfinder() print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: # 3010 = successful but reboot required if e.returncode == 3010: print 'retcode' + str(0) + 'retcode' else: print 'retcode' + str(e.returncode) + 'retcode' except: print 'retcode' + str(1) + 'retcode'