Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sherlock-project
GitHub Repository: sherlock-project/sherlock
Path: blob/master/tests/sherlock_interactives.py
761 views
1
import os
2
import platform
3
import re
4
import subprocess
5
6
class Interactives:
7
def run_cli(args:str = "") -> str:
8
"""Pass arguments to Sherlock as a normal user on the command line"""
9
# Adapt for platform differences (Windows likes to be special)
10
if platform.system() == "Windows":
11
command:str = f"py -m sherlock_project {args}"
12
else:
13
command:str = f"sherlock {args}"
14
15
proc_out:str = ""
16
try:
17
proc_out = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
18
return proc_out.decode()
19
except subprocess.CalledProcessError as e:
20
raise InteractivesSubprocessError(e.output.decode())
21
22
23
def walk_sherlock_for_files_with(pattern: str) -> list[str]:
24
"""Check all files within the Sherlock package for matching patterns"""
25
pattern:re.Pattern = re.compile(pattern)
26
matching_files:list[str] = []
27
for root, dirs, files in os.walk("sherlock_project"):
28
for file in files:
29
file_path = os.path.join(root,file)
30
if "__pycache__" in file_path:
31
continue
32
with open(file_path, 'r', errors='ignore') as f:
33
if pattern.search(f.read()):
34
matching_files.append(file_path)
35
return matching_files
36
37
class InteractivesSubprocessError(Exception):
38
pass
39
40