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