-
Notifications
You must be signed in to change notification settings - Fork 16
/
thawab-server
executable file
·269 lines (234 loc) · 6.59 KB
/
thawab-server
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#! /usr/bin/python3
# -*- coding: UTF-8 -*-
import sys, os, time, atexit, signal, shutil, tempfile, sqlite3
from Thawab.gtkUi import launchServer, onlyterminal
from Thawab.shamelaUtils import ShamelaSqlite, shamelaImport
class ThawabServer:
def __init__(self, pidfile):
self.pidfile = pidfile
def tprint(self, message, noend=False):
if not noend:
sys.stderr.write(message+"\n")
else:
sys.stderr.write(message+"\r")
def daemonize(self):
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError as err:
self.tprint('fork #1 failed: {0}'.format(err))
sys.exit(1)
# decouple from parent environment
#os.chdir('/')
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError as err:
self.tprint('fork #2 failed: {0}'.format(err))
sys.exit(1)
# redirect standard file descriptors
#sys.stdout.flush()
#sys.stderr.flush()
#si = open(os.devnull, 'r')
#so = open(os.devnull, 'a+')
#se = open(os.devnull, 'a+')
#os.dup2(si.fileno(), sys.stdin.fileno())
#os.dup2(so.fileno(), sys.stdout.fileno())
#os.dup2(se.fileno(), sys.stderr.fileno())
# write pidfile
atexit.register(self.delpid)
pid = str(os.getpid())
with open(self.pidfile,'w+') as f:
f.write(pid + '\n')
def delpid(self):
os.remove(self.pidfile)
def check_running(self):
# Check for a pidfile to see if the daemon already runs
try:
with open(self.pidfile,'r') as pf:
r = int(pf.read().strip())
c = os.system("ps o cmd= {} > /dev/null".format(r))
#print ("ps o cmd= {}".format(r),c)
if not c:
return r
return None
except IOError:
return None
def start(self):
"""Start the daemon."""
if self.check_running():
message = "pidfile {0} already exist. " + \
"Server is already running?\n"
self.tprint(message.format(self.pidfile))
sys.exit(1)
# Start the daemon
self.daemonize()
print("** Thawab server is running on: 127.0.0.1:18080")
self.run()
def stop(self):
"""Stop the daemon."""
# Get the pid from the pidfile
pid = self.check_running()
if not pid:
message = "pidfile {0} does not exist. " + \
"Server is not running?\n"
self.tprint(message.format(self.pidfile))
return # not an error in a restart
# Try killing the daemon process
try:
while 1:
os.kill(pid, signal.SIGTERM)
time.sleep(0.1)
except OSError as err:
e = str(err.args)
if e.find("No such process") > 0:
if os.path.exists(self.pidfile):
os.remove(self.pidfile)
else:
print (str(err.args))
sys.exit(1)
print("** Thawab server stopped")
def restart(self):
"""Restart the daemon."""
if self.check_running():
self.stop()
self.start()
def run(self, silent=False):
self.th, self.port, self.server = onlyterminal()
if not silent:
self.server.serve_forever()
def clean_run(self):
if self.check_running():
self.tprint("Stopping the running server")
self.stop()
self.run(True)
def test(self):
self.tprint("server started successfully")
def reindex(self):
self.clean_run()
self.th.asyncIndexer.queueIndexNew()
if not self.th.asyncIndexer.started:
self.th.asyncIndexer.start()
jj = j = self.th.asyncIndexer.jobs()
while (j > 0 ):
self.tprint("Indexing ... (%d left)" % j,True)
j = self.th.asyncIndexer.jobs()
self.tprint("No indexing jobs left")
if j <= 0 and jj > 0:
self.tprint("Indexing %d jobs, Done" % jj)
self.server.server_close()
def remove_index(self):
self.clean_run()
self.tprint("You will need to recreate search index in-order to search again.")
p = os.path.join(self.th.prefixes[0], 'index')
try:
shutil.rmtree(p)
except OSError:
self.tprint("unable to remove folder [%s]" % p)
else:
self.tprint("Done")
self.server.server_close()
def remove_meta(self):
self.clean_run()
p = os.path.join(self.th.prefixes[0], 'cache', 'meta.db')
try:
os.unlink(p)
except OSError:
self.tprint("unable to remove file [%s]" % p)
else:
self.th.reconstructMetaIndexedFlags()
self.tprint("Done")
def progress_cb(self, msg, p, *d, **kw):
self.tprint(" ** progress: [%g%% completed] %s" % (p, msg))
def importbok(self, bok):
self.clean_run()
fh, db_fn = tempfile.mkstemp(suffix = '.sqlite', prefix = 'th_shamela_tmp')
f = open(db_fn, "w")
f.truncate(0)
f.close()
cn = sqlite3.connect(db_fn, isolation_level = None)
try:
sh = ShamelaSqlite(bok,cn,0,0, self.progress_cb)
except TypeError:
self.tprint("not a shamela file")
self.server.server_close()
return
except OSError:
self.tprint("mdbtools is not installed")
self.server.server_close()
return
if not sh.toSqlite():
self.server.server_close()
return
ids = sh.getBookIds()
for j, bkid in enumerate(ids):
ki = self.th.mktemp()
c = ki.seek(-1,-1)
m = shamelaImport(c,
sh,
bkid)
c.flush()
t_fn = os.path.join(self.th.prefixes[0],
'db',
u"".join((m['kitab'] + \
u"-" + \
m['version'] + \
u'.ki',)))
try:
shutil.move(ki.uri, t_fn)
except OSError:
self.tprint("unable to move converted file.") # windows can't move an opened file
if db_fn and os.path.exists(db_fn):
try:
os.unlink(db_fn)
except OSError:
pass
self.th.loadMeta()
self.tprint("Done")
self.server.server_close()
if __name__ == "__main__":
daemon = ThawabServer('/tmp/thawab-server.pid')
if len(sys.argv) >= 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
elif 'reindex' == sys.argv[1]:
daemon.reindex()
elif 'check' == sys.argv[1]:
r = daemon.check_running()
if r:
print("** Thawab server is running on: 127.0.0.1:18080, pid: {}.".format(r))
else:
print("** Thawab server is not running.")
elif 'fix' == sys.argv[1]:
if sys.argv[2] == 'index':
daemon.remove_index()
elif sys.argv[2] == 'meta':
daemon.remove_meta()
elif 'importbok' == sys.argv[1] and len(sys.argv) >= 3:
daemon.importbok(sys.argv[2])
else:
print ("Unknown command")
sys.exit(2)
sys.exit(0)
else:
print ('''Thawab Server\nusage: thawab-server [command] [file(s)] \nCommands:
start starts the server
stop stops the server
restart restarts the server
check check server status
reindex queues new books
fix index removes search index
fix meta removes meta data cache to generate a fresh one
importbok [file path] imports Shamela .bok file''')
sys.exit(2)