sherlock/tests/sherlock_interactives.py

39 lines
1.4 KiB
Python
Raw Normal View History

2024-05-20 08:44:52 +00:00
import os
2024-05-21 03:24:03 +00:00
import platform
2024-05-20 08:44:52 +00:00
import re
import subprocess
class Interactives:
2024-05-21 03:24:03 +00:00
def run_cli(args:str = "") -> str:
2024-05-20 20:30:08 +00:00
"""Pass arguments to Sherlock as a normal user on the command line"""
2024-05-21 03:24:03 +00:00
# Adapt for platform differences (Windows likes to be special)
2024-10-11 02:23:59 +00:00
if platform.system() == "Windows":
2024-11-01 08:29:23 +00:00
command:str = f"py -m sherlock_project {args}"
2024-05-21 03:24:03 +00:00
else:
command:str = f"sherlock {args}"
proc_out:str = ""
2024-05-20 20:30:08 +00:00
try:
proc_out = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
return proc_out.decode()
except subprocess.CalledProcessError as e:
raise InteractivesSubprocessError(e.output.decode())
2024-05-20 08:44:52 +00:00
2024-05-21 03:24:03 +00:00
def walk_sherlock_for_files_with(pattern: str) -> list[str]:
2024-05-20 20:30:08 +00:00
"""Check all files within the Sherlock package for matching patterns"""
2024-05-21 03:24:03 +00:00
pattern:re.Pattern = re.compile(pattern)
matching_files:list[str] = []
2024-06-24 21:04:50 +00:00
for root, dirs, files in os.walk("sherlock_project"):
2024-05-20 08:44:52 +00:00
for file in files:
file_path = os.path.join(root,file)
if "__pycache__" in file_path:
continue
with open(file_path, 'r', errors='ignore') as f:
if pattern.search(f.read()):
matching_files.append(file_path)
return matching_files
2024-05-20 20:30:08 +00:00
class InteractivesSubprocessError(Exception):
pass