/
dirindexer.py
541 lines (465 loc) · 19.4 KB
/
dirindexer.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#usr/bin/env python
from whoosh.index import create_in
from whoosh.index import open_dir
from whoosh.fields import Schema, TEXT, ID, STORED
from whoosh.analysis import SpaceSeparatedTokenizer, LowercaseFilter
from whoosh.writing import BufferedWriter
from whoosh import highlight
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import watchdog.utils
import os
import argparse
import codecs
import multiprocessing
import threading
import colorama
import time
import Queue
class DirIndexer:
"""
Pure Python command line utility for efficiently indexing then searching
a directory
"""
def __init__(self, args):
"""
Initialization function
Parameters
----------
args: The namespace created by argparse containing several options
for each of dirindexer's modes
index, update, daemon:
directory : str
The directory to process.
exclude : list of str, optional
List of filetypes to exclude from the process.
include : list of str, optional
List of filetypes to only include in the process.
processors : int, optional
Number of processor cores to use in the process,
defaults to 1/2 of cores available
all : bool, default=false, optional
Whether to include hidden files and directories in
the process
daemon(continued):
delay : float
Amount of time to wait in between each index commit.
search:
keyword : str
The keyword to search for
color : {'always', 'auto'}, optional
Whether or not to colorize search results. Defaults
to auto unless 'always' or something else is set.
In auto mode, text will always be highlighted
unless output is being piped
limit : int, optional
The number of terms to display. Default is 0, or
no limit
exclude : list of str, optional
List of filetypes to exclude from the search
include : list of str, optional
List of filetypes to only include in search
"""
if(args.func == DirIndexer.index
or args.func == DirIndexer.update
or args.func == DirIndexer.daemon
):
self.directory = args.directory
self.exclude = args.exclude
self.include = args.include
self.processors = args.processors
self.all = args.all
if args.func == DirIndexer.daemon:
if args.delay is not None:
self.delay = args.delay
else:
self.delay = 5.0
if args.func == DirIndexer.search:
self.keyword = args.keyword
self.color = args.color
self.limit = args.limit
self.exclude = args.exclude
self.include = args.include
if args.func == DirIndexer.clear:
pass
def get_ix(self):
"""Creates the Schema and returns the index_writer"""
schema = Schema(title=TEXT(stored=True),
path=ID(stored=True, unique=True),
content=TEXT(analyzer=
SpaceSeparatedTokenizer() |
LowercaseFilter()),
date=STORED)
#create if not exists
if not os.path.exists(".indexdir"):
os.mkdir(".indexdir")
create_in(".indexdir", schema)
ix = open_dir(".indexdir")
return ix
def get_cores(self):
"""Returns the appropriate number of processor cores to use"""
cores = multiprocessing.cpu_count() / 2
if self.processors:
cores = self.processors
return cores
def scan_directory(self, dir_nm, writer, checknew=False,
to_index=set(), indexed_paths=set()):
"""
Scans a directory, adds files to be processed to the index
writer, then returns the number of changes
Does not commit changes
Parameters
----------
dir_nm : str
The directory to scan
writer : whoosh.writing.IndexWriter
The whoosh index writer object to use
checknew : bool, optional
Whether to only add new files or files from new folders
to the index
to_index : set, str, optional
For use with checknew
A list of new directories to index
indexed_paths : set, str, optional
For use with checknew
A list of directories that have already been searched
Returns
-------
x : int
Number of files that have been added to the index
"""
x = 0
print dir_nm
for root, sub_folders, files in os.walk(dir_nm, followlinks=True):
#Remove hidden files
if not self.all:
files = [f for f in files if not f[0] == '.']
#os.walk will not process deleted directories
sub_folders[:] = [d for d in sub_folders if not d[0] == '.']
#Remove excluded files
if self.exclude:
files = [f for f in files
if not os.path.splitext(f)[1][1:]
in self.exclude]
if self.include:
files = [f for f in files
if os.path.splitext(f)[1][1:]
in self.include]
for cur_file in files:
path = unicode(os.path.join(root, cur_file))
#If a file is in the queue or is new, add it
if checknew and path in to_index or path not in indexed_paths:
self.add_doc(writer, path)
x += 1
elif not checknew:
self.add_doc(writer, path)
x += 1
return x
def index(self):
"""
Index function
Adds self.directory to the index.
"""
ix = self.get_ix()
#get the number of cores to use
procs = self.get_cores()
writer = ix.writer(procs=procs)
dir_nm = unicode(self.directory)
#recursively scan the directory and add files
x = self.scan_directory(dir_nm, writer)
print "Writing %d files to index" % x
writer.commit()
ix.close()
def update(self):
"""
Update function
Checks self.directory for changes and adds them to the index
"""
try:
ix = self.get_ix()
#get the number of cores to use
procs = self.get_cores()
writer = ix.writer(procs=procs)
indexed_paths = set() # Holds all paths already indexed
to_index = set() # Holds a list of paths to index later
x = 0
with ix.searcher() as searcher:
for fields in searcher.all_stored_fields():
indexed_path = fields['path']
indexed_paths.add(indexed_path)
#if the file has been deleted, remove it from the index
if not os.path.exists(indexed_path):
self.remove_doc(writer, indexed_path)
else:
indexed_time = fields['date']
mtime = os.path.getmtime(indexed_path)
#If file has been changed,
#delete it and add it to the queue
if mtime > indexed_time:
self.remove_doc(writer, indexed_path)
to_index.add(indexed_path)
x = self.scan_directory(self.directory, writer,
True, to_index, indexed_paths)
finally:
print "Writing " + str(x) + " files to index."
writer.commit()
ix.close()
def daemon(self):
"""
Daemon function
Continuosly watches self.directory for
changes and adds them to the index.
"""
ix = self.get_ix()
writer = BufferedWriter(ix, limit=100)
event_handler = IndexWriterEventHandler(writer, self, self.all,
self.exclude, self.include,
)
observer = Observer()
observer.schedule(event_handler, path=self.directory, recursive=True)
observer.start()
#observer.should_keep_running()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
writer.commit()
ix.close()
observer.join()
def add_doc(self, writer, path):
"""Writes a given file to the index_writer"""
cur_file = codecs.open(path, encoding='utf-8', errors='ignore')
content = cur_file.read()
file_name = unicode(cur_file.name)
path = unicode(path)
modtime = os.path.getmtime(path)
print "Indexing %s" % file_name
writer.add_document(title=file_name, path=path,
content=content, date=modtime)
def remove_doc(self, writer, path):
"""Removes a given file from index_writer"""
print "Removing %s" % path
writer.delete_by_term('path', path)
def search(self):
"""
Search function
Searches all indexes for self.keyword and prints them.
"""
try:
ix = self.get_ix()
search_term = unicode(self.keyword)
from whoosh.qparser import QueryParser
with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse(
u"%s" % search_term)
results = searcher.search(query, terms=True, limit=self.limit)
results.fragmenter = highlight.ContextFragmenter(maxchars=200,
surround=20)
#If stdin == stdout, the programs output is not being
#piped and colored output is fine
color = (self.color == 'always'
or (self.color == 'auto'
and os.fstat(0) == os.fstat(1)))
results.formatter = ColorFormatter(color=color)
#Remove excluded filetypes from search results
if self.exclude:
results = [f for f in results
if not os.path.splitext(f["path"])[1][1:]
in self.exclude]
if self.include:
results = [f for f in results
if os.path.splitext(f["path"])[1][1:]
in self.include]
print results
for i, result in enumerate(results, start=1):
if color:
print "Result %i: %s" % (
i, colorama.Fore.GREEN + result["path"]
+ colorama.Fore.RESET)
else:
print "Result %i: %s" % (i, result["path"])
with codecs.open(result["path"],
encoding='utf-8',
errors='ignore') as f:
file_content = f.read()
print result.highlights("content",
text=file_content,
top=10)
print "\n"
finally:
ix.close()
def clear(self):
"""Deletes all indexes"""
print "Deleting the current index..."
for root, dirs, files in os.walk(os.getcwd() + "/.indexdir/",
topdown=False):
for name in files:
print name
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(os.getcwd() + "/.indexdir/")
class ColorFormatter(highlight.Formatter):
"""Class that handles colorized search output"""
def __init__(self, between="\n", color=True):
self.between = between
self.color = color
def format_token(self, text, token, replace=False):
tokentext = highlight.get_text(text, token, False)
if self.color:
return colorama.Back.YELLOW + tokentext + colorama.Back.RESET
else:
return tokentext
class IndexWriterEventHandler(FileSystemEventHandler):
"""
Event handler customized for updating a directory with file changes.
Implements a LIFO queue to eliminate redundant commits.
"""
def __init__(self, writer, di, all=False,
exclude=[], include=None, delay=5.0):
self.writer = writer
self.di = di
self.all = all
self.exclude = exclude
self.include = include
self.delay = delay
self.queue = Queue.LifoQueue()
self.clear_queue()
def dispatch(self, event):
print("Adding event to queue...")
self.on_any_event(event)
self.queue.put(event)
def clear_queue(self):
"""Commits queue to index and clears it."""
print("Emtpying queue...")
urls = []
while not self.queue.empty():
event = self.queue.get()
if watchdog.utils.has_attribute(event, 'dest_path'):
if event.dest_path not in urls:
urls.append(event.dest_path)
FileSystemEventHandler.dispatch(self, event)
else:
if event.src_path not in urls:
urls.append(event.src_path)
FileSystemEventHandler.dispatch(self, event)
if urls != []:
print("Commiting %i changes." % len(urls))
self.writer.commit()
print("Done.")
threading.Timer(self.delay, self.clear_queue).start()
def pathIsGood(self, path):
"""
Analyzes path according to rules in self.all, self.include,
and self.exclude and returns whether the path passes.
"""
if not self.all:
if os.path.basename(path)[0] == '.':
return False
if self.exclude:
if os.path.splitext(path)[1][1:] in self.exclude:
return False
if self.include:
if os.path.splitext(path)[1][1:] not in self.include:
return False
return True
def on_created(self, event):
#print("on_create")
if self.pathIsGood(event.src_path):
self.di.add_doc(self.writer, event.src_path)
def on_moved(self, event):
#print("on_moved")
self.di.remove_doc(self.writer, event.src_path)
if self.pathIsGood(event.dest_path):
self.di.add_doc(self.writer, event.dest_path)
def on_deleted(self, event):
#print("on_deleted")
self.di.remove_doc(self.writer, event.src_path)
def on_modified(self, event):
#print("on_modified")
if self.pathIsGood(event.src_path):
self.di.remove_doc(self.writer, event.src_path)
self.di.add_doc(self.writer, event.src_path)
def start():
colorama.init()
parser = argparse.ArgumentParser(
description='index a directory or search for keywords.')
subparsers = parser.add_subparsers()
parser_index = subparsers.add_parser(
'index', help="Index a given directory for future searches")
parser_index.add_argument('directory', help="the directory to search")
parser_index.set_defaults(func=DirIndexer.index)
parser_index_filegroup = parser_index.add_mutually_exclusive_group()
parser_index_filegroup.add_argument(
"-x", "--exclude", nargs='+',
help="Exclude specified filetypes from index")
parser_index_filegroup.add_argument(
"-i", "--include", nargs='+',
help="Include only the specified filetypes in the index")
parser_index.add_argument("-p", "--processors", type=int,
help="Number of processors to utilize")
parser_index.add_argument("-a", "--all", action='store_true',
help="Include hidden files and folders in index")
parser_update = subparsers.add_parser(
'update', help="Update the index with new or edited files")
parser_update.add_argument('directory', help="The directory to update")
parser_update.set_defaults(func=DirIndexer.update)
parser_update_filegroup = parser_update.add_mutually_exclusive_group()
parser_update_filegroup.add_argument(
"-x", "--exclude", nargs='+',
help="Exclude specified filetypes from update")
parser_update_filegroup.add_argument(
"-i", "--include", nargs='+',
help="Include only the specified filetypes in the update")
parser_update.add_argument(
"-p", "--processors", type=int, help="Number of processors to utilize")
parser_update.add_argument(
"-a", "--all", action='store_true',
help="Include hidden files and folders in update")
parser_daemon = subparsers.add_parser(
'daemon', help="Start a daemon to automatically update the index.")
parser_daemon.add_argument('directory', help="The directory to watch.")
parser_daemon.set_defaults(func=DirIndexer.daemon)
parser_daemon_filegroup = parser_daemon.add_mutually_exclusive_group()
parser_daemon_filegroup.add_argument(
"-x", "--exclude", nargs='+',
help="Exclude the specified filetypes from the updates.")
parser_daemon_filegroup.add_argument(
"-i", "--include", nargs='+',
help="Include only the specified filetypes in the updates.")
parser_daemon.add_argument(
"-p", "--processors", type=int, help="Number of processors to utilize")
parser_daemon.add_argument(
"-a", "--all", action='store_true',
help="Include hidden files and directories in the update.")
parser_daemon.add_argument(
"-d", "--delay", type=float,
help="Delay in between commits")
parser_search = subparsers.add_parser(
'search', help="Search the indexed directory for a keyword")
parser_search.add_argument('keyword', help="the search term")
parser_search.set_defaults(func=DirIndexer.search)
parser_search.add_argument(
'-c', '--color', choices=['auto', 'always', 'never'],
default='auto', type=str,
help="Highlight the search term")
parser_search.add_argument(
'-l', '--limit', type=int,
default=None,
help='Number of results to show; passing None will show all')
parser_search_filegroup = parser_search.add_mutually_exclusive_group()
parser_search_filegroup.add_argument(
'-x', "--exclude", nargs='+',
help="Exclude specified filetypes from search")
parser_search_filegroup.add_argument(
'-i', "--include", nargs='+',
help="Include only the specified filetypes in the search")
parser_clear = subparsers.add_parser(
"clear", help="Delete the current index.")
parser_clear.set_defaults(func=DirIndexer.clear)
args = parser.parse_args()
di = DirIndexer(args)
args.func(di)
if __name__ == '__main__':
start()