Merging gst-build
[platform/upstream/gstreamer.git] / subprojects / macos-bison-binary / download-binary.py
1 #!/usr/bin/env python3
2
3 import os
4 import sys
5 import ssl
6 import tarfile
7 import hashlib
8 import urllib.request
9 import urllib.error
10
11 # Disable certificate checking because it requires custom Python setup on macOS
12 ctx = ssl.create_default_context()
13 ctx.check_hostname = False
14 ctx.verify_mode = ssl.CERT_NONE
15
16 EXTRACTDIR = 'bison-{}-macos-{}'
17 BASENAME = '{}.tar.bz2'.format(EXTRACTDIR)
18 GSTREAMER_URL = 'https://gstreamer.freedesktop.org/src/mirror/{}'
19
20 version = sys.argv[1]
21 arch = sys.argv[2]
22 tar_sha256 = sys.argv[3]
23 source_dir = os.path.join(os.environ['MESON_SOURCE_ROOT'], os.environ['MESON_SUBDIR'])
24 dest = BASENAME.format(version, arch)
25 dest_path = os.path.join(source_dir, dest)
26 extract_path = EXTRACTDIR.format(version, arch)
27
28 def get_sha256(tarf):
29     hasher = hashlib.sha256()
30     with open(tarf, 'rb') as f:
31         hasher.update(f.read())
32     return hasher.hexdigest()
33
34 def download():
35     for url in (GSTREAMER_URL.format(dest),):
36         print('Downloading {} to {}'.format(url, dest), file=sys.stderr)
37         try:
38             with open(dest_path, 'wb') as d:
39                 f = urllib.request.urlopen(url, context=ctx)
40                 d.write(f.read())
41             break
42         except urllib.error.URLError as ex:
43             print(ex, file=sys.stderr)
44             print('Failed to download from {!r}, trying mirror...'.format(url), file=sys.stderr)
45             continue
46     else:
47         curdir = os.path.dirname(sys.argv[0])
48         print('Couldn\'t download {!r}! Try downloading it manually and '
49               'placing it into {!r}'.format(dest, curdir), file=sys.stderr)
50
51 def print_extract_dir():
52     'Print the extracted directory name'
53     print(extract_path, end='')
54
55 if os.path.isfile(dest_path):
56     found_sha256 = get_sha256(dest_path)
57     if found_sha256 == tar_sha256:
58         if os.path.isdir(os.path.join(source_dir, extract_path)):
59             print('{} already downloaded and extracted'.format(dest), file=sys.stderr)
60             print_extract_dir()
61             sys.exit(0)
62     else:
63         print('{} checksum mismatch, redownloading'.format(dest), file=sys.stderr)
64         download()
65 else:
66     download()
67
68 found_sha256 = get_sha256(dest_path)
69 if found_sha256 != tar_sha256:
70     print('SHA256 of downloaded file {} was {} instead of {}'
71           ''.format(dest, found_sha256, tar_sha256), file=sys.stderr)
72     sys.exit(1)
73
74 print('Extracting {}'.format(dest), file=sys.stderr)
75 tf = tarfile.open(dest_path, "r")
76 tf.extractall(path=source_dir)
77 print_extract_dir()