1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
import subprocess as sp
import threading
import time
import os
import logging
import sys
import getopt
log = logging.getLogger()
log.setLevel(logging.DEBUG)
out_hdlr = logging.StreamHandler(sys.stdout)
out_hdlr.setLevel(logging.INFO)
log.addHandler(out_hdlr)
arguments = []
try:
opts, args = getopt.getopt(sys.argv[1:],"h:",["no-update", "config="])
except getopt.GetoptError:
print 'bazarr.py -h --no-update --config <config_directory>'
sys.exit(2)
for opt, arg in opts:
arguments.append(opt)
if arg != '':
arguments.append(arg)
dir_name = os.path.dirname(__file__)
def start_bazarr():
script = [sys.executable, os.path.normcase(os.path.join(globals()['dir_name'], 'bazarr/main.py'))] + globals()['arguments']
ep = sp.Popen(script, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
logging.info("Bazarr starting...")
try:
for line in iter(ep.stdout.readline, ''):
sys.stdout.write(line)
except KeyboardInterrupt:
pass
if __name__ == '__main__':
restartfile = os.path.normcase(os.path.join(globals()['dir_name'], 'bazarr.restart'))
stopfile = os.path.normcase(os.path.join(globals()['dir_name'], 'bazarr.stop'))
try:
os.remove(restartfile)
except:
pass
try:
os.remove(stopfile)
except:
pass
def daemon():
threading.Timer(1.0, daemon).start()
if os.path.exists(stopfile):
try:
os.remove(stopfile)
except:
logging.error('Unable to delete stop file.')
else:
logging.info('Bazarr exited.')
os._exit(0)
if os.path.exists(restartfile):
try:
os.remove(restartfile)
except:
logging.error('Unable to delete restart file.')
else:
start_bazarr()
daemon()
start_bazarr()
# Keep the script running forever.
while True:
time.sleep(0.001)
|