-
Notifications
You must be signed in to change notification settings - Fork 1
/
searcher.py
186 lines (156 loc) · 5.41 KB
/
searcher.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import sys
from commands import *
print("searcher 0.5.2")
file_extensions=[".service"] # cached
skipped_file_substrings = [] # cached
case_sensitive=False # cached partially
match_strings=['zypper'] # not cached
skipped_strings = [] # not cached
multiple_lines=True # not cached
end_print_files_dict=True # not cached
skip_over_this_size = 2 * GiB # not cached
stop_after_every_found_line=False # not cached
end_print_files_dict=True # not cached
folder = OS.args[1]
if Dir.exist(folder):
paths = [folder]
else:
paths=['.']
skipped_paths=['/mnt/c/Windows/',
'/mnt/c/program files/',
'/mnt/c/program files (x86)/',
'/mnt/c/programdata/',
'/mnt/c/MSOCache',
r'c:\Windows',
r'c:\program files',
r'c:\program files (x86)',
r'c:\MSOCache',
r'c:\programdata',
r'C:\Users\Egorov\Documents\!Не моё']
whoami = Console.get_output("whoami").strip()
if OS.windows:
username = whoami.split(backslash)[1] + "." + whoami.split(backslash)[0]
else:
username = whoami
cache_create = "--cache-create" in OS.args or "--create-cache" in OS.args
cache_load = "--cache-load" in OS.args or "--load-cache" in OS.args
debug = "--debug" in OS.args
if cache_create:
print("creating cache...")
if cache_load:
print("loading cache...")
_printed_results = []
def print_result(file, line, line_cnt, found_string):
Print.rewrite()
if file not in _printed_results:
Print.colored(file, "green")
if file not in _printed_results or multiple_lines:
line = line.replace(found_string, Print.colored(found_string, "red", verbose=False))
Print.colored(fr"{line_cnt}:{line}"[:(Console.width() * 5) - 3] + "...")
_printed_results.append(file)
def check_strings(string, substring, case_sensitive):
if case_sensitive:
string_needed_case = string
substring_needed_case = substring
elif not case_sensitive:
string_needed_case = string.lower()
substring_needed_case = substring.lower()
return substring in string
files_to_read = []
if cache_create or cache_load:
files_to_read = JsonList("searcher_cache.json")
if cache_create:
files_to_read.string = []
print(f"{Time.dotted()} search files")
cnt = 0
if not cache_load:
for path in paths:
path = Path.combine(path, "")
for root, dirs, files in os.walk(path):
cnt += 1
if cnt % 100 == 0:
Print.rewrite(f"{cnt}/???", root)
skipped = False
for skipped_path in skipped_paths:
if root.lower().startswith(skipped_path.lower()) and skipped_path != "":
skipped = True
break
if not skipped:
for file in files:
skipped_file = False
for skipped_substring in skipped_file_substrings:
skipped_substring = skipped_substring if case_sensitive else skipped_substring.lower()
file_needed_case = file if case_sensitive else file.lower()
if skipped_substring in file_needed_case:
skipped_file = True
break
if skipped_file:
continue
file_path = Path.combine(root, file)
if not file_extensions: ## if empty
files_to_read.append(file_path)
continue
for ext in file_extensions:
if file.lower().endswith(ext.lower()):
files_to_read.append(file_path)
break
if cache_create:
files_to_read.save()
len_files_to_read = len(files_to_read)
Print.rewrite()
print(f"{Time.dotted()} {len_files_to_read} files to read from {cnt} folders")
cnt = 0
files = []
b = Bench()
for file in files_to_read:
try:
filesize = File.get_size(file)
except FileNotFoundError:
filesize = 0
if filesize > skip_over_this_size:
if debug:
print("BEEG file, skip " + file)
continue
if debug:
print(file)
time = b.end(start_immediately=True)
if time > 1:
print(time, b.prefix)
cnt += 1
if cnt % 100 == 0:
Print.rewrite(f"{cnt}/{len_files_to_read}", file)
try:
file_content = File.read(file)
except (PermissionError, FileNotFoundError) as e:
Print.rewrite()
print(e)
continue
file_lines = Str.nl(file_content)
for line_cnt, line in enumerate(file_lines):
skip = False
for string in skipped_strings:
if check_strings(line, string, case_sensitive):
skip = True
break
if skip:
break
if skip:
continue
add_to_result = False
for line_cnt, line in enumerate(file_lines):
for string in match_strings:
if check_strings(line, string, case_sensitive):
print_result(file, line, line_cnt, string)
add_to_result = True
if add_to_result:
files.append(file)
b.prefix = file
time = b.end(start_immediately=True)
if time > 1:
print(time, b.prefix)
print(f"{Time.dotted()} end")
print()
if end_print_files_dict:
for file in files:
print('npp "' + file + '"')