forked from Lexis/LazyLibrarian
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LazyLibrarian.py
149 lines (119 loc) · 5.02 KB
/
LazyLibrarian.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
import os, sys, time, cherrypy, threading, locale
from lib.configobj import ConfigObj
import lazylibrarian
from lazylibrarian import webStart, logger
def main():
# rename this thread
threading.currentThread().name = "MAIN"
# Set paths
if hasattr(sys, 'frozen'):
lazylibrarian.FULL_PATH = os.path.abspath(sys.executable)
else:
lazylibrarian.FULL_PATH = os.path.abspath(__file__)
lazylibrarian.PROG_DIR = os.path.dirname(lazylibrarian.FULL_PATH)
lazylibrarian.ARGS = sys.argv[1:]
lazylibrarian.SYS_ENCODING = None
try:
locale.setlocale(locale.LC_ALL, "")
lazylibrarian.SYS_ENCODING = locale.getpreferredencoding()
except (locale.Error, IOError):
pass
# for OSes that are poorly configured I'll just force UTF-8
if not lazylibrarian.SYS_ENCODING or lazylibrarian.SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
lazylibrarian.SYS_ENCODING = 'UTF-8'
# Set arguments
from optparse import OptionParser
p = OptionParser()
p.add_option('-d', '--daemon', action = "store_true",
dest = 'daemon', 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('--debug', action="store_true",
dest = 'debug', help = "Show debuglog messages")
p.add_option('--nolaunch', action = "store_true",
dest = 'nolaunch', help="Don't start browser")
p.add_option('--port',
dest = 'port', default = None,
help = "Force webinterface to listen on this port")
p.add_option('--datadir',
dest = 'datadir', default = None,
help = "Path to the data directory")
p.add_option('--config',
dest = 'config', default = None,
help = "Path to config.ini file")
p.add_option('-p', '--pidfile',
dest = 'pidfile', default = None,
help = "Store the process id in the given file")
options, args = p.parse_args()
if options.debug:
lazylibrarian.LOGLEVEL = 2
if options.quiet:
lazylibrarian.LOGLEVEL = 0
if options.daemon:
if not sys.platform == 'win32':
lazylibrarian.DAEMON = True
lazylibrarian.LOGLEVEL = 0
lazylibrarian.daemonize()
else:
print "Daemonize not supported under Windows, starting normally"
if options.nolaunch:
lazylibrarian.LAUNCH_BROWSER = False
if options.datadir:
lazylibrarian.DATADIR = str(options.datadir)
else:
lazylibrarian.DATADIR = lazylibrarian.PROG_DIR
if options.config:
lazylibrarian.CONFIGFILE = str(options.config)
else:
lazylibrarian.CONFIGFILE = os.path.join(lazylibrarian.DATADIR, "config.ini")
if options.pidfile:
if lazylibrarian.DAEMON:
lazylibrarian.PIDFILE = str(options.pidfile)
# create and check (optional) paths
if not os.path.exists(lazylibrarian.DATADIR):
try:
os.makedirs(lazylibrarian.DATADIR)
except OSError:
raise SystemExit('Could not create data directory: ' + lazylibrarian.DATADIR + '. Exit ...')
if not os.access(lazylibrarian.DATADIR, os.W_OK):
raise SystemExit('Cannot write to the data directory: ' + lazylibrarian.DATADIR + '. Exit ...')
# create database and config
lazylibrarian.DBFILE = os.path.join(lazylibrarian.DATADIR, 'lazylibrarian.db')
lazylibrarian.CFG = ConfigObj(lazylibrarian.CONFIGFILE, encoding='utf-8')
lazylibrarian.initialize()
if options.port:
HTTP_PORT = int(options.port)
logger.info('Starting LazyLibrarian on forced port: %s' % HTTP_PORT)
else:
HTTP_PORT = int(lazylibrarian.HTTP_PORT)
logger.info('Starting LazyLibrarian on port: %s' % lazylibrarian.HTTP_PORT)
if lazylibrarian.DAEMON:
lazylibrarian.daemonize()
# Try to start the server.
webStart.initialize({
'http_port': HTTP_PORT,
'http_host': lazylibrarian.HTTP_HOST,
'http_root': lazylibrarian.HTTP_ROOT,
'http_user': lazylibrarian.HTTP_USER,
'http_pass': lazylibrarian.HTTP_PASS,
})
if lazylibrarian.LAUNCH_BROWSER and not options.nolaunch:
lazylibrarian.launch_browser(lazylibrarian.HTTP_HOST, lazylibrarian.HTTP_PORT, lazylibrarian.HTTP_ROOT)
lazylibrarian.start()
while True:
if not lazylibrarian.SIGNAL:
try:
time.sleep(1)
except KeyboardInterrupt:
lazylibrarian.shutdown()
else:
if lazylibrarian.SIGNAL == 'shutdown':
lazylibrarian.shutdown()
elif lazylibrarian.SIGNAL == 'restart':
lazylibrarian.shutdown(restart=True)
else:
lazylibrarian.shutdown(restart=True, update=True)
lazylibrarian.SIGNAL = None
return
if __name__ == "__main__":
main()