-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrs.py
444 lines (425 loc) · 20.3 KB
/
rs.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
""" dS' dS'
dS' .cid ,dSSc ,dS"*sd dS' /R/egular expression /S/earch
dS' SS;' '*' YS( Y dS'
dS' SS` `*%s, dS'
dS' SS b )Sb dS'
dS' .dSSb. P*ssSP' dS'
dS' dS' """;
import multiprocessing, os, re, sys;
sModulePath = os.path.dirname(__file__);
sys.path = [sModulePath] + [sPath for sPath in sys.path if sPath.lower() != sModulePath.lower()];
from fInitializeProduct import fInitializeProduct;
fInitializeProduct();
try: # mDebugOutput use is Optional
import mDebugOutput as m0DebugOutput;
except ModuleNotFoundError as oException:
if oException.args[0] != "No module named 'mDebugOutput'":
raise;
m0DebugOutput = None;
guExitCodeInternalError = 1; # Just in case mExitCodes is not loaded, as we need this later.
try:
from mHumanReadable import fsBytesToHumanReadableString;
from fasSortedAlphabetically import fasSortedAlphabetically;
from fatsArgumentLowerNameAndValue import fatsArgumentLowerNameAndValue;
from fCheckPythonVersion import fCheckPythonVersion;
from fdtoMultithreadedFileNameAndPathMatcher import fdtoMultithreadedFileNameAndPathMatcher;
from foMultithreadedFileContentMatcher import foMultithreadedFileContentMatcher;
from fs0RunCommandAndReturnErrorMessage import fs0RunCommandAndReturnErrorMessage;
from foConsoleLoader import foConsoleLoader;
from mColorsAndChars import *;
from mExitCodes import *;
oConsole = foConsoleLoader();
gsEditoryBinaryPath = os.path.join(os.getenv("LocalAppData"), "Programs", "Microsoft VS Code", "Code.exe");
def frRegExp(sRegExp, sFlags):
return re.compile(sRegExp, sum([
{"i": re.I, "l":re.L, "m":re.M, "s": re.S, "u": re.U, "x": re.X}[sFlag]
for sFlag in sFlags
]));
def fsRegExpToString(rRegExp):
return str(rRegExp.pattern, "ascii", "strict") if isinstance(rRegExp.pattern, bytes) else rRegExp.pattern;
def fxProcessBooleanArgument(sArgumentName, s0Value, u0CanAlsoBeAnIntegerLargerThan = None):
if s0Value is None or s0Value.lower() == "true":
return True;
if s0Value.lower() == "false":
return False;
if u0CanAlsoBeAnIntegerLargerThan is not None:
try:
uValue = int(s0Value);
except:
pass;
else:
if uValue > u0CanAlsoBeAnIntegerLargerThan:
return uValue;
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " The value for ",
COLOR_INFO, sArgument,
COLOR_NORMAL, " must be \"",
COLOR_INFO, "true",
COLOR_NORMAL, "\" (default) or \"",
COLOR_INFO, "false",
COLOR_NORMAL, "\"",
[
" or an integer larger than ", COLOR_INFO, str(u0CanAlsoBeAnIntegerLargerThan), COLOR_NORMAL,
] if u0CanAlsoBeAnIntegerLargerThan is not None else [],
".",
);
sys.exit(guExitCodeBadArgument);
if __name__ == "__main__":
asTestedPythonVersions = ["3.8.5", "3.9.1", "3.9.7", "3.10.0", "3.11.1", "3.11.4", "3.12.0"];
uMaxThreads = max(1, multiprocessing.cpu_count() - 1);
grRegExpArgument = re.compile(
r"\A"
r"(!?[cpn]?|[cpn]?!?)"
r"\/"
r"(.+)"
r"\/"
r"([ilmsux]*)"
r"\Z"
);
# Make sure the Python binary is up to date; we don't want our users to unknowingly run outdated software as this is
# likely to cause unexpected issues.
fCheckPythonVersion("rs", asTestedPythonVersions, "https://github.com/SkyLined/rs/issues/new")
dto0LastNameAndPathMatches_by_sSelectedFilePath = {};
asFolderPaths = set();
arContentRegExps = [];
arNegativeContentRegExps = [];
arPathRegExps = [];
arNegativePathRegExps = [];
arNameRegExps = [];
arNegativeNameRegExps = [];
bRecursive = False;
bVerbose = False;
bPause = False;
bUnicode = False;
bQuiet = False;
uConvertTabsToSpaces = 4;
uNumberOfRelevantLinesBeforeMatch = None;
uNumberOfRelevantLinesAfterMatch = None;
bUseUEditCommandTemplate = False;
asCommandTemplate = [];
for (sArgument, s0LowerName, s0Value) in fatsArgumentLowerNameAndValue():
if s0LowerName in ["r", "recursive"]:
bRecursive = fxProcessBooleanArgument(s0LowerName, s0Value);
elif s0LowerName in ["q", "quiet"]:
bQuiet = fxProcessBooleanArgument(s0LowerName, s0Value);
elif s0LowerName in ["v", "verbose"]:
bVerbose = fxProcessBooleanArgument(s0LowerName, s0Value);
elif s0LowerName in ["p", "pause"]:
bPause = fxProcessBooleanArgument(s0LowerName, s0Value);
elif s0LowerName in ["u", "unicode"]:
bUnicode = fxProcessBooleanArgument(s0LowerName, s0Value);
elif s0LowerName in ["e", "uedit"]:
bUseUEditCommandTemplate = fxProcessBooleanArgument(s0LowerName, s0Value);
elif s0LowerName in ["t", "tabs"]:
xValue = fxProcessBooleanArgument(s0LowerName, s0Value, u0CanAlsoBeAnIntegerLargerThan = 0);
uConvertTabsToSpaces = xValue if isinstance(xValue, int) else 8;
elif s0LowerName in ["l", "lines"]:
# valid formats : "C" "-B", "-B+A" "+A"
o0BeforeAfterMatch = re.match(r"^(?:(\d+)|(?:\-(\d+))?(?:(?:,|,?\+)(\d+))?)$", s0Value) if s0Value else None;
if o0BeforeAfterMatch is None:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " The value for ",
COLOR_ERROR, s0LowerName,
COLOR_NORMAL, " must be a valid range.");
oConsole.fOutput(" Try ", COLOR_INFO, "N", COLOR_NORMAL, ", ", COLOR_INFO, "-N", COLOR_NORMAL, ", ", COLOR_INFO, "-N+N", COLOR_NORMAL, ", or ", COLOR_INFO, "+N", COLOR_NORMAL, ".");
oConsole.fOutput(" Where each N is an integer. '-' prefix indicates before, '+' prefix indices after the match.");
sys.exit(guExitCodeBadArgument);
suBeforeAndAfer, suBefore, suAfter = o0BeforeAfterMatch.groups();
if suBeforeAndAfer:
uNumberOfRelevantLinesBeforeMatch = uNumberOfRelevantLinesAfterMatch = int(suBeforeAndAfer);
else:
uNumberOfRelevantLinesBeforeMatch = int(suBefore or 0);
uNumberOfRelevantLinesAfterMatch = int(suAfter or 0);
elif s0LowerName or s0Value: # arguments before "--"
oRegExpMatch = grRegExpArgument.match(sArgument);
if oRegExpMatch:
sPrefixFlags, sRegExp, sFlags = oRegExpMatch.groups();
bNegative = "!" in sPrefixFlags;
bPath = "p" in sPrefixFlags;
bFileOrFolderName = "n" in sPrefixFlags;
# Paths are type string, file contents are type bytes; reg.exp. must conform:
sxRegExp = sRegExp if (bPath or bFileOrFolderName) else bytes(sRegExp, 'latin1');
try:
rRegExp = frRegExp(sxRegExp, sFlags);
except re.error as oException:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " Invalid regular expressions ",
COLOR_INFO, sPrefixFlags,
COLOR_NORMAL, "/",
COLOR_INFO, sRegExp,
COLOR_NORMAL, "/",
COLOR_INFO, sFlags
);
oConsole.fOutput(
" " * (len(
CHAR_ERROR
+ " Invalid regular expressions "
+ sPrefixFlags
+ "/"
) + oException.pos),
COLOR_ERROR, "▲",
COLOR_NORMAL, " ",
COLOR_INFO, oException.msg
);
sys.exit(guExitCodeBadArgument);
else:
if bPath:
if bNegative:
arNegativePathRegExps.append(rRegExp);
else:
arPathRegExps.append(rRegExp);
elif bFileOrFolderName:
if bNegative:
arNegativeNameRegExps.append(rRegExp);
else:
arNameRegExps.append(rRegExp);
else:
if bNegative:
arNegativeContentRegExps.append(rRegExp);
else:
arContentRegExps.append(rRegExp);
elif os.path.isfile(sArgument):
# The file name was provided on the command line: there are no name and path matches.
dto0LastNameAndPathMatches_by_sSelectedFilePath[sArgument] = None;
elif os.path.isdir(sArgument):
asFolderPaths.add(sArgument);
else:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " Unknown argument: ",
COLOR_INFO, sArgument,
COLOR_NORMAL, ".",
);
sys.exit(guExitCodeBadArgument);
else:
asCommandTemplate.append(sArgument);
# Check arguments and set some defaults
if bUseUEditCommandTemplate:
if asCommandTemplate:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " You cannot provide a commend template when using the -e option.",
);
sys.exit(guExitCodeBadArgument);
if not arContentRegExps:
asCommandTemplate = [gsEditoryBinaryPath, "--goto", "{f}"];
else:
asCommandTemplate = [gsEditoryBinaryPath, "--goto", "{~f}:{l}"]; # line number must be inside quotes
if (
not arContentRegExps and not arNegativeContentRegExps
and not arPathRegExps and not arNegativePathRegExps
and not arNameRegExps and not arNegativeNameRegExps
):
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " Missing regular expression.",
);
sys.exit(guExitCodeBadArgument);
if not dto0LastNameAndPathMatches_by_sSelectedFilePath and not asFolderPaths:
asFolderPaths.add(os.getcwd());
if bRecursive:
if not asFolderPaths:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, "No folders to scan recursively!",
);
sys.exit(guExitCodeBadArgument);
# Show argument values in verbose mode
if bVerbose:
if asFolderPaths:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " Selected folders:");
for sFolderPath in asFolderPaths:
oConsole.fOutput(" ", CHAR_LIST, " ", sFolderPath);
if bRecursive:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " Folders will be traversed recursively");
if dto0LastNameAndPathMatches_by_sSelectedFilePath:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " Selected files:");
for sFilePath in fasSortedAlphabetically(dto0LastNameAndPathMatches_by_sSelectedFilePath.keys()):
oConsole.fOutput(" ", CHAR_LIST, " ", sFilePath);
if arPathRegExps or arNegativePathRegExps:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " File path regular expressions:");
for rNegativePathRegExp in arNegativePathRegExps:
oConsole.fOutput(" ", COLOR_SELECT_NO, CHAR_SELECT_NO, COLOR_NORMAL, " ", fsRegExpToString(rNegativePathRegExp));
for rPathRegExp in arPathRegExps:
oConsole.fOutput(" ", COLOR_SELECT_YES, CHAR_SELECT_YES, COLOR_NORMAL, " ", fsRegExpToString(rPathRegExp));
if arNameRegExps or arNegativeNameRegExps:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " File/folder name regular expressions:");
for rNegativePathRegExp in arNegativeNameRegExps:
oConsole.fOutput(" ", COLOR_SELECT_NO, CHAR_SELECT_NO, COLOR_NORMAL, " ", fsRegExpToString(rNegativePathRegExp));
for rPathRegExp in arNameRegExps:
oConsole.fOutput(" ", COLOR_SELECT_YES, CHAR_SELECT_YES, COLOR_NORMAL, " ", fsRegExpToString(rPathRegExp));
if arContentRegExps or arNegativeContentRegExps:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " File content regular expressions:");
for rNegativeContentRegExp in arNegativeContentRegExps:
oConsole.fOutput(" ", COLOR_SELECT_NO, CHAR_SELECT_NO, COLOR_NORMAL, " ", fsRegExpToString(rNegativeContentRegExp));
for rContentRegExp in arContentRegExps:
oConsole.fOutput(" ", COLOR_SELECT_YES, CHAR_SELECT_YES, COLOR_NORMAL, " ", fsRegExpToString(rContentRegExp));
if bUnicode:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " All '\\0' characters will be removed from the file contents before scanning,");
oConsole.fOutput(" to convert UTF-16 Unicode encoded ASCII characters back to ASCII.");
if asCommandTemplate:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " Command to be executed for each matched file:");
oConsole.fOutput(" ", " ".join(asCommandTemplate));
if bPause:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " After scanning is complete, wait for the user to press ENTER.");
if asFolderPaths:
dto0LastNameAndPathMatches_by_sSelectedFilePath.update(fdtoMultithreadedFileNameAndPathMatcher(
uMaxThreads,
asFolderPaths,
bRecursive,
arPathRegExps, arNegativePathRegExps,
arNameRegExps, arNegativeNameRegExps,
bVerbose
));
if not dto0LastNameAndPathMatches_by_sSelectedFilePath:
if arPathRegExps or arNegativePathRegExps or arNameRegExps or arNegativeNameRegExps:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " No files found that matched any of the ",
" and ".join([s for s in [
"path" if (arPathRegExps or arNegativePathRegExps) else None,
"name" if (arNameRegExps or arNegativeNameRegExps) else None,
] if s]),
" regular expressions.");
else:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " No files found.",
);
uExitCode = guExitCodeSuccess;
elif not arContentRegExps and not arNegativeContentRegExps:
for sFilePath in fasSortedAlphabetically(dto0LastNameAndPathMatches_by_sSelectedFilePath.keys()):
oConsole.fOutput(FILE_NAME, sFilePath); # Strip "\\?\"
if asCommandTemplate:
(o0LastNameMatch, o0LastPathMatch) = dto0LastNameAndPathMatches_by_sSelectedFilePath[sFilePath];
s0ErrorMessage = fs0RunCommandAndReturnErrorMessage(asCommandTemplate, sFilePath, o0LastNameMatch, o0LastPathMatch);
if s0ErrorMessage:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " ", s0ErrorMessage,
);
sys.exit(guExitCodeCannotExecuteCommand);
uExitCode = guExitCodeSuccess if len(dto0LastNameAndPathMatches_by_sSelectedFilePath) > 0 else guExitCodeNoMatchesFound;
else:
oContentMatchingResults = foMultithreadedFileContentMatcher(
uMaxThreads,
list(dto0LastNameAndPathMatches_by_sSelectedFilePath.keys()),
arContentRegExps,
arNegativeContentRegExps,
bUnicode,
uNumberOfRelevantLinesBeforeMatch,
uNumberOfRelevantLinesAfterMatch
);
if bVerbose:
for sFilePath in oContentMatchingResults.asNotScannedFilePaths:
oConsole.fOutput(COLOR_DIM, "- ", sFilePath);
if not oContentMatchingResults.dMatched_auLineNumbers_by_sFilePath:
if arPathRegExps or arNegativePathRegExps or arNameRegExps or arNegativeNameRegExps:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " No match found in ",
str(len(dto0LastNameAndPathMatches_by_sSelectedFilePath)),
" files that matched the ",
" and ".join([s for s in [
"path" if (arPathRegExps or arNegativePathRegExps) else None,
"name" if (arNameRegExps or arNegativeNameRegExps) else None,
] if s]),
" regular expressions.");
else:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " No match found in any files.",
);
uExitCode = guExitCodeNoMatchesFound;
else:
uExitCode = guExitCodeSuccess;
bFirst = True;
bOutputRelevantLines = uNumberOfRelevantLinesBeforeMatch is not None or uNumberOfRelevantLinesAfterMatch is not None;
for sFilePath in fasSortedAlphabetically(oContentMatchingResults.dMatched_auLineNumbers_by_sFilePath.keys()):
auMatchedLineNumbers = sorted(oContentMatchingResults.dMatched_auLineNumbers_by_sFilePath[sFilePath]);
if not bQuiet:
if not bOutputRelevantLines:
oConsole.fOutput(
FILE_NAME, sFilePath,
COLOR_NORMAL, ":",
FILE_LINENO, str(auMatchedLineNumbers[0]),
[
[
COLOR_NORMAL, ",",
FILE_LINENO, str(uLineNumber)
] for uLineNumber in auMatchedLineNumbers[1:]
],
);
else:
if bFirst:
bFirst = False;
else:
# Separator between results for different files.
oConsole.fOutput();
uNextMatchedLineIndex = 0;
oConsole.fOutput(
FILE_BOX, " ",
FILE_BOX_NAME, sFilePath,
FILE_BOX_LINENO, ":%d" % auMatchedLineNumbers[uNextMatchedLineIndex],
FILE_BOX, sPadding = " ",
);
auRelevantLineNumbers = sorted(oContentMatchingResults.dRelevant_asbLines_by_uLineNumber_by_sFilePath[sFilePath].keys());
uPreviousLineNumber = None;
for uRelevantLineNumber in auRelevantLineNumbers:
# Seperator between non-sequential sections of file.
if uPreviousLineNumber is not None and uRelevantLineNumber > uPreviousLineNumber + 1:
oConsole.fOutput(
FILE_BOX, " ",
FILE_CUT_LINENO_COLOMN, "\xB7" * 6,
LINENO_CONTENT_SEPARATOR, ":",
FILE_CUT_NAME, " ", sFilePath,
COLOR_NORMAL, ":",
FILE_CUT_LINENO, str(auMatchedLineNumbers[uNextMatchedLineIndex]),
FILE_CUT_PAD, " ", sPadding = "\xB7",
);
sbRelevantLine = oContentMatchingResults.dRelevant_asbLines_by_uLineNumber_by_sFilePath[sFilePath][uRelevantLineNumber];
bMatchedLine = uNextMatchedLineIndex < len(auMatchedLineNumbers) and uRelevantLineNumber == auMatchedLineNumbers[uNextMatchedLineIndex];
if bMatchedLine:
uNextMatchedLineIndex += 1;
oConsole.fOutput(
FILE_BOX, " ",
bMatchedLine and LINENO_COLOMN_MATCH or LINENO_COLOMN, "%6d" % uRelevantLineNumber,
bMatchedLine and LINENO_CONTENT_SEPARATOR_MATCH or LINENO_CONTENT_SEPARATOR, "│",
bMatchedLine and CONTENT_MATCH or CONTENT, str(sbRelevantLine, 'latin1'),
CONTENT_EOL, CHAR_EOL,
uConvertTabsToSpaces = uConvertTabsToSpaces,
);
uPreviousLineNumber = uRelevantLineNumber;
if asCommandTemplate:
(o0LastNameMatch, o0LastPathMatch) = dto0LastNameAndPathMatches_by_sSelectedFilePath[sFilePath];
s0ErrorMessage = fs0RunCommandAndReturnErrorMessage(asCommandTemplate, sFilePath, o0LastNameMatch, o0LastPathMatch, auMatchedLineNumbers);
if s0ErrorMessage:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " ", s0ErrorMessage,
);
sys.exit(guExitCodeCannotExecuteCommand);
if not bFirst:
oConsole.fOutput();
if bVerbose:
if len(oContentMatchingResults.asNotScannedFilePaths) > 0:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " Scanned %d/%d files, %s bytes." % (
len(dto0LastNameAndPathMatches_by_sSelectedFilePath) - len(oContentMatchingResults.asNotScannedFilePaths),
len(dto0LastNameAndPathMatches_by_sSelectedFilePath),
fsBytesToHumanReadableString(oContentMatchingResults.uScannedBytes),
));
else:
oConsole.fOutput(COLOR_INFO, CHAR_INFO, COLOR_NORMAL, " Scanned %d files, %s bytes." % (
len(dto0LastNameAndPathMatches_by_sSelectedFilePath),
fsBytesToHumanReadableString(oContentMatchingResults.uScannedBytes),
));
if bPause:
input();
sys.exit(uExitCode);
except Exception as oException:
if m0DebugOutput:
m0DebugOutput.fTerminateWithException(oException, guExitCodeInternalError);
raise;