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 | |
Hal Canary | 7f660e8 | 2017-01-13 13:49:18 -0500 | [diff] [blame] | 8 | import hashlib |
Mike Klein | b621a35 | 2017-01-12 13:20:38 -0500 | [diff] [blame] | 9 | import os |
| 10 | import shutil |
| 11 | import stat |
mtklein | 6af0c95 | 2016-07-22 03:34:25 -0700 | [diff] [blame] | 12 | import sys |
Hal Canary | 70a4fd2 | 2020-01-09 12:35:22 -0500 | [diff] [blame] | 13 | |
| 14 | if sys.version_info[0] < 3: |
| 15 | from urllib2 import urlopen |
| 16 | else: |
| 17 | from urllib.request import urlopen |
mtklein | 6af0c95 | 2016-07-22 03:34:25 -0700 | [diff] [blame] | 18 | |
Hal Canary | 7f660e8 | 2017-01-13 13:49:18 -0500 | [diff] [blame] | 19 | os.chdir(os.path.join(os.path.dirname(__file__), os.pardir)) |
mtklein | 6af0c95 | 2016-07-22 03:34:25 -0700 | [diff] [blame] | 20 | |
Mike Klein | cdcadf7 | 2018-02-15 10:41:10 -0500 | [diff] [blame] | 21 | dst = 'bin/gn.exe' if 'win32' in sys.platform else 'bin/gn' |
Mike Klein | b621a35 | 2017-01-12 13:20:38 -0500 | [diff] [blame] | 22 | |
Mike Klein | 3375a88 | 2019-09-26 14:52:47 -0500 | [diff] [blame] | 23 | sha1 = '3523d50538357829725d4ed74b777a572ce0ac74' if 'linux' in sys.platform else \ |
| 24 | 'd43122f6140d0711518aa909980cb009c4fbce3d' if 'darwin' in sys.platform else \ |
| 25 | 'e20768d93a6b4400de0d03bb8ceb46facdbe3883' # Windows |
Hal Canary | 7f660e8 | 2017-01-13 13:49:18 -0500 | [diff] [blame] | 26 | |
| 27 | def sha1_of_file(path): |
| 28 | h = hashlib.sha1() |
| 29 | if os.path.isfile(path): |
| 30 | with open(path, 'rb') as f: |
| 31 | h.update(f.read()) |
| 32 | return h.hexdigest() |
| 33 | |
Mike Klein | cdcadf7 | 2018-02-15 10:41:10 -0500 | [diff] [blame] | 34 | if sha1_of_file(dst) != sha1: |
| 35 | with open(dst, 'wb') as f: |
Hal Canary | 70a4fd2 | 2020-01-09 12:35:22 -0500 | [diff] [blame] | 36 | f.write(urlopen('https://chromium-gn.storage-download.googleapis.com/' + sha1).read()) |
Mike Klein | b621a35 | 2017-01-12 13:20:38 -0500 | [diff] [blame] | 37 | |
Mike Klein | cdcadf7 | 2018-02-15 10:41:10 -0500 | [diff] [blame] | 38 | os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | |
| 39 | stat.S_IRGRP | stat.S_IXGRP | |
| 40 | stat.S_IROTH | stat.S_IXOTH ) |
Hal Canary | 7f660e8 | 2017-01-13 13:49:18 -0500 | [diff] [blame] | 41 | |
Mike Klein | cdcadf7 | 2018-02-15 10:41:10 -0500 | [diff] [blame] | 42 | # We'll also copy to a path that depot_tools' GN wrapper will expect to find the binary. |
| 43 | copy_path = 'buildtools/linux64/gn' if 'linux' in sys.platform else \ |
| 44 | 'buildtools/mac/gn' if 'darwin' in sys.platform else \ |
| 45 | 'buildtools/win/gn.exe' |
| 46 | if os.path.isdir(os.path.dirname(copy_path)): |
| 47 | shutil.copy(dst, copy_path) |