From d32e1527a8c0daecd8da70b69b98f983eefff761 Mon Sep 17 00:00:00 2001 From: j Date: Mon, 1 Feb 2016 13:15:34 +0530 Subject: [PATCH] fix windows restart --- oml/commands.py | 35 ++++++++++++++++++++++++++++++++--- oml/update.py | 9 ++------- oml/utils.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/oml/commands.py b/oml/commands.py index 31804fa..b17e3f5 100644 --- a/oml/commands.py +++ b/oml/commands.py @@ -2,11 +2,12 @@ # vi:si:et:sw=4:sts=4:ts=4 -import subprocess from os.path import join, exists, dirname import os -import sys import shutil +import signal +import subprocess +import sys import settings from utils import run, get @@ -55,7 +56,35 @@ def command_stop(*args): """ Stop Open Media Libary """ - pass + if sys.platform == 'win32': + from utils import check_pid + if args: + pid = args[0] + else: + pid = os.path.join(settings.data_path, 'openmedialibrary.pid') + try: + with open(pid) as fd: + pid = int(fd.read()) + except: + return + if check_pid(pid): + os.kill(pid, signal.SIGTERM) + +def command_restart(*args): + """ + Restart Open Media Libary + """ + if sys.platform == 'win32': + from utils import check_pid, ctl + pidfile = os.path.join(settings.data_path, 'openmedialibrary.pid') + try: + with open(pidfile) as fd: + pid = int(fd.read()) + except: + pid = None + if pid and check_pid(pid): + os.kill(pid, signal.SIGTERM) + ctl('server', pidfile) def command_install_launcher(*args): """ diff --git a/oml/update.py b/oml/update.py index 3e5611e..874e3aa 100644 --- a/oml/update.py +++ b/oml/update.py @@ -19,8 +19,8 @@ import OpenSSL.crypto import ox from oxtornado import actions -import state import settings +import utils import db import logging @@ -206,12 +206,7 @@ def update_available(): def restart_oml(update=False): if update: get_latest_release() - if sys.platform == 'win32': - subprocess.Popen([os.path.join(settings.base_dir, 'ctl.bat'), 'restart'], - start_new_session=True) - else: - subprocess.Popen([os.path.join(settings.base_dir, 'ctl'), 'restart'], - close_fds=True, start_new_session=True) + utils.ctl('restart') def get_app_version(app): plist = app + '/Contents/Info.plist' diff --git a/oml/utils.py b/oml/utils.py index aab530a..2e42c25 100644 --- a/oml/utils.py +++ b/oml/utils.py @@ -433,3 +433,33 @@ def update_static(): for file in files ]) ) + +def check_pid(pid): + try: + os.kill(pid, 0) + except: + return False + else: + return True + +def check_pidfile(pid): + try: + with open(pid) as fd: + pid = int(fd.read()) + except: + return False + return check_pid(pid) + +def ctl(*args): + if sys.platform == 'win32': + import settings + platform_win32 = os.path.normpath(os.path.join(settings.base_dir, '..', 'platform_win32')) + python = os.path.join(platform_win32, 'pythonw.exe') + cmd = [python, 'oml'] + list(args) + startupinfo = subprocess.STARTUPINFO() + startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW + startupinfo.wShowWindow = subprocess.SW_HIDE + subprocess.Popen(cmd, cwd=settings.base_dir, start_new_session=True, startupinfo=startupinfo) + else: + subprocess.Popen([os.path.join(settings.base_dir, 'ctl')] + list(args), + close_fds=True, start_new_session=True)