2013-06-20 15:23:22 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2018-10-05 22:21:46 +00:00
|
|
|
import argparse
|
2014-05-18 15:03:46 +00:00
|
|
|
import errno
|
2019-12-01 23:23:54 +00:00
|
|
|
import hashlib
|
2018-12-27 11:12:40 +00:00
|
|
|
import json
|
2013-06-20 15:23:22 +00:00
|
|
|
import os
|
|
|
|
|
2018-08-21 19:40:06 +00:00
|
|
|
from lib.config import PLATFORM, get_target_arch
|
|
|
|
from lib.util import add_exec_bit, download, extract_zip, rm_rf, \
|
|
|
|
safe_mkdir, tempdir
|
2013-06-20 15:23:22 +00:00
|
|
|
|
2013-07-01 14:27:14 +00:00
|
|
|
SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
|
2014-04-29 02:03:03 +00:00
|
|
|
|
2018-10-05 22:21:46 +00:00
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Download binaries for Electron build')
|
|
|
|
|
2018-12-27 11:12:40 +00:00
|
|
|
parser.add_argument('--base-url', required=False,
|
|
|
|
help="Base URL for all downloads")
|
2019-12-01 23:23:54 +00:00
|
|
|
parser.add_argument('--force', action='store_true', default=False, required=False)
|
2018-10-05 22:21:46 +00:00
|
|
|
|
|
|
|
return parser.parse_args()
|
2013-06-20 15:23:22 +00:00
|
|
|
|
2018-12-27 11:12:40 +00:00
|
|
|
|
|
|
|
def parse_config():
|
|
|
|
config_path = os.path.join(SOURCE_ROOT, 'script', 'external-binaries.json')
|
|
|
|
with open(config_path, 'r') as config_file:
|
|
|
|
config = json.load(config_file)
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2013-06-20 15:23:22 +00:00
|
|
|
def main():
|
2018-10-05 22:21:46 +00:00
|
|
|
args = parse_args()
|
2018-12-27 11:12:40 +00:00
|
|
|
config = parse_config()
|
2018-10-05 22:21:46 +00:00
|
|
|
|
2018-12-27 11:12:40 +00:00
|
|
|
base_url = args.base_url if args.base_url is not None else config['baseUrl']
|
|
|
|
version = config['version']
|
|
|
|
output_dir = os.path.join(SOURCE_ROOT, 'external_binaries')
|
|
|
|
version_file = os.path.join(output_dir, '.version')
|
2014-05-18 15:42:47 +00:00
|
|
|
|
2019-12-01 23:23:54 +00:00
|
|
|
if (is_updated(version_file, version) and not args.force):
|
2014-05-18 15:42:47 +00:00
|
|
|
return
|
|
|
|
|
2018-12-27 11:12:40 +00:00
|
|
|
rm_rf(output_dir)
|
|
|
|
safe_mkdir(output_dir)
|
|
|
|
|
|
|
|
for binary in config['binaries']:
|
|
|
|
if not binary_should_be_downloaded(binary):
|
|
|
|
continue
|
|
|
|
|
2019-12-01 23:23:54 +00:00
|
|
|
temp_path = download_binary(base_url, version, binary['url'], binary['sha'])
|
2014-07-31 06:12:01 +00:00
|
|
|
|
2018-12-27 11:12:40 +00:00
|
|
|
# We assume that all binaries are in zip archives.
|
|
|
|
extract_zip(temp_path, output_dir)
|
2013-06-20 15:23:22 +00:00
|
|
|
|
2018-12-27 11:12:40 +00:00
|
|
|
# Hack alert. Set exec bit for sccache binaries.
|
|
|
|
# https://bugs.python.org/issue15795
|
|
|
|
if 'sccache' in binary['url']:
|
|
|
|
add_exec_bit_to_sccache_binary(output_dir)
|
2018-08-21 19:40:06 +00:00
|
|
|
|
2014-08-06 15:16:42 +00:00
|
|
|
with open(version_file, 'w') as f:
|
2018-12-27 11:12:40 +00:00
|
|
|
f.write(version)
|
2014-08-06 15:16:42 +00:00
|
|
|
|
2013-06-20 15:23:22 +00:00
|
|
|
|
2014-05-18 15:42:47 +00:00
|
|
|
def is_updated(version_file, version):
|
|
|
|
existing_version = ''
|
|
|
|
try:
|
|
|
|
with open(version_file, 'r') as f:
|
|
|
|
existing_version = f.readline().strip()
|
|
|
|
except IOError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
return existing_version == version
|
|
|
|
|
|
|
|
|
2018-12-27 11:12:40 +00:00
|
|
|
def binary_should_be_downloaded(binary):
|
|
|
|
if 'platform' in binary and binary['platform'] != PLATFORM:
|
|
|
|
return False
|
2013-06-20 15:23:22 +00:00
|
|
|
|
2018-12-27 11:12:40 +00:00
|
|
|
if 'targetArch' in binary and binary['targetArch'] != get_target_arch():
|
|
|
|
return False
|
2013-06-20 15:23:22 +00:00
|
|
|
|
2018-12-27 11:12:40 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-12-01 23:23:54 +00:00
|
|
|
def sha256(file_path):
|
|
|
|
hash_256 = hashlib.sha256()
|
|
|
|
with open(file_path, "rb") as f:
|
|
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
|
|
hash_256.update(chunk)
|
|
|
|
return hash_256.hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
def download_binary(base_url, version, binary_url, sha):
|
2018-12-27 11:12:40 +00:00
|
|
|
full_url = '{0}/{1}/{2}'.format(base_url, version, binary_url)
|
2019-12-01 23:23:54 +00:00
|
|
|
temp_path = download_to_temp_dir(full_url, filename=binary_url, sha=sha)
|
2018-12-27 11:12:40 +00:00
|
|
|
return temp_path
|
|
|
|
|
|
|
|
|
2019-12-01 23:23:54 +00:00
|
|
|
def validate_sha(file_path, sha):
|
|
|
|
downloaded_sha = sha256(file_path)
|
|
|
|
if downloaded_sha != sha:
|
|
|
|
raise Exception("SHA for external binary file {} does not match expected '{}' != '{}'".format(file_path, downloaded_sha, sha))
|
|
|
|
|
|
|
|
|
|
|
|
def download_to_temp_dir(url, filename, sha):
|
2015-04-12 14:10:46 +00:00
|
|
|
download_dir = tempdir(prefix='electron-')
|
2018-12-27 11:12:40 +00:00
|
|
|
file_path = os.path.join(download_dir, filename)
|
|
|
|
download(text='Download ' + filename, url=url, path=file_path)
|
2019-12-01 23:23:54 +00:00
|
|
|
validate_sha(file_path, sha)
|
2018-12-27 11:12:40 +00:00
|
|
|
return file_path
|
|
|
|
|
|
|
|
|
|
|
|
def add_exec_bit_to_sccache_binary(binary_dir):
|
|
|
|
binary_name = 'sccache'
|
|
|
|
if PLATFORM == 'win32':
|
|
|
|
binary_name += '.exe'
|
2013-06-20 15:23:22 +00:00
|
|
|
|
2018-12-27 11:12:40 +00:00
|
|
|
binary_path = os.path.join(binary_dir, binary_name)
|
|
|
|
add_exec_bit(binary_path)
|
2013-06-20 15:23:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-12-27 11:12:40 +00:00
|
|
|
main()
|