win-nasm: Use gstreamer mirror as primary source
[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 import urllib.error
10
11 # Disable certificate checking because it always fails on Windows
12 # We verify the checksum anyway.
13 ctx = ssl.create_default_context()
14 ctx.check_hostname = False
15 ctx.verify_mode = ssl.CERT_NONE
16
17 BASENAME = 'nasm-{}-{}.zip'
18 UPSTREAM_URL = 'https://www.nasm.us/pub/nasm/releasebuilds/{}/{}/{}'
19 GSTREAMER_URL = 'https://gstreamer.freedesktop.org/src/mirror/{}'
20
21 version = sys.argv[1]
22 arch = 'win64' if sys.argv[2] == 'x86_64' else 'win32'
23 zip_sha256 = sys.argv[3]
24 source_dir = os.path.join(os.environ['MESON_SOURCE_ROOT'], os.environ['MESON_SUBDIR'])
25 dest = BASENAME.format(version, arch)
26 dest_path = os.path.join(source_dir, dest)
27
28 def get_sha256(zipf):
29     hasher = hashlib.sha256()
30     with open(zipf, 'rb') as f:
31         hasher.update(f.read())
32     return hasher.hexdigest()
33
34 if os.path.isfile(dest_path):
35     found_sha256 = get_sha256(dest_path)
36     if found_sha256 == zip_sha256:
37         print('{} already downloaded'.format(dest))
38         sys.exit(0)
39     else:
40         print('{} checksum mismatch, redownloading'.format(dest))
41
42 for url in (GSTREAMER_URL.format(dest), UPSTREAM_URL.format(version, arch, dest)):
43     print('Downloading {} to {}'.format(url, dest))
44     try:
45         with open(dest_path, 'wb') as d:
46             f = urllib.request.urlopen(url, context=ctx)
47             d.write(f.read())
48         break
49     except urllib.error.URLError as ex:
50         print(ex)
51         print('Failed to download from {!r}, trying mirror...'.format(url))
52         continue
53 else:
54     curdir = os.path.dirname(sys.argv[0])
55     print('Couldn\'t download {!r}! Try downloading it manually and '
56           'placing it into {!r}'.format(dest, curdir))
57
58 found_sha256 = get_sha256(dest_path)
59 if found_sha256 != zip_sha256:
60     print('SHA256 of downloaded file {} was {} instead of {}'
61           ''.format(dest, found_sha256, zip_sha256))
62     sys.exit(1)
63
64 print('Extracting {}'.format(dest))
65 zf = zipfile.ZipFile(dest_path, "r")
66 zf.extractall(path=source_dir)