Repository navigation
Expand file tree
/
Copy pathdoudou.py
More file actions
executable file
·232 lines (210 loc) · 5.77 KB
/
Copy pathdoudou.py
File metadata and controls
executable file
·232 lines (210 loc) · 5.77 KB
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
#!/usr/bin/python3
import os, os.path, time, json
from whoosh import index
from whoosh import highlight
from whoosh.fields import Schema, TEXT, STORED, ID
from whoosh.qparser import MultifieldParser
from jieba.analyse import ChineseAnalyzer
from http.server import BaseHTTPRequestHandler, HTTPServer
from multiprocessing import Process
import argparse
import pdfminer.settings
pdfminer.settings.STRICT = False
import pdfminer.high_level
index_dir = 'indexdir'
page_len = 15
port = 19986
sleep_secs = 60 * 10
srch_root = '/home/tk/master-tree/incr'
#srch_root = './test'
schema = Schema(
fileid=ID(unique=True),
filepath=TEXT(stored=True, analyzer=ChineseAnalyzer()),
document=TEXT(stored=True, analyzer=ChineseAnalyzer()),
moditime=STORED
)
def files_under(root):
for dirpath, dirnames, filenames in os.walk(root):
for f_name in filenames:
yield os.path.join(dirpath, f_name)
def get_index():
if not os.path.exists(index_dir):
os.mkdir(index_dir)
return index.create_in(index_dir, schema)
else:
return index.open_dir(index_dir)
def reset_index():
print('reset index!')
index.create_in(index_dir, schema)
def index_txt_file(writer, path):
mtime = os.path.getmtime(path)
with open(path, 'rb') as fh:
binary = fh.read()
possible_enc = ['utf-8', 'gb2312']
content = ''
for guess in possible_enc:
is_right = True
try:
content = binary.decode(guess)
except:
is_right = False
if is_right:
break
elif guess == possible_enc[-1]:
print('[err decode]', path)
content = ''
break
writer.add_document(
fileid=path,
filepath=path,
document=content,
moditime=mtime
)
def index_file_name(writer, path):
mtime = os.path.getmtime(path)
writer.add_document(
fileid=path,
filepath=path,
document='',
moditime=mtime
)
def index_pdf_file(writer, path):
with open('tmp.txt', 'w', encoding='utf-8') as out:
with open(path, 'rb') as fh:
try:
params = pdfminer.layout.LAParams()
pdfminer.high_level.extract_text_to_fp(fh, out, laparams=params)
except:
print('[err extract pdf]', path)
content = ''
pass
# now index the text output
mtime = os.path.getmtime(path)
with open('./tmp.txt', encoding='utf-8') as fh:
content = fh.read()
writer.add_document(
fileid=path,
filepath=path,
document=content,
moditime=mtime
)
def incremental_index():
ix = get_index()
writer = ix.writer()
indexed_paths = set()
# scan for file updates or deletions
with ix.searcher() as se:
for fields in se.all_stored_fields():
path = fields['filepath']
if not os.path.exists(path):
writer.delete_by_term('fileid', path)
print('[deletion detected]', path)
else:
indexed_time = fields['moditime']
modified_time = os.path.getmtime(path)
if modified_time > indexed_time:
print('[update detected]', path)
writer.delete_by_term('fileid', path)
else:
print('[unchanged]', path)
indexed_paths.add(path)
# re-index everyfile that is "new" to whoosh
for path in files_under(srch_root):
try:
os.stat(path)
except:
print('[err open]', path)
continue
if path not in indexed_paths:
if "/node_modules/" in path:
continue
elif ".git" in path:
continue
elif "/src/" in path:
continue
elif "/obj/" in path:
continue
ext = path.split('.')[-1].lower()
if ext in ['c', 'cpp', 'dll', 'info', 'h', 'sys', 'bin', 'hex']:
continue
print('[indexing %s] %s' % (ext, path))
if ext == 'pdf':
index_pdf_file(writer, path)
elif ext == 'txt':
index_txt_file(writer, path)
else:
index_file_name(writer, path)
writer.commit()
def indexing_forever():
while True:
incremental_index()
for t in range(sleep_secs, 1, -1):
print('indexing in %s secs\r' % t, end='')
time.sleep(1)
def search(query, page):
ix = index.open_dir(index_dir)
res = dict()
with ix.searcher() as se:
q = MultifieldParser([
"filepath", "document"
], schema=ix.schema).parse(query)
p = se.search_page(q, page, pagelen=page_len)
fg = highlight.ContextFragmenter(
maxchars=200, surround=100, charlimit=100000
)
p.results.fragmenter = fg
res['cur_page'] = p.pagenum
res['tot_page'] = p.pagecount
res['list'] = list()
for hit in p:
t = time.localtime(hit['moditime'])
timestr = time.strftime('%Y-%m-%d %H:%M:%S', t)
res['list'].append({
"path": hit['filepath'],
"time": timestr,
"path_highlight": hit.highlights('filepath'),
"highlight": hit.highlights('document')
})
return res
def doudouHandleReq(req):
print(req)
return json.dumps(search(req['q'], req['p']))
class doudouHandler(BaseHTTPRequestHandler):
def do_POST(self):
content_len = int(self.headers['Content-Length'])
raw_req = self.data_string = self.rfile.read(content_len)
response = doudouHandleReq(json.loads(raw_req.decode('utf-8')))
self.send_response(200)
self.end_headers()
self.wfile.write(response.encode('utf-8'))
def indexd():
try:
print("Runing indexd in background ...")
indexd = Process(target=indexing_forever)
indexd.start()
except KeyboardInterrupt:
indexd.terminate()
def searchd():
try:
print("Listening on port " + str(port))
server = HTTPServer(('', port), doudouHandler)
server.serve_forever()
except KeyboardInterrupt:
print('\n\n')
print('Close the server...')
server.socket.close()
# main #
parser = argparse.ArgumentParser(description='Doudou, a loyal searcher.')
parser.add_argument('--reset', help='clear previous index.', action="store_true")
parser.add_argument('--searchd', help='start Doudou daemon.', action="store_true")
parser.add_argument('--indexd', help='start index daemon.', action="store_true")
parser.add_argument('--query', help='query a string.', type=str)
args = parser.parse_args()
if args.reset:
reset_index()
if args.indexd:
indexd()
if args.searchd:
searchd()
if args.query:
print(search(args.query, 1))