blob: 506e1fe032a910a4510351ae6a20bb9bb7d2d244 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 08:59:031#!/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"""
7Used to download a pre-built version of Chrome for running unit tests
8"""
9
10import argparse
11import os
12import shutil
Liviu Rau0e7703e2020-03-02 15:33:3613import stat
Yang Guo4fd355c2019-09-19 08:59:0314import sys
15import urllib
16import zipfile
17
18
19def 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 Rau0e7703e2020-03-02 15:33:3628def 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 Raud7c07132020-03-03 10:52:0133 print("Retrying due to access error ...")
Liviu Rau0e7703e2020-03-02 15:33:3634 os.chmod(path, stat.S_IWUSR)
35 func(path)
36 else:
37 raise exc
38
Yang Guo4fd355c2019-09-19 08:59:0339def 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 Rau0e7703e2020-03-02 15:33:3652 shutil.rmtree(options.target, ignore_errors=False, onerror=handleAccessDeniedOnWindows)
Yang Guo4fd355c2019-09-19 08:59:0353
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 Guo0802bf52019-11-11 12:07:2058 # 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 Guo4fd355c2019-09-19 08:59:0365 with open(BUILD_NUMBER_FILE, 'w') as file:
66 file.write(options.build_number)
67
68
69if __name__ == '__main__':
70 OPTIONS = parse_options(sys.argv[1:])
71 download_and_extract(OPTIONS)