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