summaryrefslogtreecommitdiffhomepage
path: root/bazarr.py
blob: abe2f5c53a2d7355b1838db959f33db347b11d2d (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import subprocess as sp
import threading
import time
import os
import signal
import platform
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)


def start_bazarr():
    script = [sys.executable, os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr/main.py'))] + globals()['arguments']

    pidfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.pid'))
    ep = sp.Popen(script, stdout=sp.PIPE, stderr=sp.STDOUT)
    try:
        file = open(pidfile,'w')
    except:
        logging.error("Error trying to write pid file.")
    else:
        file.write(str(ep.pid))
        file.close()
        logging.info("Bazarr starting with process id: " + str(ep.pid) + "...")
        for line in iter(ep.stdout.readline, ''):
            sys.stdout.write(line)


def shutdown_bazarr(restarting):
    pidfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.pid'))
    if os.path.exists(pidfile):
        try:
            file = open(pidfile,'r')
        except:
            logging.error("Error trying to read pid file.")
        else:
            pid = file.read()
            file.close()
            try:
                os.remove(pidfile)
            except:
                logging.error("Unable to delete pid file.")
            else:
                logging.info('Bazarr stopping...')
                if restarting is False:
                    stopfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.stop'))
                    file = open(stopfile, 'w')
                    file.write('')
                    file.close()
                os.kill(int(pid), signal.SIGINT)
    else:
        logging.warn("pid file doesn't exist. You must start Bazarr first.")


def restart_bazarr():
    restartfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.restart'))
    file = open(restartfile, 'w')
    file.write('')
    file.close()


# GetExitCodeProcess uses a special exit code to indicate that the process is
# still running.
_STILL_ACTIVE = 259


def is_pid_running(pid):
    return (_is_pid_running_on_windows(pid) if platform.system() == "Windows"
            else _is_pid_running_on_unix(pid))


def _is_pid_running_on_unix(pid):
    try:
        os.kill(pid, 0)
    except OSError:
        return False
    return True


def _is_pid_running_on_windows(pid):
    import ctypes.wintypes

    kernel32 = ctypes.windll.kernel32
    handle = kernel32.OpenProcess(1, 0, pid)
    if handle == 0:
        return False

    # If the process exited recently, a pid may still exist for the handle.
    # So, check if we can get the exit code.
    exit_code = ctypes.wintypes.DWORD()
    is_running = (
            kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)) == 0)
    kernel32.CloseHandle(handle)

    # See if we couldn't get the exit code or the exit code indicates that the
    # process is still running.
    return is_running or exit_code.value == _STILL_ACTIVE


if __name__ == '__main__':
    pidfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.pid'))
    restartfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.restart'))
    stopfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.stop'))

    try:
        os.remove(pidfile)
        os.remove(restartfile)
        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:
                shutdown_bazarr(True)
                start_bazarr()

        if os.path.exists(pidfile):
            try:
                file = open(pidfile, 'r')
            except:
                logging.error("Error trying to read pid file.")
            else:
                pid = int(file.read())
                file.close()

                if is_pid_running(pid) is False:
                    logging.warn('Bazarr unexpectedly exited. Starting it back.')
                    start_bazarr()


    daemon()

    start_bazarr()

    # Keep the script running forever.
    while True:
        time.sleep(0.001)