-
Notifications
You must be signed in to change notification settings - Fork 90
/
GetDependencies.py
238 lines (232 loc) · 9.19 KB
/
GetDependencies.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
assert __name__ == "__main__", \
"This is a Python script, not a loadable module.";
import io, json, os, re, sys, urllib.request, zipfile;
sMainFolder = os.path.dirname(__file__);
sModulesFolder = os.path.join(sMainFolder, "modules");
sys.path += [sMainFolder, sModulesFolder];
from foConsoleLoader import foConsoleLoader;
from mColorsAndChars import \
CHAR_BUSY, \
CHAR_ERROR, \
CHAR_LIST, \
CHAR_OK, \
CHAR_WARNING, \
COLOR_BUSY, \
COLOR_ERROR, \
COLOR_HILITE, \
COLOR_LIST, \
COLOR_NORMAL, \
COLOR_OK, \
COLOR_WARNING, \
CONSOLE_UNDERLINE;
oConsole = foConsoleLoader();
from mExitCodes import \
guExitCodeInternalError;
rIgnoredProductFilesPattern = re.compile(r"^(Tests(\..*)?|lgtm\.yml|modules|releases|Internal error reports)($|\\)|(^|\\)[\.#].*");
if not os.path.isdir(sModulesFolder):
oConsole.fStatus(
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Creating ",
COLOR_HILITE, "modules",
COLOR_NORMAL, " folder...",
);
os.mkdir(sModulesFolder);
oConsole.fOutput(
COLOR_OK, CHAR_OK,
COLOR_NORMAL, " Created ",
COLOR_HILITE, "modules",
COLOR_NORMAL, " folder..",
);
asAvailableProductNames = [];
asQueuedProductDetailsJSONFilePaths = [os.path.join(sMainFolder, "dxProductDetails.json")];
while asQueuedProductDetailsJSONFilePaths:
sQueuedProductDetailsJSONFilePath = asQueuedProductDetailsJSONFilePaths.pop(0);
oConsole.fStatus(
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Reading ",
COLOR_HILITE, sQueuedProductDetailsJSONFilePath,
COLOR_NORMAL, " file...",
);
oProductDetailsFile = open(sQueuedProductDetailsJSONFilePath, "rb");
try:
dxProductDetails = json.load(oProductDetailsFile);
finally:
oProductDetailsFile.close();
oConsole.fStatus(
COLOR_OK, CHAR_OK,
COLOR_NORMAL, " Read ",
COLOR_HILITE, sQueuedProductDetailsJSONFilePath,
COLOR_NORMAL, " file.",
);
# Since we now know the product name, add it to the list of products we know to be available.
asAvailableProductNames.append(dxProductDetails["sProductName"]);
# Go through the list of dependencies:
for sProductName in dxProductDetails.get("a0sDependentOnProductNames", []):
if sProductName in asAvailableProductNames:
continue; # We already have this product.
# Download the product source as a zip file from GitHub:
sProductZipURL = "https://github.com/SkyLined/%s/archive/master.zip" % (sProductName,);
oConsole.fStatus(
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Downloading ",
COLOR_HILITE, sProductName,
COLOR_NORMAL, " from ",
COLOR_HILITE + CONSOLE_UNDERLINE, sProductZipURL,
COLOR_NORMAL, "...",
);
oResponse = urllib.request.urlopen(sProductZipURL);
# In the Python 2 version of this code, I would detect a failure to secure the connection
# and explain that this may be caused by outdated root certificates on the local machine.
# I cannot update that code to Python 3 because I do not know how to detected it; I do
# not know how to trigger it in order to determine this either. Should I at some point
# find out; here's the message I used to show in this situation:
# print " You may need to update your root certificates!";
# print " This can be done from an elevated command-prompt with the following commands:";
# print " ";
# print " > CERTUTIL.EXE -f -generateSSTFromWU \"%TEMP%\\roots.sst\"";
# print " > POWERSHELL.EXE Get-ChildItem -Path \"%TEMP%\\roots.sst\" ^| ";
# print " Import-Certificate -CertStoreLocation Cert:\\LocalMachine\\Root";
try:
if oResponse.getcode() != 200:
oConsole.fOutput(
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " Cannot download ",
COLOR_HILITE, sProductName,
COLOR_NORMAL, " from ",
COLOR_HILITE, sProductZipURL,
COLOR_NORMAL, "!",
);
oConsole.fOutput(
COLOR_NORMAL, " Expected HTTP 200 response, got ",
COLOR_HILITE, "HTTP %03d" % oResponse.getcode(),
COLOR_NORMAL, ".",
);
sys.exit(guExitCodeInternalError);
if oResponse.geturl() != sProductZipURL:
oConsole.fOutput(
COLOR_WARNING, CHAR_WARNING,
COLOR_NORMAL, " Redirected to ",
COLOR_HILITE, oResponse.geturl(),
COLOR_NORMAL, ".",
);
sProductZip = oResponse.read();
finally:
oResponse.close();
oConsole.fStatus(
COLOR_OK, CHAR_OK,
COLOR_NORMAL, " Downloaded ",
COLOR_HILITE, sProductName,
COLOR_NORMAL, " from ",
COLOR_HILITE, sProductZipURL,
COLOR_NORMAL, ".",
);
# Open the zip file.
oConsole.fStatus(
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Creating ",
COLOR_HILITE, sProductName,
COLOR_NORMAL, "...",
);
oProductZipFileStream = io.BytesIO(sProductZip);
oProductZipFile = zipfile.ZipFile(oProductZipFileStream, "r");
asProductsFileAndFoldersHeaders = [
"%s-main%s" % (sProductName, os.sep),
"%s-master%s" % (sProductName, os.sep),
]; # Really, why!?
sProductModuleFolderPath = os.path.join("modules", sProductName);
# Create the module folder.
if not os.path.isdir(sProductModuleFolderPath):
oConsole.fStatus(
COLOR_NORMAL, " ",
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Creating ",
COLOR_HILITE, sProductModuleFolderPath,
COLOR_NORMAL, " folder...",
);
os.mkdir(sProductModuleFolderPath);
oConsole.fOutput(
COLOR_NORMAL, " ",
COLOR_LIST, CHAR_LIST,
COLOR_NORMAL, " Created ",
COLOR_HILITE, sProductModuleFolderPath,
COLOR_NORMAL, " folder.",
);
# Go through the list of source files in the zip
for oZipInfo in oProductZipFile.infolist():
sProductFileOrFolderInZipName = oZipInfo.filename;
# Get the desired local path of the file from the name in the zip file:
sProductFileOrFolderPathWithHeader = oZipInfo.filename.replace(os.altsep, os.sep);
sProductFileOrFolderPathWithHeader = oZipInfo.filename.replace(os.altsep, os.sep);
for sProductsFileAndFoldersHeader in asProductsFileAndFoldersHeaders:
if sProductFileOrFolderPathWithHeader.startswith(sProductsFileAndFoldersHeader):
break;
else:
oConsole.fOutput(
COLOR_NORMAL, " ",
COLOR_ERROR, CHAR_ERROR,
COLOR_NORMAL, " ", "Folder" if sProductFileOrFolderPathWithHeader.endswith(os.sep) else "File", " ",
COLOR_HILITE, sProductFileOrFolderPathWithHeader,
COLOR_NORMAL, " does not start with a known header.",
);
sys.exit(guExitCodeInternalError);
sProductFileOrFolderPath = sProductFileOrFolderPathWithHeader[len(sProductsFileAndFoldersHeader):];
# We ignore certain files (.git, tests, the products' own dependencies folder, etc.)
if rIgnoredProductFilesPattern.match(sProductFileOrFolderPath):
continue;
# Check if it is a folder or file path
if sProductFileOrFolderPath == "" or sProductFileOrFolderPath.endswith(os.sep):
# Check if this folder exists and create it if it does not:
sProductFolderInModulesFolderPath = os.path.join(sProductModuleFolderPath, sProductFileOrFolderPath);
if not os.path.exists(sProductFolderInModulesFolderPath):
oConsole.fStatus(
COLOR_NORMAL, " ",
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Creating ",
COLOR_HILITE, sProductFolderInModulesFolderPath,
COLOR_NORMAL, " folder...",
);
os.mkdir(sProductFolderInModulesFolderPath);
oConsole.fOutput(
COLOR_NORMAL, " ",
COLOR_LIST, CHAR_LIST,
COLOR_NORMAL, " Created ",
COLOR_HILITE, sProductFolderInModulesFolderPath,
COLOR_NORMAL, " folder.",
);
else:
# Extract the file:
sProductFileInModulesFolderPath = os.path.join(sProductModuleFolderPath, sProductFileOrFolderPath);
oConsole.fStatus(
COLOR_NORMAL, " ",
COLOR_BUSY, CHAR_BUSY,
COLOR_NORMAL, " Extracting ",
COLOR_HILITE, sProductFileInModulesFolderPath,
COLOR_NORMAL, "...",
);
oZipFile = oProductZipFile.open(oZipInfo, "r");
try:
sProductFile = oZipFile.read();
finally:
oZipFile.close();
oProductFileInModulesFolder = open(sProductFileInModulesFolderPath, "wb");
try:
oProductFileInModulesFolder.write(sProductFile);
finally:
oProductFileInModulesFolder.close();
oConsole.fOutput(
COLOR_NORMAL, " ",
COLOR_LIST, CHAR_LIST,
COLOR_NORMAL, " Extracted ",
COLOR_HILITE, sProductFileInModulesFolderPath,
COLOR_NORMAL, ".",
);
# If this is a dxProductDetails.json file in the root folder of the product, add it to the
# queue for processing, so that we also download the dependencies for all dependencies.
if sProductFileOrFolderPath.lower() == "dxProductDetails.json".lower():
asQueuedProductDetailsJSONFilePaths.append(sProductFileInModulesFolderPath);
oConsole.fOutput(
COLOR_OK, CHAR_OK,
COLOR_NORMAL, " Created ",
COLOR_HILITE, sProductName,
COLOR_NORMAL, ".",
);