This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGazee.py
executable file
·207 lines (180 loc) · 7.57 KB
/
Gazee.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
#!/usr/bin/env python3
# This file is part of Gazee.
#
# Gazee is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Gazee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gazee. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import argparse
import logging
import cherrypy
from cherrypy.process.plugins import Daemonizer, PIDFile
import gazee
from gazee import Gazee, ComicScanner
logging = logging.getLogger(__name__)
gazee.FULL_PATH = os.path.abspath(__file__)
# Verify our app is working out of the install directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
if (sys.platform == 'win32' and sys.executable.split('\\')[-1] == 'pythonw.exe'):
sys.stdout = open(os.devnull, "w")
sys.stderr = open(os.devnull, "w")
def main():
parser = argparse.ArgumentParser(description='Gazee - Open Comic Book Reader')
parser.add_argument('-d', '--daemon', action='store_true', help='Run as a daemon')
parser.add_argument('-c', '--datadir', help='Set data directory')
parser.add_argument('-l', '--logdir', help='Set log directory')
parser.add_argument('-v', '--verbose', action='store_true', help='Set log level to debug')
args = parser.parse_args()
if args.datadir:
gazee.DATA_DIR = args.datadir
gazee.TEMP_DIR = os.path.join(args.datadir, 'tmp')
if not os.path.exists(gazee.DATA_DIR):
os.makedirs(os.path.abspath(gazee.DATA_DIR))
if not os.path.exists(os.path.join(gazee.DATA_DIR, 'sessions')):
os.makedirs(os.path.abspath(os.path.join(gazee.DATA_DIR, 'sessions')))
if not os.path.exists(gazee.TEMP_DIR):
os.makedirs(os.path.abspath(gazee.TEMP_DIR))
from gazee import log
if args.logdir:
gazee.LOG_DIR = args.logdir
gazee.ARGS += ["-l", gazee.LOG_DIR]
else:
gazee.LOG_DIR = gazee.DATA_DIR
if args.verbose:
log.start(gazee.LOG_DIR, True)
gazee.ARGS += ["-v"]
else:
log.start(gazee.LOG_DIR, False)
cherrypy.log.error_log.propagate = True
cherrypy.log.access_log.propagate = False
gazee.db.db_creation()
gazee.config.config_read()
if args.daemon:
if sys.platform == 'win32':
logging.info("Daemonize not supported under Windows, starting normally")
else:
# If the pidfile already exists, Gazee may still be running, so exit
if os.path.exists(gazee.PIDFILE):
sys.exit("PID file '" + gazee.PIDFILE + "' already exists. Exiting.")
# The pidfile is only useful in daemon mode, make sure we can write the file properly
try:
PIDFile(cherrypy.engine, gazee.PIDFILE).subscribe()
except IOError as e:
raise SystemExit("Unable to write PID file: %s [%d]" % (e.strerror, e.errno))
if gazee.DATA_DIR is not 'data':
gazee.ARGS += ["-c", gazee.DATA_DIR]
gazee.ARGS += ["-d"]
Daemonizer(cherrypy.engine).subscribe()
# This verifies the color scheme stays between automated updates as what the user set.
if os.path.exists('public/css/style.css'):
with open('public/css/style.css') as f:
style = f.read()
with open('public/css/style.css', "w") as f:
style = style.replace("757575", gazee.MAIN_COLOR)
style = style.replace("BDBDBD", gazee.ACCENT_COLOR)
style = style.replace("FFFFFF", gazee.WEB_TEXT_COLOR)
f.write(style)
if gazee.DATA_DIR is not 'data':
conf = {
'/': {
'tools.gzip.on': True,
'tools.gzip.mime_types': ['text/*', 'application/*', 'image/*'],
'tools.sessions.on': True,
'tools.sessions.timeout': 1440,
'tools.sessions.storage_class': cherrypy.lib.sessions.FileSession,
'tools.sessions.storage_path': os.path.join(gazee.DATA_DIR, "sessions"),
'tools.auth_basic.on': True,
'tools.auth_basic.realm': 'Gazee',
'tools.auth_basic.checkpassword': gazee.authmech.check_password,
'request.show_tracebacks': False
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd()),
'tools.staticdir.dir': "public"
},
'/data': {
'tools.staticdir.on': True,
'tools.staticdir.dir': gazee.DATA_DIR
},
'/tmp': {
'tools.staticdir.on': True,
'tools.staticdir.dir': gazee.TEMP_DIR
},
'/favicon.ico': {
'tools.staticfile.on': True,
'tools.staticfile.filename': os.path.join(os.getcwd(), "public/images/favicon.ico")
}
}
else:
conf = {
'/': {
'tools.gzip.on': True,
'tools.gzip.mime_types': ['text/*', 'application/*', 'image/*'],
'tools.staticdir.root': os.path.abspath(os.getcwd()),
'tools.sessions.on': True,
'tools.sessions.timeout': 1440,
'tools.sessions.storage_class': cherrypy.lib.sessions.FileSession,
'tools.sessions.storage_path': os.path.join(gazee.DATA_DIR, "sessions"),
'tools.auth_basic.on': True,
'tools.auth_basic.realm': 'Gazee',
'tools.auth_basic.checkpassword': gazee.authmech.check_password,
'request.show_tracebacks': False
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': "public"
},
'/data': {
'tools.staticdir.on': True,
'tools.staticdir.dir': gazee.DATA_DIR
},
'/tmp': {
'tools.staticdir.on': True,
'tools.staticdir.dir': gazee.TEMP_DIR
},
'/favicon.ico': {
'tools.staticfile.on': True,
'tools.staticfile.filename': os.path.join(os.getcwd(), "public/images/favicon.ico")
}
}
if (gazee.SSL_KEY == '') and (gazee.SSL_CERT == ''):
options_dict = {
'server.socket_port': gazee.PORT,
'server.socket_host': '0.0.0.0',
'server.thread_pool': 30,
'log.screen': False,
'engine.autoreload.on': False,
}
else:
options_dict = {
'server.socket_port': gazee.PORT,
'server.socket_host': '0.0.0.0',
'server.thread_pool': 30,
'server.ssl_module': 'builtin',
'server.ssl_certificate': gazee.SSL_CERT,
'server.ssl_private_key': gazee.SSL_KEY,
'log.screen': False,
'engine.autoreload.on': False,
}
cherrypy.config.update(options_dict)
cherrypy.engine.signals.subscribe()
cherrypy.engine.timeout_monitor.unsubscribe()
cherrypy.tree.mount(Gazee(), '/', config=conf)
logging.info("Gazee Started")
cherrypy.engine.start()
print("Gazee has started")
scanner = ComicScanner()
scanner.rescan_db()
cherrypy.engine.block()
if __name__ == '__main__':
main()