blob: ae1c51924579b07778e470b37df6c48fdce90589 [file] [log] [blame]
Tim van der Lippe8e12f922020-12-03 16:33:501#!/usr/bin/env vpython
Yang Guo4fd355c2019-09-19 08:59:032#
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"""
7Used to download a pre-built version of Chrome for running unit tests
8"""
9
10import argparse
Yang Guo08d68112020-05-12 10:08:4911import io
Yang Guo4fd355c2019-09-19 08:59:0312import os
13import shutil
Liviu Rau0e7703e2020-03-02 15:33:3614import stat
Yang Guo08d68112020-05-12 10:08:4915import subprocess
Yang Guo4fd355c2019-09-19 08:59:0316import sys
17import urllib
18import zipfile
19
20
21def 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 Rau0e7703e2020-03-02 15:33:3630def 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 Raud7c07132020-03-03 10:52:0135 print("Retrying due to access error ...")
Liviu Rau0e7703e2020-03-02 15:33:3636 os.chmod(path, stat.S_IWUSR)
37 func(path)
38 else:
39 raise exc
40
Yang Guo4fd355c2019-09-19 08:59:0341def 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 Rau0e7703e2020-03-02 15:33:3654 shutil.rmtree(options.target, ignore_errors=False, onerror=handleAccessDeniedOnWindows)
Yang Guo4fd355c2019-09-19 08:59:0355
56 # Download again and save build number
Yang Guo08d68112020-05-12 10:08:4957 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 Guo4fd355c2019-09-19 08:59:0364 zip_file = zipfile.ZipFile(filehandle, 'r')
65 zip_file.extractall(path=options.target)
Yang Guo0802bf52019-11-11 12:07:2066 # 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 Guo4fd355c2019-09-19 08:59:0373 with open(BUILD_NUMBER_FILE, 'w') as file:
74 file.write(options.build_number)
75
76
77if __name__ == '__main__':
78 OPTIONS = parse_options(sys.argv[1:])
79 download_and_extract(OPTIONS)