def snugglefish_search(indexes, search, user): """Execute search of selected index with the given string.""" # Return a dictionary where the key is the index name and the # value a dictionary with status and a list of potential matches. ret = {} # If there are no sources, return early. sources = user_sources(user) if not sources: return ret for idx in indexes: ret[idx] = {'success': True, 'reason': '', 'files': []} sngindex = SnuggleIndex.objects(name=idx).first() if not sngindex: ret[idx]['reason'] = "Index not found in database." ret[idx]['success'] = False continue snuggle = pysnugglefish.init(str(sngindex.directory + "/" + idx)) try: tmp = snuggle.search(search) for res in tmp: if Sample.objects(md5=res, source__name__in=sources).count() > 0: ret[idx]['files'].append(res) except Exception, e: ret[idx]['reason'] = "Error: %s" % e ret[idx]['success'] = False
def snugglefish_search(indexes, search, user): """Execute search of selected index with the given string.""" # Return a dictionary where the key is the index name and the # value a dictionary with status and a list of potential matches. ret = {} # If there are no sources, return early. sources = user_sources(user) if not sources: return ret for idx in indexes: ret[idx] = { 'success': True, 'reason': '', 'files': [] } sngindex = SnuggleIndex.objects(name=idx).first() if not sngindex: ret[idx]['reason'] = "Index not found in database." ret[idx]['success'] = False continue snuggle = pysnugglefish.init(str(sngindex.directory + "/" + idx)) try: tmp = snuggle.search(search) for res in tmp: if Sample.objects(md5=res, source__name__in=sources).count() > 0: ret[idx]['files'].append(res) except Exception, e: ret[idx]['reason'] = "Error: %s" % e ret[idx]['success'] = False
def __create_index(self, name, query, directory): # Note: I am intentionally not checking to make sure # the directory exists. It is up to the indexer to put # the result in the appropriate place. sngindex = SnuggleIndex() sngindex.name = name sngindex.query = query sngindex.directory = directory sngindex.save() return sngindex
def run(self, argv): parser = OptionParser() parser.add_option("-a", "--action", action="store", dest="action", type="string", help="action") parser.add_option("-n", "--name", action="store", dest="name", type="string", help="index name") parser.add_option("-c", "--count", action="store", dest="count", type="string", help="count") parser.add_option("-q", "--query", action="store", dest="query", type="string", help="query filter") parser.add_option("-d", "--directory", action="store", dest="directory", type="string", help="index directory") (opts, args) = parser.parse_args(argv) if opts.action.lower() not in ['create', 'getnew', 'status', 'delete', 'update']: print "Action must be one of create, getnew, status, delete or update." return if not opts.name: print "Need an index name." return if opts.action == 'create': if not opts.query or not opts.directory: print "Need a query filter and directory." return # We don't store the parsed query as it needs to be a string but # we can make sure it is parsable before storing it. try: query = ast.literal_eval(opts.query) except Exception, e: print "Error with query: %s" % e return sngindex = SnuggleIndex.objects(name=opts.name).first() if sngindex: print "Index already exists." return self.__create_index(opts.name, opts.query, opts.directory)
def snugglefish_status(): # Return a list of dictionaries, one per index. Filter out # the query as it can contain sensitive information like source names. # Also filter out the directory as it is not important for this. # # We do not filter out the index name though, so be careful not to put # sensitive names as your index name. ret = [] sngindexes = SnuggleIndex.objects() if not sngindexes: return ret for sngindex in sngindexes: tmp = sngindex.to_dict() del (tmp['_id']) del (tmp['query']) del (tmp['directory']) try: tmp['percent'] = (float(sngindex.count) / sngindex.total) * 100 except: tmp['percent'] = 0 ret.append(tmp) return ret
def snugglefish_status(): # Return a list of dictionaries, one per index. Filter out # the query as it can contain sensitive information like source names. # Also filter out the directory as it is not important for this. # # We do not filter out the index name though, so be careful not to put # sensitive names as your index name. ret = [] sngindexes = SnuggleIndex.objects() if not sngindexes: return ret for sngindex in sngindexes: tmp = sngindex.to_dict() del(tmp['_id']) del(tmp['query']) del(tmp['directory']) try: tmp['percent'] = (float(sngindex.count)/sngindex.total) * 100 except: tmp['percent'] = 0 ret.append(tmp) return ret
return self.__create_index(opts.name, opts.query, opts.directory) elif opts.action == 'getnew': query = None if not opts.count: print "Need a count." return try: count = int(opts.count) except Exception, e: print "Error with count: %s" % e return sngindex = SnuggleIndex.objects(name=opts.name).first() if not sngindex: if not opts.query or not opts.directory: print "Index does not exist, provide a query and directory." return else: try: query = ast.literal_eval(opts.query) except Exception, e: print "Error with query: %s" % e return sngindex = self.__create_index(opts.name, opts.query, opts.directory) if not query:
def run(self, argv): parser = OptionParser() parser.add_option("-a", "--action", action="store", dest="action", type="string", help="action") parser.add_option("-n", "--name", action="store", dest="name", type="string", help="index name") parser.add_option("-c", "--count", action="store", dest="count", type="string", help="count") parser.add_option("-q", "--query", action="store", dest="query", type="string", help="query filter") parser.add_option("-d", "--directory", action="store", dest="directory", type="string", help="index directory") (opts, args) = parser.parse_args(argv) if opts.action.lower() not in [ 'create', 'getnew', 'status', 'delete', 'update' ]: print "Action must be one of create, getnew, status, delete or update." return if not opts.name: print "Need an index name." return if opts.action == 'create': if not opts.query or not opts.directory: print "Need a query filter and directory." return # We don't store the parsed query as it needs to be a string but # we can make sure it is parsable before storing it. try: query = ast.literal_eval(opts.query) except Exception, e: print "Error with query: %s" % e return sngindex = SnuggleIndex.objects(name=opts.name).first() if sngindex: print "Index already exists." return self.__create_index(opts.name, opts.query, opts.directory)