forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.py
executable file
·550 lines (462 loc) · 20.1 KB
/
get.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#!/usr/bin/env python
"""Script to download and extract tools
This script will download and extract required tools into the current directory.
Tools list is obtained from package/package_esp32_index.template.json file.
"""
from __future__ import print_function
__author__ = "Ivan Grokhotkov"
__version__ = "2015"
import os
import shutil
import errno
import os.path
import hashlib
import json
import platform
import sys
import tarfile
import zipfile
import re
import time
import argparse
import ssl
import contextlib
from urllib.request import urlretrieve
from urllib.request import urlopen
from urllib.response import addinfourl
from typing import IO, Any, Callable, Dict, Iterator, List, Optional, Set, Tuple, Union
from ssl import SSLContext
unicode = lambda s: str(s) # noqa: E731
# the older "DigiCert Global Root CA" certificate used with github.com
DIGICERT_ROOT_CA_CERT = """
-----BEGIN CERTIFICATE-----
MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT
MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j
b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB
CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97
nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt
43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P
T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4
gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO
BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR
TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw
DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr
hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg
06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF
PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls
YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk
CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=
-----END CERTIFICATE-----
"""
# the newer "DigiCert Global Root G2" certificate used with dl.espressif.com
DIGICERT_ROOT_G2_CERT = """
-----BEGIN CERTIFICATE-----
MIIDjjCCAnagAwIBAgIQAzrx5qcRqaC7KGSxHQn65TANBgkqhkiG9w0BAQsFADBh
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH
MjAeFw0xMzA4MDExMjAwMDBaFw0z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ERT_ROOT_G2_CERT,
'github.com': DIGICERT_ROOT_CA_CERT}
# Initialize start_time globally
start_time = -1
# determine if application is a script file or frozen exe
if getattr(sys, "frozen", False):
current_dir = os.path.dirname(os.path.realpath(unicode(sys.executable)))
elif __file__:
current_dir = os.path.dirname(os.path.realpath(unicode(__file__)))
dist_dir = current_dir + "/dist/"
def sha256sum(filename, blocksize=65536):
hash = hashlib.sha256()
with open(filename, "rb") as f:
for block in iter(lambda: f.read(blocksize), b""):
hash.update(block)
return hash.hexdigest()
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno != errno.EEXIST or not os.path.isdir(path):
raise
def format_time(seconds):
minutes, seconds = divmod(seconds, 60)
return "{:02}:{:05.2f}".format(int(minutes), seconds)
def report_progress(block_count, block_size, total_size):
downloaded_size = block_count * block_size
if sys.stdout.isatty():
if total_size > 0:
percent_complete = min((downloaded_size / total_size) * 100, 100)
sys.stdout.write(
f"\rDownloading... {percent_complete:.2f}% - {downloaded_size / 1024 / 1024:.2f} MB downloaded" # noqa: E501
)
else:
sys.stdout.write(
f"\rDownloading... {downloaded_size / 1024 / 1024:.2f} MB downloaded" # noqa: E501
)
sys.stdout.flush()
def print_verification_progress(total_files, i, t1):
if sys.stdout.isatty():
sys.stdout.write(f"\rElapsed time {format_time(time.time() - t1)}")
sys.stdout.flush()
def verify_files(filename, destination, rename_to):
# Set the path of the extracted directory
extracted_dir_path = destination
t1 = time.time()
if filename.endswith(".zip"):
try:
archive = zipfile.ZipFile(filename, "r")
file_list = archive.namelist()
except zipfile.BadZipFile:
if verbose:
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
return False
elif filename.endswith(".tar.gz"):
try:
archive = tarfile.open(filename, "r:gz")
file_list = archive.getnames()
except tarfile.ReadError:
if verbose:
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
return False
elif filename.endswith(".tar.xz"):
try:
archive = tarfile.open(filename, "r:xz")
file_list = archive.getnames()
except tarfile.ReadError:
if verbose:
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
return False
else:
raise NotImplementedError("Unsupported archive type")
try:
first_dir = file_list[0].split("/")[0]
total_files = len(file_list)
for i, zipped_file in enumerate(file_list, 1):
local_path = os.path.join(extracted_dir_path, zipped_file.replace(first_dir, rename_to, 1))
if not os.path.exists(local_path):
if verbose:
print(f"\nMissing {zipped_file} on location: {extracted_dir_path}")
print(f"Verification failed; aborted in {format_time(time.time() - t1)}")
return False
print_verification_progress(total_files, i, t1)
except Exception as e:
print(f"\nError: {e}")
return False
if verbose:
print(f"\nVerification passed; completed in {format_time(time.time() - t1)}")
return True
def is_latest_version(destination, dirname, rename_to, cfile, checksum):
current_version = None
expected_version = None
try:
expected_version = checksum
with open(os.path.join(destination, rename_to, ".package_checksum"), "r") as f:
current_version = f.read()
if verbose:
print(f"\nTool: {rename_to}")
print(f"Current version: {current_version}")
print(f"Expected version: {expected_version}")
if current_version and current_version == expected_version:
if verbose:
print("Latest version already installed. Skipping extraction")
return True
if verbose:
print("New version detected")
except Exception as e:
if verbose:
print(f"Failed to verify version for {rename_to}: {e}")
return False
def unpack(filename, destination, force_extract, checksum): # noqa: C901
sys_name = platform.system()
dirname = ""
cfile = None # Compressed file
file_is_corrupted = False
if not force_extract:
print(" > Verify archive... ", end="", flush=True)
try:
if filename.endswith("tar.gz"):
if tarfile.is_tarfile(filename):
cfile = tarfile.open(filename, "r:gz")
dirname = cfile.getnames()[0].split("/")[0]
else:
print("File corrupted!")
file_is_corrupted = True
elif filename.endswith("tar.xz"):
if tarfile.is_tarfile(filename):
cfile = tarfile.open(filename, "r:xz")
dirname = cfile.getnames()[0].split("/")[0]
else:
print("File corrupted!")
file_is_corrupted = True
elif filename.endswith("zip"):
if zipfile.is_zipfile(filename):
cfile = zipfile.ZipFile(filename)
dirname = cfile.namelist()[0].split("/")[0]
else:
print("File corrupted!")
file_is_corrupted = True
else:
raise NotImplementedError("Unsupported archive type")
except EOFError:
print("File corrupted or incomplete!")
cfile = None
file_is_corrupted = True
if file_is_corrupted:
corrupted_filename = filename + ".corrupted"
os.rename(filename, corrupted_filename)
if verbose:
print(f"Renaming corrupted archive to {corrupted_filename}")
return False
# A little trick to rename tool directories so they don't contain version number
rename_to = re.match(r"^([a-z][^\-]*\-*)+", dirname).group(0).strip("-")
if rename_to == dirname and dirname.startswith("esp32-arduino-libs-"):
rename_to = "esp32-arduino-libs"
elif rename_to == dirname and dirname.startswith("esptool-"):
rename_to = "esptool"
if not force_extract:
if is_latest_version(destination, dirname, rename_to, cfile, checksum):
if verify_files(filename, destination, rename_to):
print(" Files ok. Skipping Extraction")
return True
print(" Extracting archive...")
else:
print(" Forcing extraction")
if os.path.isdir(os.path.join(destination, rename_to)):
print("Removing existing {0} ...".format(rename_to))
shutil.rmtree(os.path.join(destination, rename_to), ignore_errors=True)
if filename.endswith("tar.gz"):
if not cfile:
cfile = tarfile.open(filename, "r:gz")
cfile.extractall(destination, filter="fully_trusted")
elif filename.endswith("tar.xz"):
if not cfile:
cfile = tarfile.open(filename, "r:xz")
cfile.extractall(destination, filter="fully_trusted")
elif filename.endswith("zip"):
if not cfile:
cfile = zipfile.ZipFile(filename)
cfile.extractall(destination)
else:
raise NotImplementedError("Unsupported archive type")
if sys.platform != 'win32' and filename.endswith('zip') and isinstance(cfile, zipfile.ZipFile):
for file_info in cfile.infolist():
extracted_file = os.path.join(destination, file_info.filename)
extracted_permissions = file_info.external_attr >> 16 & 0o777 # Extract Unix permissions
if os.path.exists(extracted_file):
os.chmod(extracted_file, extracted_permissions)
if rename_to != dirname:
print("Renaming {0} to {1} ...".format(dirname, rename_to))
shutil.move(dirname, rename_to)
# Add execute permission to esptool on non-Windows platforms
if rename_to.startswith("esptool") and "CYGWIN_NT" not in sys_name and "Windows" not in sys_name:
st = os.stat(os.path.join(destination, rename_to, "esptool"))
os.chmod(os.path.join(destination, rename_to, "esptool"), st.st_mode | 0o111)
with open(os.path.join(destination, rename_to, ".package_checksum"), "w") as f:
f.write(checksum)
if verify_files(filename, destination, rename_to):
print(" Files extracted successfully.")
return True
else:
print(" Failed to extract files.")
return False
def splittype(url: str) -> Tuple[Optional[str], str]:
"""
Splits given url into its type (e.g. https, file) and the rest.
"""
match = re.match('([^/:]+):(.*)', url, re.DOTALL)
if match:
scheme, data = match.groups()
return scheme.lower(), data
return None, url
def urlretrieve_ctx(url: str,
filename: str,
reporthook: Optional[Callable[[int, int, int], None]]=None,
data: Optional[bytes]=None,
context: Optional[SSLContext]=None) -> Tuple[str, addinfourl]:
"""
Retrieve data from given URL. An alternative version of urlretrieve which takes SSL context as an argument.
"""
url_type, path = splittype(url)
# urlopen doesn't have context argument in Python <=2.7.9
extra_urlopen_args = {}
if context:
extra_urlopen_args['context'] = context
with contextlib.closing(urlopen(url, data, **extra_urlopen_args)) as fp: # type: ignore
headers = fp.info()
# Just return the local path and the "headers" for file://
# URLs. No sense in performing a copy unless requested.
if url_type == 'file' and not filename:
return os.path.normpath(path), headers
# Handle temporary file setup.
tfp = open(filename, 'wb')
with tfp:
result = filename, headers
bs = 1024 * 8
size = int(headers.get('content-length', -1))
read = 0
blocknum = 0
if reporthook:
reporthook(blocknum, bs, size)
while True:
block = fp.read(bs)
if not block:
break
read += len(block)
tfp.write(block)
blocknum += 1
if reporthook:
reporthook(blocknum, bs, size)
if size >= 0 and read < size:
raise ContentTooShortError(
'retrieval incomplete: got only %i out of %i bytes'
% (read, size), result)
return result
def get_tool(tool, force_download, force_extract):
sys_name = platform.system()
archive_name = tool["archiveFileName"]
checksum = tool["checksum"][8:]
local_path = dist_dir + archive_name
url = tool["url"]
start_time = time.time()
print("")
if not os.path.isfile(local_path) or force_download:
if verbose:
print("Downloading '" + archive_name + "' to '" + local_path + "'")
else:
print("Downloading '" + archive_name + "' ...")
sys.stdout.flush()
try:
for site, cert in DL_CERT_DICT.items():
if site in url:
ctx = ssl.create_default_context()
ctx.load_verify_locations(cadata=cert)
break
else:
ctx = None
urlretrieve_ctx(url, local_path, report_progress, context=ctx)
except Exception as e:
print(f"Failed to download {archive_name}: {e}")
return False
finally:
sys.stdout.flush()
else:
print("Tool {0} already downloaded".format(archive_name))
sys.stdout.flush()
if sha256sum(local_path) != checksum:
print("Checksum mismatch for {0}".format(archive_name))
return False
return unpack(local_path, ".", force_extract, checksum)
def load_tools_list(filename, platform):
tools_info = json.load(open(filename))["packages"][0]["tools"]
tools_to_download = []
for t in tools_info:
if platform == "x86_64-mingw32":
if "i686-mingw32" not in [p["host"] for p in t["systems"]]:
raise Exception("Windows x64 requires both i686-mingw32 and x86_64-mingw32 tools")
tool_platform = [p for p in t["systems"] if p["host"] == platform]
if len(tool_platform) == 0:
# Fallback to x86 on Apple ARM
if platform == "arm64-apple-darwin":
tool_platform = [p for p in t["systems"] if p["host"] == "x86_64-apple-darwin"]
if len(tool_platform) == 0:
continue
# Fallback to 32bit on 64bit x86 Windows
elif platform == "x86_64-mingw32":
tool_platform = [p for p in t["systems"] if p["host"] == "i686-mingw32"]
if len(tool_platform) == 0:
continue
else:
if verbose:
print(f"Tool {t['name']} is not available for platform {platform}")
continue
tools_to_download.append(tool_platform[0])
return tools_to_download
def identify_platform():
arduino_platform_names = {
"Darwin": {32: "i386-apple-darwin", 64: "x86_64-apple-darwin"},
"DarwinARM": {32: "arm64-apple-darwin", 64: "arm64-apple-darwin"},
"Linux": {32: "i686-pc-linux-gnu", 64: "x86_64-pc-linux-gnu"},
"LinuxARM": {32: "arm-linux-gnueabihf", 64: "aarch64-linux-gnu"},
"Windows": {32: "i686-mingw32", 64: "x86_64-mingw32"},
}
bits = 32
if sys.maxsize > 2**32:
bits = 64
sys_name = platform.system()
sys_platform = platform.platform()
if "Darwin" in sys_name and (sys_platform.find("arm") > 0 or sys_platform.find("arm64") > 0):
sys_name = "DarwinARM"
if "Linux" in sys_name and (sys_platform.find("arm") > 0 or sys_platform.find("aarch64") > 0):
sys_name = "LinuxARM"
if "CYGWIN_NT" in sys_name:
sys_name = "Windows"
print("System: %s, Bits: %d, Info: %s" % (sys_name, bits, sys_platform))
return arduino_platform_names[sys_name][bits]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Download and extract tools")
parser.add_argument("-v", "--verbose", action="store_true", required=False, help="Print verbose output")
parser.add_argument("-d", "--force_download", action="store_true", required=False, help="Force download of tools")
parser.add_argument("-e", "--force_extract", action="store_true", required=False, help="Force extraction of tools")
parser.add_argument(
"-f", "--force_all", action="store_true", required=False, help="Force download and extraction of tools"
)
parser.add_argument("-t", "--test", action="store_true", required=False, help=argparse.SUPPRESS)
args = parser.parse_args()
verbose = args.verbose
force_download = args.force_download
force_extract = args.force_extract
force_all = args.force_all
is_test = args.test
# Set current directory to the script location
if getattr(sys, "frozen", False):
os.chdir(os.path.dirname(sys.executable))
else:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
if is_test and (force_download or force_extract or force_all):
print("Cannot combine test (-t) and forced execution (-d | -e | -f)")
parser.print_help(sys.stderr)
sys.exit(1)
if is_test:
print("Test run!")
if force_all:
force_download = True
force_extract = True
identified_platform = identify_platform()
print("Platform: {0}".format(identified_platform))
tools_to_download = load_tools_list(
current_dir + "/../package/package_esp32_index.template.json", identified_platform
)
mkdir_p(dist_dir)
print("\nDownloading and extracting tools...")
for tool in tools_to_download:
if is_test:
print("Would install: {0}".format(tool["archiveFileName"]))
else:
if not get_tool(tool, force_download, force_extract):
if verbose:
print(f"Tool {tool['archiveFileName']} was corrupted. Re-downloading...\n")
if not get_tool(tool, True, force_extract):
print(f"Tool {tool['archiveFileName']} was corrupted, but re-downloading did not help!\n")
sys.exit(1)
print("\nPlatform Tools Installed")