Takuto Ikuta | b7bebb0 | 2021-12-15 11:19:52 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 2 | # |
| 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 | """ |
| 7 | Used to download a pre-built version of emscripten for running e2e tests |
| 8 | testing DevTools with emscripten generated Wasm binaries. |
| 9 | """ |
| 10 | |
| 11 | import argparse |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 12 | import os |
Takuto Ikuta | b7bebb0 | 2021-12-15 11:19:52 | [diff] [blame] | 13 | import platform |
Nikolay Vitkov | 7951c23 | 2025-02-07 14:02:18 | [diff] [blame] | 14 | import shutil |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 15 | import sys |
| 16 | import tarfile |
Takuto Ikuta | b7bebb0 | 2021-12-15 11:19:52 | [diff] [blame] | 17 | import urllib.request |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 18 | import zipfile |
| 19 | |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 20 | BS = 8192 |
| 21 | STAMP_FILE = 'build-revision' |
Michael Achenbach | 431cb39 | 2023-11-17 13:23:27 | [diff] [blame] | 22 | DOWNLOAD_URL = "https://storage.googleapis.com/webassembly/emscripten-releases-builds/%s/%s/wasm-binaries%s.%s" |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 23 | |
| 24 | |
Nikolay Vitkov | 7951c23 | 2025-02-07 14:02:18 | [diff] [blame] | 25 | # Returns None when files was not found |
| 26 | # Return boolean if file found depending on version is the same |
Michael Achenbach | 431cb39 | 2023-11-17 13:23:27 | [diff] [blame] | 27 | def check_stamp_file(options, url): |
Philip Pfaffe | 8e89944 | 2020-10-20 09:18:35 | [diff] [blame] | 28 | file_name = os.path.join(options.dest, STAMP_FILE) |
| 29 | if not os.path.isfile(file_name): |
Nikolay Vitkov | 7951c23 | 2025-02-07 14:02:18 | [diff] [blame] | 30 | return None |
Philip Pfaffe | 8e89944 | 2020-10-20 09:18:35 | [diff] [blame] | 31 | with open(file_name) as f: |
Michael Achenbach | 431cb39 | 2023-11-17 13:23:27 | [diff] [blame] | 32 | return url == f.read().strip() |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 33 | |
| 34 | |
Michael Achenbach | 431cb39 | 2023-11-17 13:23:27 | [diff] [blame] | 35 | def write_stamp_file(options, url): |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 36 | with open(os.path.join(options.dest, STAMP_FILE), 'w') as f: |
Michael Achenbach | 431cb39 | 2023-11-17 13:23:27 | [diff] [blame] | 37 | return f.write(url) |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def 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 | |
| 46 | def 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 Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 55 | os_name = { |
| 56 | 'Linux': 'linux', |
| 57 | 'Windows': 'win', |
| 58 | 'Darwin': 'mac' |
| 59 | }[platform.system()] |
| 60 | |
Michael Achenbach | 431cb39 | 2023-11-17 13:23:27 | [diff] [blame] | 61 | 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 Vitkov | 7951c23 | 2025-02-07 14:02:18 | [diff] [blame] | 70 | build_revision_same = check_stamp_file(options, url) |
| 71 | if build_revision_same: |
Michael Achenbach | 431cb39 | 2023-11-17 13:23:27 | [diff] [blame] | 72 | return 0 |
Nikolay Vitkov | 7951c23 | 2025-02-07 14:02:18 | [diff] [blame] | 73 | 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 Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 78 | |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 79 | try: |
Takuto Ikuta | b7bebb0 | 2021-12-15 11:19:52 | [diff] [blame] | 80 | filename, _ = urllib.request.urlretrieve(url) |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 81 | |
| 82 | unzip(os_name, filename, options.dest) |
| 83 | |
Michael Achenbach | 431cb39 | 2023-11-17 13:23:27 | [diff] [blame] | 84 | write_stamp_file(options, url) |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 85 | except Exception as e: |
| 86 | sys.stderr.write('Error Downloading URL "{url}": {e}\n'.format(url=url, |
| 87 | e=e)) |
| 88 | return 1 |
Takuto Ikuta | b7bebb0 | 2021-12-15 11:19:52 | [diff] [blame] | 89 | finally: |
| 90 | urllib.request.urlcleanup() |
Philip Pfaffe | 0237609 | 2020-09-29 11:10:05 | [diff] [blame] | 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | sys.exit(script_main(sys.argv[1:])) |