forked from CouchPotato/CouchPotatoV1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CouchPotato.py
executable file
·235 lines (188 loc) · 6.92 KB
/
CouchPotato.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env python
import sys
import os
rundir = os.path.dirname(os.path.abspath(__file__))
try:
frozen = sys.frozen
except AttributeError:
frozen = False
# Define path based on frozen state
if frozen:
path_base = os.environ['_MEIPASS2']
rundir = os.path.dirname(sys.executable)
#path_base = os.path.dirname(sys.executable)
else:
path_base = rundir
# Include paths
sys.path.insert(0, path_base)
sys.path.insert(0, os.path.join(path_base, 'library'))
def server_start():
from optparse import OptionParser
p = OptionParser()
p.add_option('-d', action = "store_true",
dest = 'daemonize', help = "Run the server as a daemon")
p.add_option('-q', '--quiet', action = "store_true",
dest = 'quiet', help = "Don't log to console")
p.add_option('-p', '--pidfile',
dest = 'pidfile', default = None,
help = "Store the process id in the given file")
p.add_option('--config',
dest = 'config', default = None,
help = "Path to config.ini file")
p.add_option('--datadir',
dest = 'datadir', default = None,
help = "Path to the data directory")
options, args = p.parse_args()
if options.datadir:
datadir = options.datadir
if not os.path.isdir(datadir):
os.makedirs(datadir)
else:
datadir = rundir
datadir = os.path.abspath(datadir)
if not os.access(datadir, os.W_OK):
raise SystemExit("Data dir must be writeable '" + datadir + "'")
import app.config
app.config.DATADIR = datadir
if options.config:
config = options.config
else:
config = os.path.join(datadir, 'config.ini')
config = os.path.abspath(config)
if not os.access(os.path.dirname(config), os.W_OK) and not os.access(config, os.W_OK):
if not os.path.exists(os.path.dirname(config)):
os.makedirs(os.path.dirname(config))
else:
raise SystemExit("Directory for config file must be writeable")
import cherrypy
import app.config.render
# Configure logging
from app.config.cplog import CPLog
# Setup logging
debug = os.path.isfile(os.path.join(datadir, 'debug.conf'))
log = CPLog()
log.config(os.path.join(datadir, 'logs'), debug)
# Create cache dir
cachedir = os.path.join(datadir, 'cache')
if not os.path.isdir(cachedir):
os.mkdir(cachedir)
# Stop logging
if options.quiet or options.daemonize:
cherrypy.config.update({'log.screen': False})
# Config app
from app.config.configApp import configApp
ca = configApp(config)
# Setup db
from app.config.db import initDb
initDb()
from app.config.routes import setup as Routes
from app.lib.cron import CronJobs
from app.config.updater import Updater
from cherrypy.process import plugins
# Check an see if CP is already running
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ca.get('global', 'host')
port = int(ca.get('global', 'port'))
try:
s.connect((host, port))
s.shutdown(0)
app.launchBrowser(host, port)
return
except:
pass
# Start threads
myCrons = CronJobs(cherrypy.engine, ca, debug)
myCrons.subscribe()
# Update script
myUpdater = Updater(cherrypy.engine)
myUpdater.subscribe()
# User config, use own stuff to prevent unexpected results
cherrypy.config.update({
'global': {
'server.thread_pool': 10,
'server.socket_port': int(ca.get('global', 'port')),
'server.socket_host': ca.get('global', 'host'),
'server.environment': ca.get('global', 'server.environment'),
'engine.autoreload_on': ca.get('global', 'server.environment') == 'development',
'tools.mako.collection_size': 500,
'tools.mako.directories': os.path.join(path_base, 'app', 'views'),
'basePath': path_base,
'runPath': rundir,
'cachePath': cachedir,
'debug': debug,
'frozen': frozen,
# Global workers
'config': ca,
'updater': myUpdater,
'cron': myCrons.threads,
'searchers': myCrons.searchers,
'flash': app.flash()
}
})
# Static config
conf = {
'/': {
'request.dispatch': Routes(),
'tools.sessions.on': True,
'tools.sessions.timeout': 240,
'tools.gzip.on': True,
'tools.gzip.mime_types': ['text/html', 'text/plain', 'text/css', 'text/javascript', 'application/javascript']
},
'/media':{
'tools.staticdir.on': True,
'tools.staticdir.root': path_base,
'tools.staticdir.dir': "media",
'tools.expires.on': True,
'tools.expires.secs': 3600 * 24 * 7
},
'/cache':{
'tools.staticdir.on': True,
'tools.staticdir.root': datadir,
'tools.staticdir.dir': "cache",
'tools.expires.on': True,
'tools.expires.secs': 3600 * 24 * 7
}
}
# Don't use auth when password is empty
if ca.get('global', 'password') != '':
conf['/'].update({
'tools.basic_auth.on': True,
'tools.basic_auth.realm': 'Awesomeness',
'tools.basic_auth.users': {ca.get('global', 'username'):ca.get('global', 'password')},
'tools.basic_auth.encrypt': app.clearAuthText
})
cherrypy.tools.mybasic_auth = cherrypy.Tool('on_start_resource', app.basicAuth)
# I'll do my own logging, thanks!
#cherrypy.log.error_log.propagate = False
#cherrypy.log.access_log.propagate = False
#No Root controller as we provided all our own.
cherrypy.tree.mount(root = None, config = conf)
#HTTP Errors
def http_error_hander(status, message, traceback, version):
args = [status, message, traceback, version]
return "<html><body><h1>Error %s</h1>Something unexpected has happened.</body></html>" % args[0]
cherrypy.config.update({'error_page.default' : http_error_hander})
# Deamonize
if options.daemonize:
plugins.Daemonizer(cherrypy.engine).subscribe()
# PIDfile
if options.pidfile:
plugins.PIDFile(cherrypy.engine, options.pidfile).subscribe()
# Setup the signal handler
if hasattr(cherrypy.engine, "signal_handler"):
cherrypy.engine.signal_handler.subscribe()
if hasattr(cherrypy.engine, "console_control_handler"):
cherrypy.engine.console_control_handler.subscribe()
## start the app
try:
cherrypy.engine.start()
except:
sys.exit(1)
else:
# Launch browser
if ca.get('global', 'launchbrowser'):
app.launchBrowser(ca.get('global', 'host'), ca.get('global', 'port'))
cherrypy.engine.block()
if __name__ == '__main__':
server_start()