1#!/usr/bin/env python 2"""Downloads a prebuilt gn binary to a place where gn.py can find it.""" 3 4from __future__ import print_function 5 6import io 7import os 8import urllib2 9import sys 10import zipfile 11 12 13def download_and_unpack(url, output_dir, gn): 14 """Download an archive from url and extract gn from it into output_dir.""" 15 print('downloading %s ...' % url, end='') 16 sys.stdout.flush() 17 data = urllib2.urlopen(url).read() 18 print(' done') 19 zipfile.ZipFile(io.BytesIO(data)).extract(gn, path=output_dir) 20 21 22def set_executable_bit(path): 23 mode = os.stat(path).st_mode 24 mode |= (mode & 0o444) >> 2 # Copy R bits to X. 25 os.chmod(path, mode) # No-op on Windows. 26 27 28def get_platform(): 29 import platform 30 if platform.machine() not in ('AMD64', 'x86_64'): 31 return None 32 if sys.platform.startswith('linux'): 33 return 'linux-amd64' 34 if sys.platform == 'darwin': 35 return 'mac-amd64' 36 if sys.platform == 'win32': 37 return 'windows-amd64' 38 39 40def main(): 41 platform = get_platform() 42 if not platform: 43 print('no prebuilt binary for', sys.platform) 44 return 1 45 46 dirname = os.path.join(os.path.dirname(__file__), 'bin', platform) 47 if not os.path.exists(dirname): 48 os.makedirs(dirname) 49 50 url = 'https://chrome-infra-packages.appspot.com/dl/gn/gn/%s/+/latest' 51 gn = 'gn' + ('.exe' if sys.platform == 'win32' else '') 52 download_and_unpack(url % platform, dirname, gn) 53 set_executable_bit(os.path.join(dirname, gn)) 54 55 56if __name__ == '__main__': 57 sys.exit(main()) 58