-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_finder.py
executable file
·114 lines (96 loc) · 4.99 KB
/
string_finder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
import os
from multiprocessing import Pool
import subprocess
import argparse
interesting_paths = ['config']
parser = argparse.ArgumentParser(description='This programm finds all files containing the specified string')
parser.add_argument('directory', help='directory to start')
parser.add_argument('-s', '--string', help='string to find', required=True)
args = parser.parse_args()
dir_name = args.directory
string = args.string
def print_banner():
return """
█████████ █████ ███
███░░░░░███ ░░███ ░░░
░███ ░░░ ███████ ████████ ████ ████████ ███████
░░█████████ ░░░███░ ░░███░░███░░███ ░░███░░███ ███░░███
░░░░░░░░███ ░███ ░███ ░░░ ░███ ░███ ░███ ░███ ░███
███ ░███ ░███ ███ ░███ ░███ ░███ ░███ ░███ ░███
░░█████████ ░░█████ █████ █████ ████ █████░░███████
░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░ ░░░░░ ░░░░░███
███ ░███
░░██████
░░░░░░
███████████ ███ █████
░░███░░░░░░█ ░░░ ░░███
░███ █ ░ ████ ████████ ███████ ██████ ████████
░███████ ░░███ ░░███░░███ ███░░███ ███░░███░░███░░███
░███░░░█ ░███ ░███ ░███ ░███ ░███ ░███████ ░███ ░░░
░███ ░ ░███ ░███ ░███ ░███ ░███ ░███░░░ ░███
█████ █████ ████ █████░░████████░░██████ █████
"""
def main():
global dir_name
print_banner()
print("-------------------------------------------------------------")
print("This programm finds all files containing the specified string")
print("-------------------------------------------------------------")
files = get_all_files(dir_name)
with Pool() as pool:
results = pool.map(check_if_in_file, files)
def gprint(string):
print('\033[92m' + string + '\033[0m')
def rprint(string):
print('\033[91m' + string + '\033[0m')
def check_if_in_file(file_name):
global string
encoding = get_file_encoding(file_name)
if encoding:
try:
check_file(file_name, string, encoding)
except (UnicodeDecodeError, LookupError):
try:
check_file(file_name, string, 'utf-8')
except UnicodeDecodeError:
pass
def check_file(file_name,string,encoding):
with open(file_name,mode='r', encoding=encoding) as reader:
if string.lower() in reader.read().lower():
if any(subpath in file_name for subpath in interesting_paths):
rprint(file_name)
else:
gprint(file_name)
with open(file_name,mode='r', encoding=encoding) as inner:
for line in inner:
if string.lower() in line.lower():
if len(line) > 200:
print(line[:200].strip())
else:
print(line.strip())
print()
return True
return False
def get_file_encoding(file_name):
output = subprocess.run(['file', '-ib', file_name], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
if output == 'regular file':
return 'utf-8'
else:
try:
encoding = [string for string in output.split(' ') if string.startswith('charset=')][0].split('=')[1]
except IndexError:
print('Index Error', file_name)
return False
if encoding == 'binary': # filter binary file
return False
return encoding
#iterate over directory
def get_all_files(start_directory):
all_files = []
for root, dirs, files in os.walk(start_directory):
for file in files:
all_files.append(os.path.join(root, file))
return all_files
if __name__ == '__main__':
main()