-
Notifications
You must be signed in to change notification settings - Fork 90
/
fatsArgumentLowerNameAndValue.py
171 lines (167 loc) · 7.31 KB
/
fatsArgumentLowerNameAndValue.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
import os, sys;
import mProductDetails;
from mProductDetails import faoGetLicensesFromRegistry, faoGetLicensesFromFile;
from mFileSystemItem import cFileSystemItem;
from foConsoleLoader import foConsoleLoader;
from fOutputUsageInformation import fOutputUsageInformation;
from fOutputVersionInformation import fOutputVersionInformation;
from fOutputLicenseInformation import fOutputLicenseInformation;
from mColorsAndChars import \
COLOR_ADD, CHAR_ADD, \
COLOR_BUSY, CHAR_BUSY,\
COLOR_ERROR, CHAR_ERROR, \
COLOR_REMOVE, CHAR_REMOVE,\
COLOR_WARNING, CHAR_WARNING, \
COLOR_INFO, COLOR_NORMAL;
from mExitCodes import \
guExitCodeBadArgument, \
guExitCodeSuccess;
oConsole = foConsoleLoader();
def fExitWithBadArgumentValue(sArgumentName, sMessage):
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " Invalid ",
COLOR_INFO, sArgumentName,
COLOR_NORMAL, " argument:",
);
oConsole.fOutput(
COLOR_INFO, " ", sMessage,
);
sys.exit(guExitCodeBadArgument);
def fatsArgumentLowerNameAndValue(f0dsGetAdditionalVersionByName = None):
atsArgumentNameAndValue = [];
asArguments = sys.argv[1:];
while len(asArguments) > 0:
sArgument = asArguments.pop(0);
if sArgument == "--":
break;
if len(sArgument) >= 2 and sArgument.startswith("-"):
if "=" in sArgument:
(sNameWithPrefix, s0Value) = sArgument.split("=", 1);
else:
(sNameWithPrefix, s0Value) = (sArgument, None);
if sNameWithPrefix.startswith("--"):
sLowerName = sNameWithPrefix[2:].lower();
else:
sLowerName = sNameWithPrefix[1:].lower();
if sLowerName in ["h", "?", "help"]:
fOutputUsageInformation();
sys.exit(guExitCodeSuccess);
if sLowerName in ["version", "version-check"]:
fOutputVersionInformation(
bCheckForUpdates = sLowerName == "version-check",
bShowInstallationFolders = sLowerName == "version",
dsAdditionalVersion_by_sName = f0dsGetAdditionalVersionByName() if f0dsGetAdditionalVersionByName else {},
);
sys.exit(guExitCodeSuccess);
if sLowerName in ["license", "license-update"]:
fOutputLicenseInformation(
bUpdateIfNeeded = sLowerName == "license-update",
);
sys.exit(guExitCodeSuccess);
if sLowerName in ["license-server-url"]:
bValueIsIsValidURL = False;
if s0Value:
try:
sbLicenseServerURL = bytes(s0Value, "ascii", "strict");
except UnicodeError:
pass;
else:
bValueIsIsValidURL = True;
if not bValueIsIsValidURL:
fExitWithBadArgumentValue(sLowerName, "You must provide a valid URL for a license server as value for this argument.");
for oProductDetails in mProductDetails.faoGetProductDetailsForAllLoadedModules():
if oProductDetails.sb0LicenseServerURL is not None:
oProductDetails.sb0LicenseServerURL = sbLicenseServerURL;
elif sLowerName in ["license-clear-cache"]:
# Remove all cached licenses from registry
aoCachedLicenses = faoGetLicensesFromRegistry();
if len(aoCachedLicenses) == 0:
oConsole.fOutput(
COLOR_WARNING, CHAR_WARNING,
COLOR_NORMAL, " There are ",
COLOR_INFO, "no",
COLOR_NORMAL, " licenses cached in the registry.",
);
else:
oConsole.fOutput(
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Removing ",
COLOR_INFO, str(len(aoCachedLicenses)),
COLOR_NORMAL, " cached licenses from the registry:",
);
for oLicense in aoCachedLicenses:
assert oLicense.fbRemoveFromRegistry(bThrowErrors = True), \
"Unreachable code !?";
oConsole.fOutput(
" ",
COLOR_REMOVE, CHAR_REMOVE,
COLOR_NORMAL, " License ", COLOR_INFO, oLicense.sLicenseId,
COLOR_NORMAL, " covering ", COLOR_INFO, oLicense.sUsageTypeDescription,
COLOR_NORMAL, " by ", COLOR_INFO, oLicense.sLicenseeName,
COLOR_NORMAL, " of ", COLOR_INFO, oLicense.asProductNames[0],
COLOR_NORMAL, " on ", COLOR_INFO, str(oLicense.uLicensedInstances),
COLOR_NORMAL, " machine", "s" if oLicense.uLicensedInstances != 1 else "", ".",
);
sys.exit(guExitCodeSuccess);
elif sLowerName in ["license-load-file"]:
sPath = s0Value or "#license.asc";
oLicenseFile = cFileSystemItem(sPath);
if not oLicenseFile.fbIsFile():
fExitWithBadArgumentValue(sLowerName, "File %s not found." % oLicenseFile.sPath);
aoLoadedLicenses = faoGetLicensesFromFile(sPath);
if len(aoLoadedLicenses) == 0:
oConsole.fOutput(
COLOR_WARNING, CHAR_WARNING,
COLOR_NORMAL, " There are ",
COLOR_INFO, "no",
COLOR_NORMAL, " licenses in the file ",
COLOR_INFO, sPath,
COLOR_NORMAL, ".",
);
else:
oConsole.fOutput(
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Caching ",
COLOR_INFO, str(len(aoLoadedLicenses)),
COLOR_NORMAL, " licenses from file ",
COLOR_INFO, sPath,
COLOR_NORMAL, ":",
);
for oLicense in faoGetLicensesFromFile(sPath):
oLicense.fWriteToRegistry();
oConsole.fOutput(
" ",
COLOR_ADD, CHAR_ADD,
COLOR_NORMAL, " License ", COLOR_INFO, oLicense.sLicenseId,
COLOR_NORMAL, " covering ", COLOR_INFO, oLicense.sUsageTypeDescription,
COLOR_NORMAL, " by ", COLOR_INFO, oLicense.sLicenseeName,
COLOR_NORMAL, " of ", COLOR_INFO, oLicense.asProductNames[0],
COLOR_NORMAL, " on ", COLOR_INFO, str(oLicense.uLicensedInstances),
COLOR_NORMAL, " machine", "s" if oLicense.uLicensedInstances != 1 else "", ".",
);
sys.exit(guExitCodeSuccess);
elif sLowerName == "arguments":
if not s0Value:
fExitWithBadArgumentValue(sLowerName, "You must provide a path to a file containing arguments as value for this argument.");
# Read additional arguments from file and insert them after the current argument.
oArgumentsFile = cFileSystemItem(s0Value);
if not oArgumentsFile.fbIsFile(bThrowErrors = False):
fExitWithBadArgumentValue(sLowerName, "File %s not found." % oArgumentsFile.sPath);
sb0ArgumentsFileContent = oArgumentsFile.fsb0Read(bThrowErrors = False);
if sb0ArgumentsFileContent is None:
fExitWithBadArgumentValue(sLowerName, "File %s cannot be read." % oArgumentsFile.sPath);
sArgumentsFileContent = str(sb0ArgumentsFileContent, "utf-8");
asArguments = [
os.path.expandvars(sStrippedArgumentFileLine) for sStrippedArgumentFileLine in [
sArgumentFileLine.strip() for sArgumentFileLine in sArgumentsFileContent.split("\n")
] if sStrippedArgumentFileLine != ""
] + asArguments;
else:
atsArgumentNameAndValue.append((sArgument, sLowerName, s0Value));
else:
atsArgumentNameAndValue.append((sArgument, None, sArgument));
while len(asArguments) > 0:
sArgument = asArguments.pop(0);
atsArgumentNameAndValue.append((sArgument, None, None));
return atsArgumentNameAndValue;