import os import subprocess import time import _winreg import re import shlex def spm_install(dir, args): try: if dir is not None and os.path.isdir(dir): os.chdir(dir) if args is None: print 'retcode1retcode' return argslist = shlex.split(args, posix=False) subprocess.check_call(argslist) print 'retcode0retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode' except Exception: print 'retcode1retcode' # ============================================================ # UNINSTALL # ============================================================ def spm_uninstall(dir, args): try: if dir is not None and os.path.isdir(dir): os.chdir(dir) if args is None: print 'retcode1retcode' return argslist = shlex.split(args, posix=False) subprocess.check_call(argslist) print 'retcode0retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode' except Exception: print 'retcode1retcode' # ============================================================ # UPDATE # ============================================================ def spm_update(dir, args): import os import subprocess import time import _winreg import re import shlex # -------------------------------------------------------- # MuseScore product name # -------------------------------------------------------- PRODUCT_NAME = 'MuseScore Studio' UNINSTALL_KEY = ( 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall' ) uninstall_commands = [] # -------------------------------------------------------- # Registry locations to check # -------------------------------------------------------- registry_locations = [ (_winreg.HKEY_LOCAL_MACHINE, _winreg.KEY_WOW64_64KEY), (_winreg.HKEY_LOCAL_MACHINE, _winreg.KEY_WOW64_32KEY), (_winreg.HKEY_CURRENT_USER, _winreg.KEY_WOW64_64KEY), (_winreg.HKEY_CURRENT_USER, _winreg.KEY_WOW64_32KEY) ] # -------------------------------------------------------- # Search installed MuseScore Studio applications # -------------------------------------------------------- for root_key, wow_flag in registry_locations: try: access = _winreg.KEY_READ | wow_flag uninstall_root = _winreg.OpenKey( root_key, UNINSTALL_KEY, 0, access ) except: continue index = 0 while True: try: subkey_name = _winreg.EnumKey( uninstall_root, index ) index += 1 except: break app_key = None try: app_key = _winreg.OpenKey( uninstall_root, subkey_name, 0, _winreg.KEY_READ ) try: display_name = _winreg.QueryValueEx( app_key, 'DisplayName' )[0] except: display_name = '' try: uninstall_string = _winreg.QueryValueEx( app_key, 'UninstallString' )[0] except: uninstall_string = '' if display_name: display_name_clean = display_name.strip().lower() if display_name_clean.startswith( PRODUCT_NAME.lower() ): if uninstall_string: # Avoid duplicate registry entries if uninstall_string not in uninstall_commands: uninstall_commands.append( uninstall_string ) _winreg.CloseKey(app_key) app_key = None except: if app_key is not None: try: _winreg.CloseKey(app_key) except: pass try: _winreg.CloseKey(uninstall_root) except: pass for uninstall_string in uninstall_commands: try: command = uninstall_string.strip() if not command: continue if 'msiexec' in command.lower(): # Find MSI ProductCode guid_match = re.search( r'\{[0-9A-Fa-f]{8}-' r'[0-9A-Fa-f]{4}-' r'[0-9A-Fa-f]{4}-' r'[0-9A-Fa-f]{4}-' r'[0-9A-Fa-f]{12}\}', command ) if guid_match: product_code = guid_match.group(0) uninstall_command = [ 'msiexec.exe', '/x', product_code, '/qn', '/norestart' ] # Execute MSI uninstall subprocess.call(uninstall_command) else: uninstall_command = shlex.split( command, posix=False ) uninstall_command.append('/S') subprocess.call(uninstall_command) except: pass time.sleep(20) try: # dir can be None in ITSM. # Only change directory when a valid directory is supplied. if dir is not None: if os.path.isdir(dir): os.chdir(dir) if args is None: print 'retcode1retcode' return # Parse installer command correctly, # including paths containing spaces. argslist = shlex.split( args, posix=False ) subprocess.check_call(argslist) print 'retcode0retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode' except Exception: print 'retcode1retcode'