Debian Codegraph @

bin/fetch-sk

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