New subproject win-nasm to provide nasm on Windows
[platform/upstream/gstreamer.git] / subprojects / win-nasm / download-binary.py
1 #!/usr/bin/env python3
2
3 import os
4 import sys
5 import ssl
6 import zipfile
7 import hashlib
8 import urllib.request
9
10 # Disable certificate checking because it always fails on Windows
11 # We verify the checksum anyway.
12 ctx = ssl.create_default_context()
13 ctx.check_hostname = False
14 ctx.verify_mode = ssl.CERT_NONE
15
16 base_url = 'https://www.nasm.us/pub/nasm/releasebuilds/{0}/{1}/nasm-{0}-{1}.zip'
17 arch = 'win64' if sys.argv[2] == 'x86_64' else 'win32'
18 url = base_url.format(sys.argv[1], arch)
19 zip_sha256 = sys.argv[3]
20 source_dir = os.path.join(os.environ['MESON_SOURCE_ROOT'], os.environ['MESON_SUBDIR'])
21 dest = os.path.basename(url)
22 dest_path = os.path.join(source_dir, dest)
23
24 def get_sha256(zipf):
25     hasher = hashlib.sha256()
26     with open(zipf, 'rb') as f:
27         hasher.update(f.read())
28     return hasher.hexdigest()
29
30 if os.path.isfile(dest_path):
31     found_sha256 = get_sha256(dest_path)
32     if found_sha256 == zip_sha256:
33         print('{} already downloaded'.format(dest))
34         sys.exit(0)
35     else:
36         print('{} checksum mismatch, redownloading'.format(dest))
37
38 print('Downloading {} to {}'.format(url, dest))
39 with open(dest_path, 'wb') as d:
40     f = urllib.request.urlopen(url, context=ctx)
41     d.write(f.read())
42
43 found_sha256 = get_sha256(dest_path)
44 if found_sha256 != zip_sha256:
45     print('SHA256 of downloaded file {} was {} instead of {}'
46           ''.format(dest, found_sha256, zip_sha256))
47     sys.exit(1)
48
49 print('Extracting {}'.format(dest))
50 zf = zipfile.ZipFile(dest_path, "r")
51 zf.extractall(path=source_dir)