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) import time time.sleep(60) os.popen('Taskkill /IM "QfinderPro.exe" /F').read() print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode' def spm_uninstall(dir, args): import re import time import _winreg product_name = "QNAP Qfinder Pro" uninstallkey32 = 'SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall' uninstallkey64 = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall' reg_list = [ (_winreg.HKEY_LOCAL_MACHINE, uninstallkey32, _winreg.KEY_WOW64_32KEY | _winreg.KEY_ALL_ACCESS), (_winreg.HKEY_LOCAL_MACHINE, uninstallkey64, _winreg.KEY_WOW64_64KEY | _winreg.KEY_ALL_ACCESS), ] def kill_qfinder(): try: os.popen('Taskkill /IM "QfinderPro.exe" /F').read() except: pass def find_entries(): found = [] for reg_key, sub_key, access in reg_list: try: reg = _winreg.OpenKey(reg_key, sub_key, 0, access) except: continue i = 0 while True: try: key_value = _winreg.EnumKey(reg, i) except: break path = os.path.join(sub_key, key_value) try: Hkey = _winreg.OpenKey(reg_key, path, 0, access) dis_name, _ = _winreg.QueryValueEx(Hkey, 'DisplayName') if product_name.lower() in dis_name.strip().lower(): try: uninstr, _ = _winreg.QueryValueEx(Hkey, 'UninstallString') except: uninstr = None try: instloc, _ = _winreg.QueryValueEx(Hkey, 'InstallLocation') except: instloc = None found.append([dis_name.strip(), path, reg_key, access, uninstr, instloc]) except: pass i += 1 return found def force_remove(entry): dis_name, path, reg_key, access, uninstr, instloc = entry kill_qfinder() if instloc and os.path.isdir(instloc): try: import shutil shutil.rmtree(instloc, ignore_errors=True) except: pass try: _winreg.DeleteKey(reg_key, path) except: pass kill_qfinder() entries = find_entries() if not entries: print 'retcode' + str(0) + 'retcode' return for dis_name, path, reg_key, access, uninstr, instloc in entries: if uninstr: m = re.match(r'^\s*"([^"]+)"(.*)$', uninstr) if m: exe_path = m.group(1) rest_args = m.group(2).strip() else: parts = uninstr.strip().split(' ', 1) exe_path = parts[0] rest_args = parts[1] if len(parts) > 1 else '' cmd = [exe_path] if rest_args: cmd += rest_args.split() if "/S" not in cmd: cmd.append("/S") try: # Popen, NOT check_call -- exit code 0 does not mean the # uninstall actually finished, see note above. subprocess.Popen(cmd) except Exception as e: print 'launch_error:' + str(e) kill_qfinder() removed = False attempt = 0 while attempt < 12: time.sleep(5) kill_qfinder() if not find_entries(): removed = True break attempt += 1 if not removed: remaining = find_entries() for entry in remaining: force_remove(entry) time.sleep(2) removed = not find_entries() if removed: print 'retcode' + str(0) + 'retcode' else: print 'retcode' + str(1) + 'retcode' def spm_update(dir, args): os.chdir(dir) argslist = args.split() try: subprocess.check_call(argslist) import time time.sleep(60) os.popen('Taskkill /IM "QfinderPro.exe" /F').read() print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode'