-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathninja_gn_binaries.py
More file actions
executable file
·138 lines (112 loc) · 4.28 KB
/
ninja_gn_binaries.py
File metadata and controls
executable file
·138 lines (112 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This script is used to download prebuilt gn/ninja binaries."""
import platform
import json
import argparse
import os
import sys
import zipfile
import tempfile
import time
import http.client
from v8_deps import Var
from urllib.error import HTTPError, URLError
from stat import ST_MODE, S_IXOTH, S_IXGRP, S_IXUSR
from urllib.request import urlopen
from urllib.parse import urlparse
def get_platform():
system = platform.system().lower()
if system == 'darwin':
system = 'mac'
machine = platform.machine().lower()
if machine == 'x86_64':
machine = 'amd64'
elif machine == 'aarch64':
machine = 'arm64'
return f'{system}-{machine}'
PLATFORM = get_platform()
is_windows = PLATFORM.startswith('windows')
RESOLVE_URL = 'https://chrome-infra-packages.appspot.com/_ah/api/repo/v1/instance/resolve?package_name={}&version={}'
INSTANCE_URL = 'https://chrome-infra-packages.appspot.com/_ah/api/repo/v1/instance?package_name={}&instance_id={}'
NINJA_VERSION = Var('ninja_version')
GN_VERSION = Var('gn_version')
NINJA_PACKAGE = f'infra/3pp/tools/ninja/{PLATFORM}'
GN_PACKAGE = f'gn/gn/{PLATFORM}'
def DownloadUrl(url, output_file):
"""Download url into output_file."""
CHUNK_SIZE = 4096
num_retries = 3
retry_wait_s = 5 # Doubled at each retry.
while True:
try:
sys.stdout.write('Downloading %s...' % url)
sys.stdout.flush()
response = urlopen(url)
bytes_done = 0
while True:
chunk = response.read(CHUNK_SIZE)
if not chunk:
break
output_file.write(chunk)
bytes_done += len(chunk)
if bytes_done == 0:
raise URLError("empty response")
print(' Done.')
return
except URLError as e:
sys.stdout.write('\n')
print(e)
if num_retries == 0 or isinstance(e, HTTPError) and e.code == 404:
raise e
num_retries -= 1
print('Retrying in %d s ...' % retry_wait_s)
sys.stdout.flush()
time.sleep(retry_wait_s)
retry_wait_s *= 2
def EnsureDirExists(path):
if not os.path.exists(path):
os.makedirs(path)
def DownloadAndUnpack(url, output_dir):
"""Download an archive from url and extract into output_dir."""
with tempfile.TemporaryFile() as f:
DownloadUrl(url, f)
f.seek(0)
EnsureDirExists(output_dir)
with zipfile.ZipFile(f, 'r') as z:
z.extractall(path=output_dir)
if not is_windows:
for info in z.infolist():
if info.is_dir():
continue
file = os.path.join(output_dir, info.filename)
hi = info.external_attr >> 16
if hi:
mode = os.stat(file)[ST_MODE]
mode |= hi
os.chmod(file, mode)
def DownloadCIPD(package, tag, output_dir):
def get(url):
parsed = urlparse(url)
conn = http.client.HTTPSConnection(parsed.netloc)
conn.request("GET", parsed.path + (f'?{parsed.query}' if parsed.query else ''), headers={"Host": parsed.netloc})
response = conn.getresponse()
if response.status != 200:
raise Exception(f'GET {url} returned {response.status} {response.reason}')
data = response.read().decode()
return json.loads(data)
resolved = get(RESOLVE_URL.format(package, tag))
instance_id = resolved['instance_id']
instance = get(INSTANCE_URL.format(package, instance_id))
DownloadAndUnpack(instance['fetch_url'], output_dir)
def main():
parser = argparse.ArgumentParser(description='Download ninja/gn binaries.')
parser.add_argument('--dir', help='Where to extract the package.')
args = parser.parse_args()
output_dir = os.path.abspath(args.dir)
DownloadCIPD(GN_PACKAGE, GN_VERSION, os.path.join(output_dir, 'gn'))
DownloadCIPD(NINJA_PACKAGE, NINJA_VERSION, os.path.join(output_dir, 'ninja'))
if __name__ == '__main__':
sys.exit(main())