Debian Codegraph @

bin/fetch-ninja

raw
1 #!/usr/bin/env python3
2
3 # Copyright 2022 Google LLC
4 #
5 # Use of this source code is governed by a BSD-style license that can be
6 # found in the LICENSE file.
7
8 import hashlib
9 import json
10 import os
11 import platform
12 import re
13 import stat
14 import sys
15 import tempfile
16 import zipfile
17
18 if sys.version_info[0] < 3:
19 from urllib2 import urlopen
20 else:
21 from urllib.request import urlopen
22
23 def sha256sum(path):
24 try:
25 with open(path, 'rb') as f:
26 return hashlib.sha256(f.read()).hexdigest()
27 except OSError:
28 return ''
29
30
31 os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
32
33 OS = {'darwin': 'mac', 'linux': 'linux', 'linux2': 'linux', 'win32': 'windows'}[sys.platform]
34 cpu = {'aarch64': 'arm64', 'amd64': 'amd64', 'arm64': 'arm64', 'x86_64': 'amd64'}[platform.machine().lower()]
35 platform = '%s-%s' % (OS, cpu)
36 ninja = 'ninja'
37 if 'windows' in platform:
38 ninja = 'ninja.exe'
39
40 # Find the version of 'ninja' requested by DEPS.
41 with open('DEPS', 'rb') as f:
42 deps = f.read().decode()
43 found = re.findall(r"'ninja_version':\s*'(\S+)'", deps)
44 if len(found) != 1:
45 print('Unable to find ninja_version in DEPS', file=sys.stderr)
46 exit(1)
47 desired_version = found[0]
48
49 # Determine which version (if any) we currently have.
50 # depot_tools scripts expect ninja to be in third_party
51 os.makedirs(os.path.join('third_party', 'ninja'), exist_ok=True)
52 ninja_path = os.path.join('third_party', 'ninja', ninja)
53 current_sha256 = sha256sum(ninja_path)
54 ninja_version_path = os.path.join('bin', 'ninja.version')
55
56 # When we download 'ninja', we write the version information to a file so we can
57 # keep track of which version we have. Read the file to determine whether the
58 # current version matches what we want.
59 current_version = {
60 'version': '',
61 'sha256': '',
62 }
63 try:
64 with open(ninja_version_path, 'r', encoding='utf8') as f:
65 current_version = json.load(f)
66 except OSError:
67 pass
68
69 if desired_version != current_version['version']:
70 print('Version "%s" requested by DEPS differs from current version "%s"' % (
71 desired_version, current_version['version']))
72 elif current_sha256 != current_version['sha256']:
73 print('sha256 sum "%s" does not match last-downloaded version "%s"' % (
74 current_sha256, current_version['sha256']))
75 else:
76 print('Already up to date.')
77 exit(0)
78
79 print('Fetching %s at %s for platform %s' % (ninja, desired_version, platform))
80
81 # Download ninja.
82 ninjazip = os.path.join(tempfile.mkdtemp(), 'ninja.zip')
83 with open(ninjazip, 'wb') as f:
84 url = 'https://chrome-infra-packages.appspot.com/dl/infra/3pp/tools/ninja/%s/+/%s' % (
85 platform, desired_version)
86 f.write(urlopen(url).read())
87
88 with zipfile.ZipFile(ninjazip, 'r') as f:
89 f.extract(ninja, os.path.join('third_party', 'ninja'))
90
91 if not 'windows' in platform:
92 uid = os.getuid()
93 gid = os.getgid()
94 os.chown(ninja_path, uid, gid)
95 os.chmod(ninja_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
96 stat.S_IRGRP | stat.S_IXGRP |
97 stat.S_IROTH | stat.S_IXOTH )
98
99 # Write the downloaded version info to a file.
100 current_version['version'] = desired_version
101 current_version['sha256'] = sha256sum(ninja_path)
102 with open(ninja_version_path, 'w', encoding='utf8') as f:
103 json.dump(current_version, f, sort_keys=True, indent=2)