File: thawab-server

package info (click to toggle)
thawab 4.1-2
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye, sid
  • size: 1,912 kB
  • sloc: python: 4,041; makefile: 134; sh: 33
file content (253 lines) | stat: -rwxr-xr-x 6,206 bytes parent folder | download
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
#! /usr/bin/python
# -*- coding: UTF-8 -*-
import sys, os, time, atexit, signal, shutil, tempfile, sqlite3
sys.path.append("/usr/share/thawab")
from Thawab.gtkUi import launchServer
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:

				return int(pf.read().strip())
		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()
		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)

	def restart(self):
		"""Restart the daemon."""
		self.stop()
		self.start()

	def run(self, silent=False):
		self.th, self.port, self.server = launchServer()
		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',
				"".join((m['kitab'] + \
				"-" + \
				m['version'] + \
				'.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 '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 
	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)