• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
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
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)
36sk = 'sk'
37if 'windows' in platform:
38  sk = 'sk.exe'
39
40# Find the version of 'sk' requested by DEPS.
41with open('DEPS', 'rb') as f:
42  deps = f.read().decode()
43found = re.findall(r"'sk_tool_revision':\s*'(\S+)'", deps)
44if len(found) != 1:
45  print('Unable to find sk_tool_revision in DEPS', file=sys.stderr)
46  exit(1)
47desired_version = found[0]
48
49# Determine which version (if any) we currently have.
50sk_path = os.path.join('bin', sk)
51current_sha256 = sha256sum(sk_path)
52sk_version_path = os.path.join('bin', 'sk.version')
53
54# When we download 'sk', we write the version information to a file so we can
55# keep track of which version we have. Read the file to determine whether the
56# current version matches what we want.
57current_version = {
58  'version': '',
59  'sha256': '',
60}
61try:
62  with open(sk_version_path, 'r', encoding='utf8') as f:
63    current_version = json.load(f)
64except OSError:
65  pass
66
67if desired_version != current_version['version']:
68  print('Version "%s" requested by DEPS differs from current version "%s"' % (
69      desired_version, current_version['version']))
70elif current_sha256 != current_version['sha256']:
71  print('sha256 sum "%s" does not match last-downloaded version "%s"' % (
72      current_sha256, current_version['sha256']))
73else:
74  print('Already up to date.')
75  exit(0)
76
77print('Fetching %s at %s for platform %s' % (sk, desired_version, platform))
78
79# Download sk.
80skzip = os.path.join(tempfile.mkdtemp(), 'sk.zip')
81with open(skzip, 'wb') as f:
82  url = 'https://chrome-infra-packages.appspot.com/dl/skia/tools/sk/%s/+/%s' % (
83      platform, desired_version)
84  f.write(urlopen(url).read())
85
86with zipfile.ZipFile(skzip, 'r') as f:
87  f.extract(sk, 'bin')
88
89if not 'windows' in platform:
90  uid = os.getuid()
91  gid = os.getgid()
92  os.chown(sk_path, uid, gid)
93  os.chmod(sk_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
94                    stat.S_IRGRP                | stat.S_IXGRP |
95                    stat.S_IROTH                | stat.S_IXOTH )
96
97# Write the downloaded version info to a file.
98current_version['version'] = desired_version
99current_version['sha256'] = sha256sum(sk_path)
100with open(sk_version_path, 'w', encoding='utf8') as f:
101  json.dump(current_version, f, sort_keys=True, indent=2)
102