mtklein | 6af0c95 | 2016-07-22 03:34:25 -0700 | [diff] [blame] | 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 | |
Mike Klein | b621a35 | 2017-01-12 13:20:38 -0500 | [diff] [blame] | 8 | import os |
Mike Klein | 7de307a | 2021-01-15 07:43:03 -0600 | [diff] [blame] | 9 | import platform |
Mike Klein | b621a35 | 2017-01-12 13:20:38 -0500 | [diff] [blame] | 10 | import shutil |
| 11 | import stat |
mtklein | 6af0c95 | 2016-07-22 03:34:25 -0700 | [diff] [blame] | 12 | import sys |
Mike Klein | bfc0926 | 2020-03-31 15:23:58 -0500 | [diff] [blame] | 13 | import tempfile |
| 14 | import zipfile |
Hal Canary | 70a4fd2 | 2020-01-09 12:35:22 -0500 | [diff] [blame] | 15 | |
| 16 | if sys.version_info[0] < 3: |
| 17 | from urllib2 import urlopen |
| 18 | else: |
| 19 | from urllib.request import urlopen |
mtklein | 6af0c95 | 2016-07-22 03:34:25 -0700 | [diff] [blame] | 20 | |
Hal Canary | 7f660e8 | 2017-01-13 13:49:18 -0500 | [diff] [blame] | 21 | os.chdir(os.path.join(os.path.dirname(__file__), os.pardir)) |
mtklein | 6af0c95 | 2016-07-22 03:34:25 -0700 | [diff] [blame] | 22 | |
Mike Klein | bfc0926 | 2020-03-31 15:23:58 -0500 | [diff] [blame] | 23 | gnzip = os.path.join(tempfile.mkdtemp(), 'gn.zip') |
| 24 | with open(gnzip, 'wb') as f: |
Mike Klein | 7de307a | 2021-01-15 07:43:03 -0600 | [diff] [blame] | 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 | |
Mike Klein | 4f4c064 | 2021-01-15 07:20:33 -0600 | [diff] [blame] | 28 | rev = 'd62642c920e6a0d1756316d225a90fd6faa9e21e' |
Mike Klein | 7de307a | 2021-01-15 07:43:03 -0600 | [diff] [blame] | 29 | url = 'https://chrome-infra-packages.appspot.com/dl/gn/gn/{}-{}/+/git_revision:{}'.format( |
| 30 | OS,cpu,rev) |
Mike Klein | bfc0926 | 2020-03-31 15:23:58 -0500 | [diff] [blame] | 31 | f.write(urlopen(url).read()) |
Mike Klein | b621a35 | 2017-01-12 13:20:38 -0500 | [diff] [blame] | 32 | |
Mike Klein | bfc0926 | 2020-03-31 15:23:58 -0500 | [diff] [blame] | 33 | gn = 'gn.exe' if 'win32' in sys.platform else 'gn' |
| 34 | with zipfile.ZipFile(gnzip, 'r') as f: |
| 35 | f.extract(gn, 'bin') |
Hal Canary | 7f660e8 | 2017-01-13 13:49:18 -0500 | [diff] [blame] | 36 | |
Mike Klein | bfc0926 | 2020-03-31 15:23:58 -0500 | [diff] [blame] | 37 | gn = os.path.join('bin', gn) |
Hal Canary | 7f660e8 | 2017-01-13 13:49:18 -0500 | [diff] [blame] | 38 | |
Mike Klein | bfc0926 | 2020-03-31 15:23:58 -0500 | [diff] [blame] | 39 | os.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 ) |
Hal Canary | 7f660e8 | 2017-01-13 13:49:18 -0500 | [diff] [blame] | 42 | |
Mike Klein | cdcadf7 | 2018-02-15 10:41:10 -0500 | [diff] [blame] | 43 | # We'll also copy to a path that depot_tools' GN wrapper will expect to find the binary. |
| 44 | copy_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' |
| 47 | if os.path.isdir(os.path.dirname(copy_path)): |
Mike Klein | bfc0926 | 2020-03-31 15:23:58 -0500 | [diff] [blame] | 48 | shutil.copy(gn, copy_path) |