Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 47 lines (36 sloc) 1.26 kB
f91a5c0 @Rafiot Add wrapper around redis_search
authored
1 #!/usr/bin/python
2
3 import subprocess
4 import os
5
6 from redis_search import ScoredIndexSearch
7
31c4385 @Rafiot improve strings generator
authored
8 strings_command = "/usr/bin/strings"
9
f91a5c0 @Rafiot Add wrapper around redis_search
authored
10 class StringsGenerator(object):
11
12 def __init__(self, directory):
13 self.index = ScoredIndexSearch()
14 self.directory = directory
15
16 def strings(self, f):
17 strings = ''
18 nul_f = open(os.devnull, 'w')
31c4385 @Rafiot improve strings generator
authored
19 p = subprocess.Popen([strings_command, f], stdout=subprocess.PIPE, stderr = nul_f)
f91a5c0 @Rafiot Add wrapper around redis_search
authored
20 for s in p.stdout:
21 strings += s + " "
22 nul_f.close()
31c4385 @Rafiot improve strings generator
authored
23 self.index.add_indexed_item(f, strings)
f91a5c0 @Rafiot Add wrapper around redis_search
authored
24
25 def dir_strings(self):
26 for r,d,files in os.walk(self.directory):
27 for f in files:
31c4385 @Rafiot improve strings generator
authored
28 print f
29 self.strings(os.path.join(r,f))
f91a5c0 @Rafiot Add wrapper around redis_search
authored
30
31 def search(self, string):
32 return self.index.search(string)
33
34
35 if __name__ == '__main__':
31c4385 @Rafiot improve strings generator
authored
36 import sys
37 if len(sys.argv) < 2:
38 sys.exit("Directory needed.")
39 sg = StringsGenerator(sys.argv[1])
f91a5c0 @Rafiot Add wrapper around redis_search
authored
40 response = raw_input("Generate database? (y/N) ")
41 if response == 'y':
42 sg.dir_strings()
31c4385 @Rafiot improve strings generator
authored
43 print("Database generated.")
44 else:
45 print("We assume that the database has already been generated.")
f91a5c0 @Rafiot Add wrapper around redis_search
authored
46 print("Search in the database using sg.search('query')")
Something went wrong with that request. Please try again.