Tim van der Lippe | 8e12f92 | 2020-12-03 16:33:50 | [diff] [blame] | 1 | #!/usr/bin/env vpython |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2019 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 Chrome for running unit tests |
| 8 | """ |
| 9 | |
| 10 | import argparse |
Yang Guo | 08d6811 | 2020-05-12 10:08:49 | [diff] [blame] | 11 | import io |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 12 | import os |
| 13 | import shutil |
Liviu Rau | 0e7703e | 2020-03-02 15:33:36 | [diff] [blame] | 14 | import stat |
Yang Guo | 08d6811 | 2020-05-12 10:08:49 | [diff] [blame] | 15 | import subprocess |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 16 | import sys |
| 17 | import urllib |
| 18 | import zipfile |
| 19 | |
| 20 | |
| 21 | def parse_options(cli_args): |
| 22 | parser = argparse.ArgumentParser(description='Download Chromium') |
| 23 | parser.add_argument('url', help='download URL') |
| 24 | parser.add_argument('target', help='target directory') |
| 25 | parser.add_argument('path_to_binary', help='path to binary inside of the zip archive') |
| 26 | parser.add_argument('build_number', help='build number to find out whether we need to re-download') |
| 27 | return parser.parse_args(cli_args) |
| 28 | |
| 29 | |
Liviu Rau | 0e7703e | 2020-03-02 15:33:36 | [diff] [blame] | 30 | def handleAccessDeniedOnWindows(func, path, exc): |
| 31 | if not os.name == 'nt': |
| 32 | raise exc |
| 33 | if not os.access(path, os.W_OK): |
| 34 | # Is the error an access error ? |
Liviu Rau | d7c0713 | 2020-03-03 10:52:01 | [diff] [blame] | 35 | print("Retrying due to access error ...") |
Liviu Rau | 0e7703e | 2020-03-02 15:33:36 | [diff] [blame] | 36 | os.chmod(path, stat.S_IWUSR) |
| 37 | func(path) |
| 38 | else: |
| 39 | raise exc |
| 40 | |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 41 | def download_and_extract(options): |
| 42 | BUILD_NUMBER_FILE = os.path.join(options.target, 'build_number') |
| 43 | EXPECTED_BINARY = os.path.join(options.target, options.path_to_binary) |
| 44 | # Check whether we already downloaded pre-built Chromium of right build number |
| 45 | if os.path.exists(BUILD_NUMBER_FILE): |
| 46 | with open(BUILD_NUMBER_FILE) as file: |
| 47 | build_number = file.read().strip() |
| 48 | if build_number == options.build_number: |
| 49 | assert os.path.exists(EXPECTED_BINARY) |
| 50 | return |
| 51 | |
| 52 | # Remove previous download |
| 53 | if os.path.exists(options.target): |
Liviu Rau | 0e7703e | 2020-03-02 15:33:36 | [diff] [blame] | 54 | shutil.rmtree(options.target, ignore_errors=False, onerror=handleAccessDeniedOnWindows) |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 55 | |
| 56 | # Download again and save build number |
Yang Guo | 08d6811 | 2020-05-12 10:08:49 | [diff] [blame] | 57 | try: |
| 58 | filehandle, headers = urllib.urlretrieve(options.url) |
| 59 | except: |
| 60 | print("Using curl as fallback. You should probably update OpenSSL.") |
| 61 | filehandle = io.BytesIO( |
| 62 | subprocess.check_output( |
| 63 | ['curl', '--output', '-', '-sS', options.url])) |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 64 | zip_file = zipfile.ZipFile(filehandle, 'r') |
| 65 | zip_file.extractall(path=options.target) |
Yang Guo | 0802bf5 | 2019-11-11 12:07:20 | [diff] [blame] | 66 | # Fix permissions. Do this recursively is necessary for MacOS bundles. |
| 67 | if os.path.isfile(EXPECTED_BINARY): |
| 68 | os.chmod(EXPECTED_BINARY, 0o555) |
| 69 | else: |
| 70 | for root, dirs, files in os.walk(EXPECTED_BINARY): |
| 71 | for f in files: |
| 72 | os.chmod(os.path.join(root, f), 0o555) |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 73 | with open(BUILD_NUMBER_FILE, 'w') as file: |
| 74 | file.write(options.build_number) |
| 75 | |
| 76 | |
| 77 | if __name__ == '__main__': |
| 78 | OPTIONS = parse_options(sys.argv[1:]) |
| 79 | download_and_extract(OPTIONS) |