|
@ -1,10 +1,9 @@ |
|
|
#!/usr/bin/env python |
|
|
#!/usr/bin/env python |
|
|
from os.path import dirname |
|
|
from os.path import dirname |
|
|
from signal import signal, SIGTERM |
|
|
|
|
|
import os |
|
|
import os |
|
|
|
|
|
import signal |
|
|
import subprocess |
|
|
import subprocess |
|
|
import sys |
|
|
import sys |
|
|
import time |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Root path |
|
|
# Root path |
|
@ -13,60 +12,69 @@ base_path = dirname(os.path.abspath(__file__)) |
|
|
# Insert local directories into path |
|
|
# Insert local directories into path |
|
|
sys.path.insert(0, os.path.join(base_path, 'libs')) |
|
|
sys.path.insert(0, os.path.join(base_path, 'libs')) |
|
|
|
|
|
|
|
|
from couchpotato.core.logger import CPLog |
|
|
|
|
|
log = CPLog(__name__) |
|
|
|
|
|
|
|
|
|
|
|
# Get options via arg |
|
|
class Loader(object): |
|
|
from couchpotato.runner import getOptions |
|
|
|
|
|
from couchpotato.core.helpers.variable import getDataDir |
|
|
|
|
|
options = getOptions(base_path, sys.argv[1:]) |
|
|
|
|
|
data_dir = getDataDir() |
|
|
|
|
|
|
|
|
|
|
|
def start(): |
|
|
do_restart = True |
|
|
try: |
|
|
|
|
|
args = [sys.executable] + [os.path.join(base_path, __file__)] + sys.argv[1:] |
|
|
|
|
|
new_environ = os.environ.copy() |
|
|
|
|
|
new_environ['cp_main'] = 'true' |
|
|
|
|
|
|
|
|
|
|
|
if os.name == 'nt': |
|
|
|
|
|
for key, value in new_environ.iteritems(): |
|
|
|
|
|
if isinstance(value, unicode): |
|
|
|
|
|
new_environ[key] = value.encode('iso-8859-1') |
|
|
|
|
|
|
|
|
|
|
|
subprocess.call(args, env = new_environ) |
|
|
|
|
|
return os.path.isfile(os.path.join(data_dir, 'restart')) |
|
|
|
|
|
except KeyboardInterrupt, e: |
|
|
|
|
|
pass |
|
|
|
|
|
except Exception, e: |
|
|
|
|
|
log.critical(e) |
|
|
|
|
|
return 0 |
|
|
|
|
|
|
|
|
|
|
|
from couchpotato.runner import runCouchPotato |
|
|
def __init__(self): |
|
|
def main(): |
|
|
|
|
|
if os.environ.get('cp_main', 'false') == 'true': |
|
|
from couchpotato.core.logger import CPLog |
|
|
try: |
|
|
self.log = CPLog(__name__) |
|
|
runCouchPotato(options, base_path, sys.argv[1:]) |
|
|
|
|
|
except Exception, e: |
|
|
|
|
|
log.critical(e) |
|
|
|
|
|
else: |
|
|
|
|
|
while 1: |
|
|
|
|
|
restart = start() |
|
|
|
|
|
if not restart: |
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Get options via arg |
|
|
|
|
|
from couchpotato.runner import getOptions |
|
|
|
|
|
from couchpotato.core.helpers.variable import getDataDir |
|
|
|
|
|
self.options = getOptions(base_path, sys.argv[1:]) |
|
|
|
|
|
self.data_dir = getDataDir() |
|
|
|
|
|
|
|
|
|
|
|
def addSignals(self): |
|
|
|
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, self.onExit) |
|
|
|
|
|
signal.signal(signal.SIGTERM, lambda signum, stack_frame: sys.exit(1)) |
|
|
|
|
|
|
|
|
|
|
|
from couchpotato.core.event import addEvent |
|
|
|
|
|
addEvent('app.after_shutdown', self.afterShutdown) |
|
|
|
|
|
|
|
|
|
|
|
def afterShutdown(self, restart): |
|
|
|
|
|
self.do_restart = restart |
|
|
|
|
|
|
|
|
|
|
|
def onExit(self, signal, frame): |
|
|
from couchpotato.core.event import fireEvent |
|
|
from couchpotato.core.event import fireEvent |
|
|
fireEvent('app.crappy_shutdown', single = True) |
|
|
fireEvent('app.crappy_shutdown', single = True) |
|
|
time.sleep(1) |
|
|
|
|
|
|
|
|
|
|
|
sys.exit() |
|
|
def run(self): |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
self.addSignals() |
|
|
|
|
|
|
|
|
signal(SIGTERM, lambda signum, stack_frame: sys.exit(1)) |
|
|
try: |
|
|
|
|
|
from couchpotato.runner import runCouchPotato |
|
|
|
|
|
runCouchPotato(self.options, base_path, sys.argv[1:]) |
|
|
|
|
|
except Exception, e: |
|
|
|
|
|
self.log.critical(e) |
|
|
|
|
|
|
|
|
|
|
|
if self.do_restart: |
|
|
|
|
|
self.restart() |
|
|
|
|
|
|
|
|
|
|
|
sys.exit(0) |
|
|
|
|
|
|
|
|
|
|
|
def restart(self): |
|
|
|
|
|
try: |
|
|
|
|
|
args = [sys.executable] + [os.path.join(base_path, __file__)] + sys.argv[1:] |
|
|
|
|
|
subprocess.Popen(args) |
|
|
|
|
|
except Exception, e: |
|
|
|
|
|
self.log.critical(e) |
|
|
|
|
|
return 0 |
|
|
|
|
|
|
|
|
if options.daemon and options.pid_file and not os.environ.get('cp_main'): |
|
|
def daemonize(self): |
|
|
|
|
|
|
|
|
|
|
|
if self.options.daemon and self.options.pid_file: |
|
|
from daemon import Daemon |
|
|
from daemon import Daemon |
|
|
daemon = Daemon(options.pid_file) |
|
|
daemon = Daemon(self.options.pid_file) |
|
|
daemon.daemonize() |
|
|
daemon.daemonize() |
|
|
|
|
|
|
|
|
main() |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
l = Loader() |
|
|
|
|
|
l.daemonize() |
|
|
|
|
|
l.run() |
|
|