1bf215546Sopenharmony_cidiff --git a/bin/fetch-gn b/bin/fetch-gn
2bf215546Sopenharmony_ciindex d5e94a2..59c4591 100755
3bf215546Sopenharmony_ci--- a/bin/fetch-gn
4bf215546Sopenharmony_ci+++ b/bin/fetch-gn
5bf215546Sopenharmony_ci@@ -5,39 +5,44 @@
6bf215546Sopenharmony_ci # Use of this source code is governed by a BSD-style license that can be
7bf215546Sopenharmony_ci # found in the LICENSE file.
8bf215546Sopenharmony_ci 
9bf215546Sopenharmony_ci-import hashlib
10bf215546Sopenharmony_ci import os
11bf215546Sopenharmony_ci+import platform
12bf215546Sopenharmony_ci import shutil
13bf215546Sopenharmony_ci import stat
14bf215546Sopenharmony_ci import sys
15bf215546Sopenharmony_ci-import urllib2
16bf215546Sopenharmony_ci+import tempfile
17bf215546Sopenharmony_ci+import zipfile
18bf215546Sopenharmony_ci+
19bf215546Sopenharmony_ci+if sys.version_info[0] < 3:
20bf215546Sopenharmony_ci+  from urllib2 import urlopen
21bf215546Sopenharmony_ci+else:
22bf215546Sopenharmony_ci+  from urllib.request import urlopen
23bf215546Sopenharmony_ci 
24bf215546Sopenharmony_ci os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
25bf215546Sopenharmony_ci 
26bf215546Sopenharmony_ci-dst = 'bin/gn.exe' if 'win32' in sys.platform else 'bin/gn'
27bf215546Sopenharmony_ci+gnzip = os.path.join(tempfile.mkdtemp(), 'gn.zip')
28bf215546Sopenharmony_ci+with open(gnzip, 'wb') as f:
29bf215546Sopenharmony_ci+  OS  = {'darwin': 'mac', 'linux': 'linux', 'linux2': 'linux', 'win32': 'windows'}[sys.platform]
30bf215546Sopenharmony_ci+  cpu = {'amd64': 'amd64', 'arm64': 'arm64', 'x86_64': 'amd64', 'aarch64': 'arm64'}[platform.machine().lower()]
31bf215546Sopenharmony_ci 
32bf215546Sopenharmony_ci-sha1 = '2f27ff0b6118e5886df976da5effa6003d19d1ce' if 'linux'  in sys.platform else \
33bf215546Sopenharmony_ci-       '9be792dd9010ce303a9c3a497a67bcc5ac8c7666' if 'darwin' in sys.platform else \
34bf215546Sopenharmony_ci-       'eb69be2d984b4df60a8c21f598135991f0ad1742'  # Windows
35bf215546Sopenharmony_ci+  rev = 'd62642c920e6a0d1756316d225a90fd6faa9e21e'
36bf215546Sopenharmony_ci+  url = 'https://chrome-infra-packages.appspot.com/dl/gn/gn/{}-{}/+/git_revision:{}'.format(
37bf215546Sopenharmony_ci+          OS,cpu,rev)
38bf215546Sopenharmony_ci+  f.write(urlopen(url).read())
39bf215546Sopenharmony_ci 
40bf215546Sopenharmony_ci-def sha1_of_file(path):
41bf215546Sopenharmony_ci-  h = hashlib.sha1()
42bf215546Sopenharmony_ci-  if os.path.isfile(path):
43bf215546Sopenharmony_ci-    with open(path, 'rb') as f:
44bf215546Sopenharmony_ci-      h.update(f.read())
45bf215546Sopenharmony_ci-  return h.hexdigest()
46bf215546Sopenharmony_ci+gn = 'gn.exe' if 'win32' in sys.platform else 'gn'
47bf215546Sopenharmony_ci+with zipfile.ZipFile(gnzip, 'r') as f:
48bf215546Sopenharmony_ci+  f.extract(gn, 'bin')
49bf215546Sopenharmony_ci 
50bf215546Sopenharmony_ci-if sha1_of_file(dst) != sha1:
51bf215546Sopenharmony_ci-  with open(dst, 'wb') as f:
52bf215546Sopenharmony_ci-    f.write(urllib2.urlopen('https://chromium-gn.storage-download.googleapis.com/' + sha1).read())
53bf215546Sopenharmony_ci+gn = os.path.join('bin', gn)
54bf215546Sopenharmony_ci 
55bf215546Sopenharmony_ci-  os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
56bf215546Sopenharmony_ci-                stat.S_IRGRP                | stat.S_IXGRP |
57bf215546Sopenharmony_ci-                stat.S_IROTH                | stat.S_IXOTH )
58bf215546Sopenharmony_ci+os.chmod(gn, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
59bf215546Sopenharmony_ci+             stat.S_IRGRP                | stat.S_IXGRP |
60bf215546Sopenharmony_ci+             stat.S_IROTH                | stat.S_IXOTH )
61bf215546Sopenharmony_ci 
62bf215546Sopenharmony_ci # We'll also copy to a path that depot_tools' GN wrapper will expect to find the binary.
63bf215546Sopenharmony_ci copy_path = 'buildtools/linux64/gn' if 'linux'  in sys.platform else \
64bf215546Sopenharmony_ci             'buildtools/mac/gn'     if 'darwin' in sys.platform else \
65bf215546Sopenharmony_ci             'buildtools/win/gn.exe'
66bf215546Sopenharmony_ci if os.path.isdir(os.path.dirname(copy_path)):
67bf215546Sopenharmony_ci-  shutil.copy(dst, copy_path)
68bf215546Sopenharmony_ci+  shutil.copy(gn, copy_path)
69