-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfdtoMultithreadedFileNameAndPathMatcher.py
More file actions
300 lines (285 loc) · 11.6 KB
/
fdtoMultithreadedFileNameAndPathMatcher.py
File metadata and controls
300 lines (285 loc) · 11.6 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os, queue, time;
from mMultiThreading import cThread;
from cCounter import cCounter;
from cDict import cDict;
from foConsoleLoader import foConsoleLoader;
from mColorsAndChars import *;
oConsole = foConsoleLoader();
giBarLength = 80;
def fdtoMultithreadedFileNameAndPathMatcher(
uMaxThreads,
asFolderPaths,
bRecursive,
arPathRegExps, arNegativePathRegExps,
arNameRegExps, arNegativeNameRegExps,
bVerbose,
):
oMatcher = cMultithreadedFileNameAndPathMatcher(
uMaxThreads,
asFolderPaths,
bRecursive,
arPathRegExps, arNegativePathRegExps,
arNameRegExps, arNegativeNameRegExps,
bVerbose,
);
return oMatcher.oMatchesByFilePath.dxValue;
def fVerboseOutputHelper(b0Selected, sItemPath, sRegExpType, rRegExp, o0Match):
oConsole.fLock();
try:
oConsole.fOutput(
" ",
[COLOR_SELECT_MAYBE, CHAR_SELECT_MAYBE] if b0Selected is None else
[COLOR_SELECT_YES, CHAR_SELECT_YES] if b0Selected else
[COLOR_SELECT_NO, CHAR_SELECT_NO],
COLOR_NORMAL, " ", sItemPath,
COLOR_DIM, " (",
"matches" if o0Match else "does not match",
" ", sRegExpType, " reg.exp. ", str(rRegExp.pattern),
")",
);
if o0Match and len(o0Match.groups()):
asSubMatchesOutput = [];
for sSubMatch in o0Match.groups():
asSubMatchesOutput += [", " if len(asSubMatchesOutput) else "", COLOR_NORMAL, sSubMatch, COLOR_DIM];
oConsole.fOutput(COLOR_DIM, " Sub-matches: ", asSubMatchesOutput, ".");
finally:
oConsole.fUnlock();
class cMultithreadedFileNameAndPathMatcher(object):
def __init__(oSelf,
uMaxThreads,
asFolderPaths,
bRecursive,
arPathRegExps, arNegativePathRegExps,
arNameRegExps, arNegativeNameRegExps,
bVerbose,
):
oSelf.oUnhandledItemPathsQueue = queue.Queue();
oSelf.bRecursive = bRecursive;
oSelf.arPathRegExps = arPathRegExps;
oSelf.arNegativePathRegExps = arNegativePathRegExps;
oSelf.arNameRegExps = arNameRegExps;
oSelf.arNegativeNameRegExps = arNegativeNameRegExps;
oSelf.bVerbose = bVerbose;
oSelf.oMatchesByFilePath = cDict();
oSelf.oException = None;
oSelf.oNumberOfFilesFound = cCounter();
oSelf.oNumberOfFoldersFound = cCounter();
oSelf.oNumberOfItemsFound = cCounter(0);
oSelf.oNumberOfItemsCompleted = cCounter();
oSelf.oScanThreadsStarted = cCounter(uMaxThreads);
oSelf.oScanThreadsFinished = cCounter(0);
if oSelf.bVerbose:
oConsole.fOutput(
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Scanning ",
COLOR_INFO, str(len(asFolderPaths)),
COLOR_NORMAL, " folder paths", " (and descendants)" if bRecursive else "", "...",
);
for sFolderPath in set(asFolderPaths):
oSelf.oNumberOfItemsFound.fuIncrease();
if os.path.isdir(sFolderPath):
oSelf.oUnhandledItemPathsQueue.put(sFolderPath);
oSelf.fDebug();
else:
oSelf.oNumberOfItemsCompleted.fuIncrease();
oSelf.fDebug();
aoThreads = [
cThread(oSelf.fScanThread)
for x in range(uMaxThreads)
] + [
cThread(oSelf.fStatusThread)
];
for oThread in aoThreads:
oThread.fStart(bVital = False);
for oThread in aoThreads:
oThread.fWait();
def fDebug(oSelf):
# oConsole.fOutput("%d/%d/%d/%d" % (oSelf.oNumberOfFilesFound.uValue, oSelf.oNumberOfFoldersFound.uValue, oSelf.oNumberOfItemsFound.uValue, oSelf.oNumberOfItemsCompleted.uValue));
# assert oSelf.oNumberOfFilesFound.uValue + oSelf.oNumberOfFoldersFound.uValue <= oSelf.oNumberOfItemsFound.uValue, \
# "111!";
# assert oSelf.oNumberOfItemsCompleted.uValue <= oSelf.oNumberOfItemsFound.uValue, \
# "333!";
pass;
def fScanThread(oSelf):
try:
while oSelf.oNumberOfItemsCompleted.uValue < oSelf.oNumberOfItemsFound.uValue and not oSelf.oException:
# Try to get the item's content is if it is a folder. If this fails, it is assumed to be a file.
sItemPath = oSelf.oUnhandledItemPathsQueue.get();
if sItemPath is None:
break;
try:
asSubItemNames = os.listdir("\\\\?\\" + sItemPath);
except Exception:
# This failed: assume it is a file.
oSelf.oNumberOfFilesFound.fuIncrease();
bIsFile = True;
else:
# This is a folder
oSelf.oNumberOfFoldersFound.fuIncrease();
bIsFile = False;
oSelf.fDebug();
if bIsFile:
oSelf.fHandleFile(sItemPath);
else:
oSelf.fHandleFolder(sItemPath, asSubItemNames);
oSelf.fDebug();
oSelf.oNumberOfItemsCompleted.fuIncrease();
oSelf.fDebug();
except Exception as oException:
oSelf.oException = oException;
raise;
finally:
oSelf.oScanThreadsFinished.fuIncrease();
# oConsole.fOutput("scan thread %d/%d done" % (oSelf.oScanThreadsFinished.uValue, oSelf.oScanThreadsStarted.uValue));
oSelf.oUnhandledItemPathsQueue.put(None);
def fHandleFile(oSelf, sFilePath):
o0LastNameMatch = None;
o0LastPathMatch = None;
# Having no reg.exps. means always add:
if (
oSelf.arPathRegExps or oSelf.arNegativePathRegExps
or oSelf.arNameRegExps or oSelf.arNegativeNameRegExps
):
if oSelf.arNameRegExps or oSelf.arNegativeNameRegExps:
sItemName = os.path.basename(sFilePath);
# Having negative reg.exps. means not add if matched:
for rNegativePathRegExp in oSelf.arNegativePathRegExps:
o0Match = rNegativePathRegExp.search(sFilePath)
if o0Match:
# Matching negative reg.exp. means don't add (and stop matching).
if oSelf.bVerbose:
fVerboseOutputHelper(False, sFilePath, "negative path", rNegativePathRegExp, o0Match);
return;
for rNegativeNameRegExp in oSelf.arNegativeNameRegExps:
o0Match = rNegativeNameRegExp.search(sItemName);
if o0Match:
# Matching negative reg.exp. means don't add (and stop matching).
if oSelf.bVerbose:
fVerboseOutputHelper(False, sFilePath, "negative name", rNegativeNameRegExp, o0Match);
return;
# Not matching negative reg.exp. means maybe add:
# Not having positive reg.exps. means add:
# Having positive reg.exps. means add if matches all:
if oSelf.arPathRegExps:
for rPathRegExp in oSelf.arPathRegExps:
o0Match = rPathRegExp.search(sFilePath);
if oSelf.bVerbose:
fVerboseOutputHelper(None if o0Match else False, sFilePath, "path", rPathRegExp, o0Match);
if o0Match:
o0LastPathMatch = o0Match;
else:
return;
if oSelf.arNameRegExps:
for rNameRegExp in oSelf.arNameRegExps:
o0Match = rNameRegExp.search(sItemName);
if oSelf.bVerbose:
fVerboseOutputHelper(None if o0Match else False, sFilePath, "name", rNameRegExp, o0Match);
if o0Match:
o0LastNameMatch = o0Match;
else:
return;
if oSelf.bVerbose:
sMatchedExpressionTypes = " and ".join([s for s in [
"path" if oSelf.arPathRegExps else None,
"name" if oSelf.arNameRegExps else None,
] if s]);
oConsole.fOutput(
" ",
COLOR_SELECT_YES, CHAR_SELECT_YES,
COLOR_NORMAL, " ", sFilePath,
COLOR_DIM, " (matches all ", sMatchedExpressionTypes, " reg.exp.)",
);
elif oSelf.bVerbose:
oConsole.fOutput(
" ",
COLOR_SELECT_YES, CHAR_SELECT_YES,
COLOR_NORMAL, " ", sFilePath,
);
oSelf.oMatchesByFilePath.fSet(sFilePath, (o0LastNameMatch, o0LastPathMatch));
def fHandleFolder(oSelf, sFolderPath, asSubItemNames):
# Having no reg.exps. means always add:
if (
oSelf.arPathRegExps or oSelf.arNegativePathRegExps
or oSelf.arNameRegExps or oSelf.arNegativeNameRegExps
):
# Having negative path reg.exps. means not add if matched:
for rNegativePathRegExp in oSelf.arNegativePathRegExps:
oMatch = rNegativePathRegExp.search(sFolderPath);
if oMatch:
# Matching negative path reg.exp. means don't add and files in this folder (and stop matching).
if oSelf.bVerbose:
fVerboseOutputHelper(False, sFolderPath + "\\*", "negative path", rNegativePathRegExp, oMatch);
return;
# Not matching negative path reg.exp. means maybe add:
# Having positive reg.exps. means add if matches all:
if oSelf.arPathRegExps:
for rPathRegExp in oSelf.arPathRegExps:
oMatch = rPathRegExp.search(sFolderPath);
if not oMatch:
if oSelf.bVerbose:
fVerboseOutputHelper(False, sFolderPath + "\\*", "path", rPathRegExp, None);
return;
if oSelf.bVerbose:
oConsole.fOutput(
" ",
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Scanning ",
COLOR_INFO, str(len(asSubItemNames)),
COLOR_NORMAL, " descendants of folder ",
COLOR_INFO, sFolderPath,
COLOR_DIM, " (matches all path reg.exp.)",
);
oSelf.oNumberOfItemsFound.fuAdd(len(asSubItemNames));
for sSubItemName in asSubItemNames:
sSubItemPath = os.path.join(sFolderPath, sSubItemName);
# Doing a recursive find means getting sub-items of sub-folders as well, so queue it:
if oSelf.bRecursive:
oSelf.oUnhandledItemPathsQueue.put(sSubItemPath);
else:
# Not doing a recursive find means only handle sub-items that are files:
if os.path.isfile("\\\\?\\" + sSubItemPath):
oSelf.oNumberOfFilesFound.fuIncrease();
oSelf.fDebug();
oSelf.fHandleFile(sSubItemPath);
oSelf.oNumberOfItemsCompleted.fuIncrease();
oSelf.fDebug();
else:
oSelf.oNumberOfFoldersFound.fuIncrease();
oSelf.fDebug();
oSelf.oNumberOfItemsCompleted.fuIncrease();
oSelf.fDebug();
def fStatusThread(oSelf):
try:
bMatchingPath = oSelf.arPathRegExps or oSelf.arNegativePathRegExps;
bMatchingName = oSelf.arNameRegExps or oSelf.arNegativeNameRegExps;
bMatchingPathOrName = bMatchingPath or bMatchingName;
while oSelf.oScanThreadsStarted.uValue != oSelf.oScanThreadsFinished.uValue and not oSelf.oException:
uNumberOfItemsFound = oSelf.oNumberOfItemsFound.uValue;
uNumberOfFilesFound = oSelf.oNumberOfFilesFound.uValue;
uNumberOfFoldersFound = oSelf.oNumberOfFoldersFound.uValue;
uNumberOfItemsCompleted = oSelf.oNumberOfItemsCompleted.uValue;
uNumberOfItemsRemaining = uNumberOfItemsFound - uNumberOfFoldersFound - uNumberOfFilesFound;
# TODO: nProgress and nSubProgress should never be > 1 but I've ran into this happening
# in nSubProgress. This is why I use min() to prevent it from causing an issue.
# I want to find out why this happens and fix it.
nProgress = min(1.0, 1.0 * uNumberOfItemsCompleted / uNumberOfItemsFound) if uNumberOfItemsFound else 0;
nSubProgress = min(1.0, 1.0 * oSelf.oMatchesByFilePath.uSize / uNumberOfFilesFound) if uNumberOfFilesFound else 0;
oConsole.fProgressBar(
nProgress,
[
"Matched ",
" and ".join([s for s in [
"path" if bMatchingPath else None,
"name" if bMatchingName else None,
] if s]),
" for ", str(oSelf.oMatchesByFilePath.uSize), "/", str(uNumberOfFilesFound), " files",
] if bMatchingPathOrName else [
"Found ", str(uNumberOfFilesFound), " files",
],
" (", str(uNumberOfItemsRemaining), "/", str(uNumberOfItemsFound), " items remaining)",
nSubProgress = nSubProgress,
);
time.sleep(0.25);
except Exception as oException:
oSelf.oException = oException;
raise;