in vmassist/linux/vmassist.py [0:0]
def searchDirForString(dirToSearch, returnDict, string):
for root, _, files in os.walk(dirToSearch):
# loop through all files in the dirToSearch
for file_name in files:
file_path = os.path.join(root, file_name)
# Check if it's a regular file (skip links, sockets, pipes, etc.)
if os.path.isfile(file_path):
try:
# Create a holding space for any/all matched text
defLines=""
# Open this file and search for the string
with open(file_path, 'r') as file:
for line_number, line in enumerate(file, start=1):
# store the line number and line in the return string
if string in line:
# after the first occurrance add a newline every time we find a line
if (defLines):
defLines = f"{defLines}\n"
defLines = f"{defLines}{line_number}: {line.strip()}"
# if we found anything create the file entry in the dict with all lines
if ( defLines ):
returnDict[file_path] = defLines
except (UnicodeDecodeError, PermissionError):
# Skip files that can't be read due to encoding or permission issues
# just create the exception code block, but we won't be using it
pass