import os import subprocess import time # NoMachine services / processes that lock files in C:\Program Files\NoMachine\bin NX_SERVICE = 'nxservice' NX_PROCESSES = [ 'nxplayer.exe', 'nxclient.exe', 'nxrunner.exe', 'nxdisplay.exe', 'nxnode.exe', 'nxserver.exe', 'nxexec.exe', 'nxfs.exe', 'nxfsserver.exe', 'nxkb.exe', 'nxlpd.exe', 'nxpost.exe', 'nxsh.exe', 'nxupdatecli.exe', 'nxd.exe', 'nxdx32.exe', 'nxdx64.exe', 'nxservice64.exe', 'nxservice.exe' ] def _run_quiet(cmd): # run a command, hide output, never raise try: devnull = open(os.devnull, 'w') rc = subprocess.call(cmd, stdout=devnull, stderr=devnull) devnull.close() return rc except Exception: return 1 # Stop NoMachine service and kill all its processes so DLLs are not locked def stop_nomachine(): _run_quiet(['net', 'stop', NX_SERVICE]) time.sleep(5) for proc in NX_PROCESSES: _run_quiet(['taskkill', '/F', '/IM', proc]) time.sleep(3) # Start NoMachine service after install / update def start_nomachine(): _run_quiet(['net', 'start', NX_SERVICE]) # 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: stop_nomachine() subprocess.check_call(argslist) time.sleep(15) start_nomachine() print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode' except OSError: print 'retcode' + str(1) + '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: stop_nomachine() subprocess.check_call(argslist) time.sleep(10) print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode' except OSError: print 'retcode' + str(1) + 'retcode' # 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: stop_nomachine() try: subprocess.check_call(argslist) except subprocess.CalledProcessError: # first attempt failed (file lock etc.) - stop everything again and retry once stop_nomachine() time.sleep(5) subprocess.check_call(argslist) time.sleep(15) start_nomachine() print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode' except OSError: print 'retcode' + str(1) + 'retcode'