• 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 shutil
14import stat
15import sys
16import tempfile
17import zipfile
18
19if sys.version_info[0] < 3:
20  from urllib2 import urlopen
21else:
22  from urllib.request import urlopen
23
24
25def sha256sum(path):
26  try:
27    with open(sk_path, 'rb') as f:
28      return hashlib.sha256(f.read()).hexdigest()
29  except OSError:
30    return ''
31
32
33os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
34
35OS  = {'darwin': 'mac', 'linux': 'linux', 'linux2': 'linux', 'win32': 'windows'}[sys.platform]
36cpu = {'amd64': 'amd64', 'arm64': 'arm64', 'x86_64': 'amd64'}[platform.machine().lower()]
37platform = '%s-%s' % (OS, cpu)
38sk = 'sk'
39if 'windows' in platform:
40  sk = 'sk.exe'
41
42# Find the version of 'sk' requested by DEPS.
43with open('DEPS', 'rb') as f:
44  deps = f.read().decode()
45found = re.findall(r"'sk_tool_revision':\s*'(\S+)'", deps)
46if len(found) != 1:
47  print('Unable to find sk_tool_revision in DEPS', file=sys.stderr)
48  exit(1)
49desired_version = found[0]
50
51# Determine which version (if any) we currently have.
52sk_path = os.path.join('bin', sk)
53current_sha256 = sha256sum(sk_path)
54sk_version_path = os.path.join('bin', 'sk.version')
55
56# When we download 'sk', 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(sk_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' % (sk, desired_version, platform))
80
81# Download sk.
82skzip = os.path.join(tempfile.mkdtemp(), 'sk.zip')
83with open(skzip, 'wb') as f:
84  url = 'https://chrome-infra-packages.appspot.com/dl/skia/tools/sk/%s/+/%s' % (
85      platform, desired_version)
86  f.write(urlopen(url).read())
87
88with zipfile.ZipFile(skzip, 'r') as f:
89  f.extract(sk, 'bin')
90
91if not 'windows' in platform:
92  uid = os.getuid()
93  gid = os.getgid()
94  os.chown(sk_path, uid, gid)
95  os.chmod(sk_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(sk_path)
102with open(sk_version_path, 'w', encoding='utf8') as f:
103  json.dump(current_version, f, sort_keys=True, indent=2)
104