blob: 97718421005fbdaf8d136fd4a305b1a3914eb16d [file] [log] [blame]
Takuto Ikutab7bebb02021-12-15 11:19:521#!/usr/bin/env python3
Philip Pfaffe02376092020-09-29 11:10:052#
3# Copyright 2020 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6"""
7Used to download a pre-built version of emscripten for running e2e tests
8testing DevTools with emscripten generated Wasm binaries.
9"""
10
11import argparse
Philip Pfaffe02376092020-09-29 11:10:0512import os
Takuto Ikutab7bebb02021-12-15 11:19:5213import platform
Nikolay Vitkov7951c232025-02-07 14:02:1814import shutil
Philip Pfaffe02376092020-09-29 11:10:0515import sys
16import tarfile
Takuto Ikutab7bebb02021-12-15 11:19:5217import urllib.request
Philip Pfaffe02376092020-09-29 11:10:0518import zipfile
19
Philip Pfaffe02376092020-09-29 11:10:0520BS = 8192
21STAMP_FILE = 'build-revision'
Michael Achenbach431cb392023-11-17 13:23:2722DOWNLOAD_URL = "https://storage.googleapis.com/webassembly/emscripten-releases-builds/%s/%s/wasm-binaries%s.%s"
Philip Pfaffe02376092020-09-29 11:10:0523
24
Nikolay Vitkov7951c232025-02-07 14:02:1825# Returns None when files was not found
26# Return boolean if file found depending on version is the same
Michael Achenbach431cb392023-11-17 13:23:2727def check_stamp_file(options, url):
Philip Pfaffe8e899442020-10-20 09:18:3528 file_name = os.path.join(options.dest, STAMP_FILE)
29 if not os.path.isfile(file_name):
Nikolay Vitkov7951c232025-02-07 14:02:1830 return None
Philip Pfaffe8e899442020-10-20 09:18:3531 with open(file_name) as f:
Michael Achenbach431cb392023-11-17 13:23:2732 return url == f.read().strip()
Philip Pfaffe02376092020-09-29 11:10:0533
34
Michael Achenbach431cb392023-11-17 13:23:2735def write_stamp_file(options, url):
Philip Pfaffe02376092020-09-29 11:10:0536 with open(os.path.join(options.dest, STAMP_FILE), 'w') as f:
Michael Achenbach431cb392023-11-17 13:23:2737 return f.write(url)
Philip Pfaffe02376092020-09-29 11:10:0538
39
40def unzip(os_name, file, dest):
41 is_zip = os_name == 'win'
42 z = zipfile.ZipFile(file) if is_zip else tarfile.open(file, 'r:bz2')
43 z.extractall(path=dest)
44
45
46def script_main(args):
47 parser = argparse.ArgumentParser(description='Download Emscripten')
48 parser.add_argument('tag', help='emscripten tag')
49 parser.add_argument('dest', help='destination directory')
50 options = parser.parse_args(args)
51
52 if not os.path.isdir(options.dest):
53 os.makedirs(options.dest)
54
Philip Pfaffe02376092020-09-29 11:10:0555 os_name = {
56 'Linux': 'linux',
57 'Windows': 'win',
58 'Darwin': 'mac'
59 }[platform.system()]
60
Michael Achenbach431cb392023-11-17 13:23:2761 arch_suffix = ''
62 host_arch = platform.machine().lower()
63 if host_arch == 'arm64' or host_arch.startswith('aarch64'):
64 arch_suffix = '-arm64'
65
66 file_extension = 'zip' if os_name == 'win' else 'tbz2'
67
68 url = DOWNLOAD_URL % (os_name, options.tag, arch_suffix, file_extension)
69
Nikolay Vitkov7951c232025-02-07 14:02:1870 build_revision_same = check_stamp_file(options, url)
71 if build_revision_same:
Michael Achenbach431cb392023-11-17 13:23:2772 return 0
Nikolay Vitkov7951c232025-02-07 14:02:1873 elif build_revision_same is False:
74 try:
75 shutil.rmtree(os.path.join(options.dest, 'install'))
76 except Exception as e:
77 sys.stderr.write('Error while remove old version: {e}'.format(e=e))
Philip Pfaffe02376092020-09-29 11:10:0578
Philip Pfaffe02376092020-09-29 11:10:0579 try:
Takuto Ikutab7bebb02021-12-15 11:19:5280 filename, _ = urllib.request.urlretrieve(url)
Philip Pfaffe02376092020-09-29 11:10:0581
82 unzip(os_name, filename, options.dest)
83
Michael Achenbach431cb392023-11-17 13:23:2784 write_stamp_file(options, url)
Philip Pfaffe02376092020-09-29 11:10:0585 except Exception as e:
86 sys.stderr.write('Error Downloading URL "{url}": {e}\n'.format(url=url,
87 e=e))
88 return 1
Takuto Ikutab7bebb02021-12-15 11:19:5289 finally:
90 urllib.request.urlcleanup()
Philip Pfaffe02376092020-09-29 11:10:0591
92
93if __name__ == '__main__':
94 sys.exit(script_main(sys.argv[1:]))