-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch_binaries.py
73 lines (62 loc) · 2.21 KB
/
fetch_binaries.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import zipfile
import stat
if sys.version >= '3':
import urllib.request as urllib
else:
import urllib
def get_redirected_url(url):
try:
remote = urllib.urlopen(url)
except Exception as e:
print(e)
return None
return remote.url
def download(url, file):
if not os.path.exists(os.path.dirname(file)):
os.makedirs(os.path.dirname(file))
try:
remote = urllib.urlopen(url)
except Exception as e:
print(e)
return False
with open(file, 'wb') as local:
BLOCK_SIZE = 1024 * 1024
while True:
buffer = remote.read(BLOCK_SIZE)
local.write(buffer)
if len(buffer) < BLOCK_SIZE:
break
return True
def main():
os.chdir(os.path.dirname(__file__))
ninja_latest = os.path.basename(get_redirected_url(
'https://github.com/ninja-build/ninja/releases/latest'))
url = {}
if sys.platform == 'win32':
url['gn'] = 'https://chrome-infra-packages.appspot.com/dl/gn/gn/windows-amd64/+/latest'
url['ninja'] = 'https://github.com/ninja-build/ninja/releases/download/%s/ninja-win.zip' % ninja_latest
elif sys.platform == 'darwin':
url['gn'] = 'https://chrome-infra-packages.appspot.com/dl/gn/gn/mac-amd64/+/latest'
url['ninja'] = 'https://github.com/ninja-build/ninja/releases/download/%s/ninja-mac.zip' % ninja_latest
elif sys.platform == 'linux':
url['gn'] = 'https://chrome-infra-packages.appspot.com/dl/gn/gn/linux-amd64/+/latest'
url['ninja'] = 'https://github.com/ninja-build/ninja/releases/download/%s/ninja-linux.zip' % ninja_latest
else:
assert False, 'Unsupport system: %s' % sys.platform
for t in ('gn', 'ninja',):
z = os.path.join('bin', t + '.zip')
download(url[t], z)
with zipfile.ZipFile(z, 'r') as zip:
f = t + '.exe' if sys.platform == 'win32' else t
zip.extract(f, 'bin')
if sys.platform != 'win32':
p = os.path.join('bin', f)
st = os.stat(p)
os.chmod(p, st.st_mode | stat.S_IEXEC)
os.remove(z)
if __name__ == '__main__':
main()