vkmemory: flush whole size
[platform/upstream/gstreamer.git] / subprojects / win-flex-bison-binaries / 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 = 'win_flex_bison-{}.zip'
18 UPSTREAM_URL = 'https://github.com/lexxmark/winflexbison/releases/download/v{}/{}'
19 GSTREAMER_URL = 'https://gstreamer.freedesktop.org/src/mirror/{}'
20
21 version = sys.argv[1]
22 zip_sha256 = sys.argv[2]
23 source_dir = os.path.join(os.environ['MESON_SOURCE_ROOT'], os.environ['MESON_SUBDIR'])
24 dest = BASENAME.format(version)
25 dest_path = os.path.join(source_dir, dest)
26
27 def get_sha256(zipf):
28     hasher = hashlib.sha256()
29     with open(zipf, 'rb') as f:
30         hasher.update(f.read())
31     return hasher.hexdigest()
32
33 if os.path.isfile(dest_path):
34     found_sha256 = get_sha256(dest_path)
35     if found_sha256 == zip_sha256:
36         print('{} already downloaded'.format(dest))
37         sys.exit(0)
38     else:
39         print('{} checksum mismatch, redownloading'.format(dest))
40
41 for url in (GSTREAMER_URL.format(dest), UPSTREAM_URL.format(version, dest)):
42     print('Downloading {} to {}'.format(url, dest))
43     try:
44         with open(dest_path, 'wb') as d:
45             f = urllib.request.urlopen(url, context=ctx)
46             d.write(f.read())
47         break
48     except urllib.error.URLError as ex:
49         print(ex)
50         print('Failed to download from {!r}, trying mirror...'.format(url))
51         continue
52 else:
53     curdir = os.path.dirname(sys.argv[0])
54     print('Couldn\'t download {!r}! Try downloading it manually and '
55           'placing it into {!r}'.format(dest, curdir))
56
57 found_sha256 = get_sha256(dest_path)
58 if found_sha256 != zip_sha256:
59     print('SHA256 of downloaded file {} was {} instead of {}'
60           ''.format(dest, found_sha256, zip_sha256))
61     sys.exit(1)
62
63 print('Extracting {}'.format(dest))
64 zf = zipfile.ZipFile(dest_path, "r")
65 zf.extractall(path=source_dir)