xref: /aosp_15_r20/external/skia/bin/fetch-ninja (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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
8import hashlib
9import json
10import os
11import platform
12import re
13import stat
14import sys
15import tempfile
16import zipfile
17
18if sys.version_info[0] < 3:
19  from urllib2 import urlopen
20else:
21  from urllib.request import urlopen
22
23def 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
31os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
32
33OS  = {'darwin': 'mac', 'linux': 'linux', 'linux2': 'linux', 'win32': 'windows'}[sys.platform]
34cpu = {'aarch64': 'arm64', 'amd64': 'amd64', 'arm64': 'arm64', 'x86_64': 'amd64'}[platform.machine().lower()]
35platform = '%s-%s' % (OS, cpu)
36ninja = 'ninja'
37if 'windows' in platform:
38  ninja = 'ninja.exe'
39
40# Find the version of 'ninja' requested by DEPS.
41with open('DEPS', 'rb') as f:
42  deps = f.read().decode()
43found = re.findall(r"'ninja_version':\s*'(\S+)'", deps)
44if len(found) != 1:
45  print('Unable to find ninja_version in DEPS', file=sys.stderr)
46  exit(1)
47desired_version = found[0]
48
49# Determine which version (if any) we currently have.
50# depot_tools scripts expect ninja to be in third_party
51os.makedirs(os.path.join('third_party', 'ninja'), exist_ok=True)
52ninja_path = os.path.join('third_party', 'ninja', ninja)
53current_sha256 = sha256sum(ninja_path)
54ninja_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.
59current_version = {
60  'version': '',
61  'sha256': '',
62}
63try:
64  with open(ninja_version_path, 'r', encoding='utf8') as f:
65    current_version = json.load(f)
66except OSError:
67  pass
68
69if desired_version != current_version['version']:
70  print('Version "%s" requested by DEPS differs from current version "%s"' % (
71      desired_version, current_version['version']))
72elif current_sha256 != current_version['sha256']:
73  print('sha256 sum "%s" does not match last-downloaded version "%s"' % (
74      current_sha256, current_version['sha256']))
75else:
76  print('Already up to date.')
77  exit(0)
78
79print('Fetching %s at %s for platform %s' % (ninja, desired_version, platform))
80
81# Download ninja.
82ninjazip = os.path.join(tempfile.mkdtemp(), 'ninja.zip')
83with 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
88with zipfile.ZipFile(ninjazip, 'r') as f:
89  f.extract(ninja, os.path.join('third_party', 'ninja'))
90
91if 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.
100current_version['version'] = desired_version
101current_version['sha256'] = sha256sum(ninja_path)
102with open(ninja_version_path, 'w', encoding='utf8') as f:
103  json.dump(current_version, f, sort_keys=True, indent=2)
104