SolarFM/plugins/searcher/utils/search.py

146 lines
4.8 KiB
Python
Raw Normal View History

2022-10-03 05:52:50 +00:00
#!/usr/bin/python3
# Python imports
2022-10-07 01:48:44 +00:00
import os, traceback, argparse, threading, json, base64, time, pickle
2022-10-03 05:52:50 +00:00
from setproctitle import setproctitle
2022-10-04 01:50:38 +00:00
from multiprocessing.connection import Client
2022-10-03 05:52:50 +00:00
# Lib imports
# Application imports
2022-10-04 01:50:38 +00:00
_ipc_address = f'/tmp/solarfm-search_grep-ipc.sock'
_ipc_authkey = b'' + bytes(f'solarfm-search_grep-ipc', 'utf-8')
2022-10-03 05:52:50 +00:00
filter = (".cpp", ".css", ".c", ".go", ".html", ".htm", ".java", ".js", ".json", ".lua", ".md", ".py", ".rs", ".toml", ".xml", ".pom") + \
(".txt", ".text", ".sh", ".cfg", ".conf", ".log")
2022-10-03 05:52:50 +00:00
# NOTE: Threads WILL NOT die with parent's destruction.
def threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=False).start()
return wrapper
# NOTE: Threads WILL die with parent's destruction.
def daemon_threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
return wrapper
2022-10-03 05:52:50 +00:00
2022-10-04 01:50:38 +00:00
def send_ipc_message(message) -> None:
conn = Client(address=_ipc_address, family="AF_UNIX", authkey=_ipc_authkey)
conn.send(message)
conn.close()
2022-10-05 03:58:27 +00:00
2022-10-07 01:48:44 +00:00
# NOTE: Kinda important as this prevents overloading the UI thread
time.sleep(0.05)
2022-10-04 01:50:38 +00:00
def file_search(path, query):
2022-10-03 05:52:50 +00:00
try:
2022-10-07 01:48:44 +00:00
for _path, _dir, _files in os.walk(path, topdown = True):
for file in _files:
if query in file.lower():
target = os.path.join(_path, file)
data = f"SEARCH|{json.dumps([target, file])}"
send_ipc_message(data)
2022-10-03 05:52:50 +00:00
except Exception as e:
print("Couldn't traverse to path. Might be permissions related...")
traceback.print_exc()
def _search_for_string(file, query):
2022-10-04 01:50:38 +00:00
b64_file = base64.urlsafe_b64encode(file.encode('utf-8')).decode('utf-8')
grep_result_set = {}
padding = 15
with open(file, 'rb') as fp:
2022-10-07 01:48:44 +00:00
# NOTE: I know there's an issue if there's a very large file with content
# all on one line will lower and dupe it. And, yes, it will only
# return one instance from the file.
try:
for i, raw in enumerate(fp):
line = None
llower = raw.lower()
if not query in llower:
continue
if len(raw) > 72:
start = 0
end = len(raw) - 1
index = llower.index(query)
sindex = llower.index(query) - 15 if index >= 15 else abs(start - index) - index
eindex = sindex + 15 if end > (index + 15) else abs(index - end) + index
line = raw[sindex:eindex]
else:
line = raw
b64_line = base64.urlsafe_b64encode(line).decode('utf-8')
if f"{b64_file}" in grep_result_set.keys():
grep_result_set[f"{b64_file}"][f"{i+1}"] = b64_line
else:
grep_result_set[f"{b64_file}"] = {}
grep_result_set[f"{b64_file}"] = {f"{i+1}": b64_line}
except Exception as e:
...
try:
data = f"GREP|{json.dumps(grep_result_set)}"
send_ipc_message(data)
except Exception as e:
...
2022-10-04 01:50:38 +00:00
2022-10-03 05:52:50 +00:00
2022-10-07 01:48:44 +00:00
@daemon_threaded
def _search_for_string_threaded(file, query):
_search_for_string(file, query)
2022-10-03 05:52:50 +00:00
def grep_search(path, query):
try:
for file in os.listdir(path):
target = os.path.join(path, file)
if os.path.isdir(target):
grep_search(target, query)
else:
if target.lower().endswith(filter):
size = os.path.getsize(target)
2022-10-07 01:48:44 +00:00
if not size > 5000:
2022-10-03 05:52:50 +00:00
_search_for_string(target, query)
else:
_search_for_string_threaded(target, query)
2022-10-03 05:52:50 +00:00
except Exception as e:
print("Couldn't traverse to path. Might be permissions related...")
traceback.print_exc()
def search(args):
if args.type == "file_search":
file_search(args.dir, args.query.lower())
2022-10-03 05:52:50 +00:00
if args.type == "grep_search":
grep_search(args.dir, args.query.lower().encode("utf-8"))
2022-10-03 05:52:50 +00:00
if __name__ == "__main__":
try:
setproctitle('SolarFM: File Search - Grepy')
parser = argparse.ArgumentParser()
# Add long and short arguments
parser.add_argument("--type", "-t", default=None, help="Type of search to do.")
parser.add_argument("--dir", "-d", default=None, help="Directory root for search type.")
parser.add_argument("--query", "-q", default=None, help="Query search is working against.")
# Read arguments (If any...)
args = parser.parse_args()
search(args)
except Exception as e:
traceback.print_exc()