-
Notifications
You must be signed in to change notification settings - Fork 625
/
PlexConnect.py
executable file
·203 lines (157 loc) · 5.65 KB
/
PlexConnect.py
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
"""
PlexConnect
Sources:
inter-process-communication (queue): http://pymotw.com/2/multiprocessing/communication.html
"""
import sys, time
from os import sep
import socket
from multiprocessing import Process, Pipe
from multiprocessing.managers import BaseManager
import signal, errno
import argparse
from Version import __VERSION__
import DNSServer, WebServer
import Settings, ATVSettings
from PILBackgrounds import isPILinstalled
from Debug import * # dprint()
CONFIG_PATH = '.'
def getIP_self():
cfg = param['CSettings']
if cfg.getSetting('enable_plexgdm')=='False':
dprint('PlexConnect', 0, "IP_PMS: "+cfg.getSetting('ip_pms'))
if cfg.getSetting('enable_plexconnect_autodetect')=='True':
# get public ip of machine running PlexConnect
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('1.2.3.4', 1000))
IP = s.getsockname()[0]
dprint('PlexConnect', 0, "IP_self: "+IP)
else:
# manual override from "settings.cfg"
IP = cfg.getSetting('ip_plexconnect')
dprint('PlexConnect', 0, "IP_self (from settings): "+IP)
return IP
# initializer for Manager, proxy-ing ATVSettings to WebServer/XMLConverter
def initProxy():
signal.signal(signal.SIGINT, signal.SIG_IGN)
procs = {}
pipes = {}
param = {}
running = False
def startup():
global procs
global pipes
global param
global running
# Settings
cfg = Settings.CSettings(CONFIG_PATH)
param['CSettings'] = cfg
# Logfile
if cfg.getSetting('logpath').startswith('.'):
# relative to current path
logpath = sys.path[0] + sep + cfg.getSetting('logpath')
else:
# absolute path
logpath = cfg.getSetting('logpath')
param['LogFile'] = logpath + sep + 'PlexConnect.log'
param['LogLevel'] = cfg.getSetting('loglevel')
dinit('PlexConnect', param, True) # init logging, new file, main process
dprint('PlexConnect', 0, "Version: {0}", __VERSION__)
dprint('PlexConnect', 0, "Python: {0}", sys.version)
dprint('PlexConnect', 0, "Host OS: {0}", sys.platform)
dprint('PlexConnect', 0, "PILBackgrounds: Is PIL installed? {0}", isPILinstalled())
# more Settings
param['IP_self'] = getIP_self()
param['HostToIntercept'] = cfg.getSetting('hosttointercept')
param['baseURL'] = 'http://'+ param['HostToIntercept']
# proxy for ATVSettings
proxy = BaseManager()
proxy.register('ATVSettings', ATVSettings.CATVSettings)
proxy.start(initProxy)
param['CATVSettings'] = proxy.ATVSettings(CONFIG_PATH)
running = True
# init DNSServer
if cfg.getSetting('enable_dnsserver')=='True':
master, slave = Pipe() # endpoint [0]-PlexConnect, [1]-DNSServer
proc = Process(target=DNSServer.Run, args=(slave, param))
proc.start()
time.sleep(0.1)
if proc.is_alive():
procs['DNSServer'] = proc
pipes['DNSServer'] = master
else:
dprint('PlexConnect', 0, "DNSServer not alive. Shutting down.")
running = False
# init WebServer
if running:
master, slave = Pipe() # endpoint [0]-PlexConnect, [1]-WebServer
proc = Process(target=WebServer.Run, args=(slave, param))
proc.start()
time.sleep(0.1)
if proc.is_alive():
procs['WebServer'] = proc
pipes['WebServer'] = master
else:
dprint('PlexConnect', 0, "WebServer not alive. Shutting down.")
running = False
# init WebServer_SSL
if running and \
cfg.getSetting('enable_webserver_ssl')=='True':
master, slave = Pipe() # endpoint [0]-PlexConnect, [1]-WebServer
proc = Process(target=WebServer.Run_SSL, args=(slave, param))
proc.start()
time.sleep(0.1)
if proc.is_alive():
procs['WebServer_SSL'] = proc
pipes['WebServer_SSL'] = master
else:
dprint('PlexConnect', 0, "WebServer_SSL not alive. Shutting down.")
running = False
# not started successful - clean up
if not running:
cmdShutdown()
shutdown()
return running
def run(timeout=60):
# do something important
try:
time.sleep(timeout)
except IOError as e:
if e.errno == errno.EINTR and not running:
pass # mask "IOError: [Errno 4] Interrupted function call"
else:
raise
return running
def shutdown():
for slave in procs:
procs[slave].join()
param['CATVSettings'].saveSettings()
dprint('PlexConnect', 0, "Shutdown")
def cmdShutdown():
global running
running = False
# send shutdown to all pipes
for slave in pipes:
pipes[slave].send('shutdown')
dprint('PlexConnect', 0, "Shutting down.")
def sighandler_shutdown(signum, frame):
signal.signal(signal.SIGINT, signal.SIG_IGN) # we heard you!
cmdShutdown()
if __name__=="__main__":
signal.signal(signal.SIGINT, sighandler_shutdown)
signal.signal(signal.SIGTERM, sighandler_shutdown)
parser = argparse.ArgumentParser()
parser.add_argument('--config_path', metavar='<config_path>', required=False,
help='path of folder containing config files, relative to PlexConnect.py')
args = parser.parse_args()
if args.config_path:
CONFIG_PATH = args.config_path
dprint('PlexConnect', 0, "***")
dprint('PlexConnect', 0, "PlexConnect")
dprint('PlexConnect', 0, "Press CTRL-C to shut down.")
dprint('PlexConnect', 0, "***")
running = startup()
while running:
running = run()
shutdown()