forked from MHeasell/rwe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-msvc-libs.py
79 lines (58 loc) · 1.75 KB
/
fetch-msvc-libs.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
#!/usr/bin/env python3
"""Downloads the MSVC libs bundle and extracts it to libs/"""
# configuration
bundle_name = "rwe-msvc-libs-4.zip"
sha256hash_hex = "6e5516e1eff1fa1db9ac093f49cd8295d2dbcf441bdff821b2695996a729175e"
# code
import binascii
import hashlib
import os
import shutil
import sys
import urllib.request
from zipfile import ZipFile
def extract_file(filename):
with ZipFile(filename) as zipobj:
zipobj.extractall()
def download_file(url, filename):
with urllib.request.urlopen(url) as response, open(filename, 'wb') as fh:
shutil.copyfileobj(response, fh)
def check_hash(filename, expected_hash):
actual_hash = get_hash(filename)
return expected_hash == actual_hash
def get_hash(filename):
h = hashlib.sha256()
with open(filename, 'rb') as f:
while True:
buf = f.read(65536)
if not buf:
break
h.update(buf)
return h.digest()
def log(msg):
print(msg, file=sys.stderr)
sys.stderr.flush()
if __name__ == "__main__":
os.chdir("libs")
sha256hash = binascii.unhexlify(sha256hash_hex)
bundle_url = "https://rwe.michaelheasell.com/bundles/" + bundle_name
log("Deleting old files...")
try:
os.remove(bundle_name)
except FileNotFoundError:
pass
try:
shutil.rmtree("_msvc")
except FileNotFoundError:
pass
log("Downloading library bundle...")
download_file(bundle_url, bundle_name)
log("Verifying file hash...")
if not check_hash(bundle_name, sha256hash):
log("Downloaded file hash did not match", file=sys.stderr)
sys.exit(1)
log("Extracting bundle...")
extract_file(bundle_name)
log("Deleting bundle file...")
os.remove(bundle_name)
log("Done!")