summaryrefslogtreecommitdiffhomepage
path: root/bazarr.py
blob: d0c983a0b72bdb9441038e7b151a711a0b33aaf8 (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
# coding=utf-8

from __future__ import absolute_import
from __future__ import print_function
import subprocess as sp
import time
import os
import sys
import platform
import re

from bazarr.get_args import args


def check_python_version():
    python_version = platform.python_version_tuple()
    minimum_python_version_tuple = (2, 7, 13)
    minimum_python3_version_tuple = (3, 6, 0)
    minimum_python_version = ".".join(str(i) for i in minimum_python_version_tuple)
    minimum_python3_version = ".".join(str(i) for i in minimum_python3_version_tuple)

    if int(python_version[0]) == minimum_python3_version_tuple[0]:
        if int(python_version[1]) >= minimum_python3_version_tuple[1]:
            pass
        else:
            print("Python " + minimum_python3_version + " or greater required. Current version is " + platform.python_version() + ". Please upgrade Python.")
            os._exit(0)

    elif int(python_version[1]) < minimum_python_version_tuple[1] or int(re.search(r'\d+', python_version[2]).group()) < minimum_python_version_tuple[2]:
        print("Python " + minimum_python_version + " or greater required. Current version is " + platform.python_version() + ". Please upgrade Python.")
        os._exit(0)


check_python_version()

dir_name = os.path.dirname(__file__)


def start_bazarr():
    script = [sys.executable, "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]
    
    ep = sp.Popen(script, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
    print("Bazarr starting...")
    try:
        for line in iter(ep.stdout.readline, ''):
            sys.stdout.buffer.write(line)
    except KeyboardInterrupt:
        pass


if __name__ == '__main__':
    restartfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.restart'))
    stopfile = os.path.normcase(os.path.join(args.config_dir, 'bazarr.stop'))
    
    try:
        os.remove(restartfile)
    except:
        pass
    
    try:
        os.remove(stopfile)
    except:
        pass
    
    
    def daemon():
        if os.path.exists(stopfile):
            try:
                os.remove(stopfile)
            except:
                print('Unable to delete stop file.')
            else:
                print('Bazarr exited.')
                os._exit(0)
        
        if os.path.exists(restartfile):
            try:
                os.remove(restartfile)
            except:
                print('Unable to delete restart file.')
            else:
                start_bazarr()
    
    
    start_bazarr()
    
    # Keep the script running forever.
    while True:
        daemon()
        time.sleep(1)