Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [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 |
| 13 | import sys |
| 14 | import urllib |
| 15 | import zipfile |
| 16 | |
| 17 | |
| 18 | def parse_options(cli_args): |
| 19 | parser = argparse.ArgumentParser(description='Download Chromium') |
| 20 | parser.add_argument('url', help='download URL') |
| 21 | parser.add_argument('target', help='target directory') |
| 22 | parser.add_argument('path_to_binary', help='path to binary inside of the zip archive') |
| 23 | parser.add_argument('build_number', help='build number to find out whether we need to re-download') |
| 24 | return parser.parse_args(cli_args) |
| 25 | |
| 26 | |
| 27 | def download_and_extract(options): |
| 28 | BUILD_NUMBER_FILE = os.path.join(options.target, 'build_number') |
| 29 | EXPECTED_BINARY = os.path.join(options.target, options.path_to_binary) |
| 30 | # Check whether we already downloaded pre-built Chromium of right build number |
| 31 | if os.path.exists(BUILD_NUMBER_FILE): |
| 32 | with open(BUILD_NUMBER_FILE) as file: |
| 33 | build_number = file.read().strip() |
| 34 | if build_number == options.build_number: |
| 35 | assert os.path.exists(EXPECTED_BINARY) |
| 36 | return |
| 37 | |
| 38 | # Remove previous download |
| 39 | if os.path.exists(options.target): |
| 40 | shutil.rmtree(options.target) |
| 41 | |
| 42 | # Download again and save build number |
| 43 | filehandle, headers = urllib.urlretrieve(options.url) |
| 44 | zip_file = zipfile.ZipFile(filehandle, 'r') |
| 45 | zip_file.extractall(path=options.target) |
| 46 | os.chmod(EXPECTED_BINARY, 0o555) |
| 47 | with open(BUILD_NUMBER_FILE, 'w') as file: |
| 48 | file.write(options.build_number) |
| 49 | |
| 50 | |
| 51 | if __name__ == '__main__': |
| 52 | OPTIONS = parse_options(sys.argv[1:]) |
| 53 | download_and_extract(OPTIONS) |