-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs0RunCommandAndReturnErrorMessage.py
96 lines (90 loc) · 2.96 KB
/
fs0RunCommandAndReturnErrorMessage.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
import os, re;
from mWindowsAPI import cConsoleProcess;
sComSpec = os.environ["COMSPEC"];
rSubstitudeTemplate = re.compile(
r"(\\)?"
r"\{"
r"(~?)"
r"("
r"l"
r"|"
r"(n|p)?[0-9]+"
r"|"
r"[fdpnx]+"
r")"
r"\}"
);
def fs0RunCommandAndReturnErrorMessage(asCommandTemplate, sFilePath, o0LastNameMatch, o0LastPathMatch, auLineNumbers = []):
asCommandTemplate = [s for s in asCommandTemplate];
sDrivePath, sNameExtension = sFilePath.rsplit("\\", 1);
if ":" in sDrivePath:
sDrive, sPath = sDrivePath.split(":", 1);
if sDrive.startswith("\\\\?\\"):
sDrive = sDrive[4:];
sDrive += ":";
else:
sDrive, sPath = "", sDrivePath;
if "." in sNameExtension:
sName, sExtension = sNameExtension.rsplit(".", 1);
sExtension = "." + sExtension;
else:
sName, sExtension = sNameExtension, "";
def fsSubstitudeTemplate(oMatch):
sEscape, sDoNotQuote, sChars, s0IndexAppliesToNameOrPath = oMatch.groups();
if sEscape:
return "{" + sDoNotQuote + sChars + "}"; # do not replace.
if sChars == "l":
if fsSubstitudeTemplate.uCurrentLineNumberIndex < len(auLineNumbers):
fsSubstitudeTemplate.uCurrentLineNumberIndex += 1;
return "%d" % auLineNumbers[fsSubstitudeTemplate.uCurrentLineNumberIndex - 1];
return "-1";
if sChars[0] in "0123456789":
o0LastNameOrPathMatch = (
o0LastNameMatch if s0IndexAppliesToNameOrPath == "n" else
o0LastPathMatch if s0IndexAppliesToNameOrPath == "p" else
(o0LastNameMatch or o0LastPathMatch)
);
assert o0LastNameOrPathMatch, \
"There is no %s match from which to extract group %s" % (
{"n": "name", "p": "path"}.get(s0IndexAppliesToNameOrPath, "name or path"),
sChars
)
try:
sSubstitute = o0LastNameOrPathMatch.group(int(sChars));
except IndexError:
sSubstitute = "";
else:
sSubstitute = "";
dsReplacements = {
"f": sFilePath,
"d": sDrive or "",
"p": sPath or "",
"n": sName or "",
"x": sExtension or "",
};
sLastChar = "";
for sChar in sChars:
if sChar == "n" and sLastChar == "p":
sSubstitute += "\\"
sSubstitute += dsReplacements[sChar];
sLastChar = sChar;
if sDoNotQuote == "":
sSubstitute = '"%s"' % sSubstitute.replace('"', '"""');
return sSubstitute;
fsSubstitudeTemplate.uCurrentLineNumberIndex = 0;
asCommandLine = [
# match everything "{" replacement "}", and note if "{" is escaped as "\\{"
rSubstitudeTemplate.sub(fsSubstitudeTemplate, sTemplate)
for sTemplate in asCommandTemplate
];
try:
oProcess = cConsoleProcess.foCreateForBinaryPathAndArguments(
sBinaryPath = asCommandLine[0],
asArguments = asCommandLine[1:],
bRedirectStdOut = False,
bRedirectStdErr = False,
);
except FileNotFoundError:
return "Cannot find binary named '%s'" % asCommandLine[0];
oProcess.fWait();
return None;