1#!/usr/bin/env python
2
3# Copyright 2016 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8import os
9import platform
10import shutil
11import stat
12import sys
13import tempfile
14import zipfile
15
16if sys.version_info[0] < 3:
17  from urllib2 import urlopen
18else:
19  from urllib.request import urlopen
20
21os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
22
23gnzip = os.path.join(tempfile.mkdtemp(), 'gn.zip')
24with open(gnzip, 'wb') as f:
25  OS  = {'darwin': 'mac', 'linux': 'linux', 'linux2': 'linux', 'win32': 'windows'}[sys.platform]
26  cpu = {'amd64': 'amd64', 'arm64': 'arm64', 'x86_64': 'amd64'}[platform.machine().lower()]
27
28  rev = 'd62642c920e6a0d1756316d225a90fd6faa9e21e'
29  url = 'https://chrome-infra-packages.appspot.com/dl/gn/gn/{}-{}/+/git_revision:{}'.format(
30          OS,cpu,rev)
31  f.write(urlopen(url).read())
32
33gn = 'gn.exe' if 'win32' in sys.platform else 'gn'
34with zipfile.ZipFile(gnzip, 'r') as f:
35  f.extract(gn, 'bin')
36
37gn = os.path.join('bin', gn)
38
39os.chmod(gn, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
40             stat.S_IRGRP                | stat.S_IXGRP |
41             stat.S_IROTH                | stat.S_IXOTH )
42
43# We'll also copy to a path that depot_tools' GN wrapper will expect to find the binary.
44copy_path = 'buildtools/linux64/gn' if 'linux'  in sys.platform else \
45            'buildtools/mac/gn'     if 'darwin' in sys.platform else \
46            'buildtools/win/gn.exe'
47if os.path.isdir(os.path.dirname(copy_path)):
48  shutil.copy(gn, copy_path)
49