diff --git a/CouchPotato.py b/CouchPotato.py index b1591b3a..f0ba0b45 100755 --- a/CouchPotato.py +++ b/CouchPotato.py @@ -23,7 +23,7 @@ data_dir = getDataDir() def start(): try: - args = [sys.executable] + [os.path.join(os.environ['PWD'], __file__)] + sys.argv[1:] + args = [sys.executable] + [os.path.join(base_path, __file__)] + sys.argv[1:] new_environ = os.environ.copy() new_environ['cp_main'] = 'true' diff --git a/couchpotato/core/_base/_core/getppid.py b/couchpotato/core/_base/_core/getppid.py new file mode 100644 index 00000000..6854e291 --- /dev/null +++ b/couchpotato/core/_base/_core/getppid.py @@ -0,0 +1,45 @@ +from ctypes import * +from ctypes.wintypes import * +import win32process + + +class PROCESSENTRY32(Structure): + _fields_ = ( + ('dwSize', DWORD,), + ('cntUsage', DWORD,), + ('th32ProcessID', DWORD,), + ('th32DefaultHeapID', POINTER(ULONG),), + ('th32ModuleID', DWORD,), + ('cntThreads', DWORD,), + ('th32ParentProcessID', DWORD,), + ('pcPriClassBase', LONG,), + ('dwFlags', DWORD,), + ('szExeFile', c_char * MAX_PATH,), + ) + + +def getppid(pid): + """the Windows version of os.getppid""" + pe = PROCESSENTRY32() + pe.dwSize = sizeof(PROCESSENTRY32) + + snapshot = windll.kernel32.CreateToolhelp32Snapshot(2, 0) + try: + if not windll.kernel32.Process32First(snapshot, byref(pe)): + raise WindowsError + while pe.th32ProcessID != pid: + if not windll.kernel32.Process32Next(snapshot, byref(pe)): + raise WindowsError + result = pe.th32ParentProcessID + finally: + windll.kernel32.CloseHandle(snapshot) + + if result not in win32process.EnumProcesses(): + result = 1 + + return result + + +import os +if not hasattr(os, 'getppid'): + os.getppid = getppid diff --git a/couchpotato/core/_base/_core/main.py b/couchpotato/core/_base/_core/main.py index 7c64e061..d9c5e54f 100644 --- a/couchpotato/core/_base/_core/main.py +++ b/couchpotato/core/_base/_core/main.py @@ -11,12 +11,15 @@ import time import traceback import webbrowser +if os.name == 'nt': + import getppid + log = CPLog(__name__) class Core(Plugin): - ignore_restart = ['Core.crappyRestart', 'Core.crappyShutdown'] + ignore_restart = ['Core.crappyRestart', 'Core.crappyShutdown', 'Core.monitorParent'] shutdown_started = False def __init__(self): @@ -27,6 +30,7 @@ class Core(Plugin): addEvent('app.crappy_shutdown', self.crappyShutdown) addEvent('app.crappy_restart', self.crappyRestart) addEvent('app.load', self.launchBrowser, priority = 1) + addEvent('app.load', self.monitorParent) addEvent('app.base_url', self.createBaseUrl) addEvent('app.api_url', self.createApiUrl) @@ -34,6 +38,19 @@ class Core(Plugin): self.removeRestartFile() + def monitorParent(self): + while 1: + if os.name == 'nt': + if os.getppid(os.getpid()) <= 1: + break + else: + if os.getppid() <= 1: + break + time.sleep(1) + + log.info('Starterscript has shutdown, shutdown subprocess') + fireEvent('app.crappy_shutdown') + def md5Password(self, value): return md5(value) if value else ''