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 8import hashlib 9import json 10import os 11import platform 12import re 13import stat 14import sys 15import tempfile 16import zipfile 17 18from urllib.request import urlopen 19 20 21def 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 29os.chdir(os.path.join(os.path.dirname(__file__), os.pardir)) 30 31OS = {'darwin': 'mac', 'linux': 'linux', 'linux2': 'linux', 'win32': 'windows'}[sys.platform] 32cpu = {'aarch64': 'arm64', 'amd64': 'amd64', 'arm64': 'arm64', 'x86_64': 'amd64'}[platform.machine().lower()] 33platform = '%s-%s' % (OS, cpu) 34sk = 'sk' 35if 'windows' in platform: 36 sk = 'sk.exe' 37 38# Find the version of 'sk' requested by DEPS. 39with open('DEPS', 'rb') as f: 40 deps = f.read().decode() 41found = re.findall(r"'infra_revision':\s*'(\S+)'", deps) 42if len(found) != 1: 43 print('Unable to find infra_revision in DEPS', file=sys.stderr) 44 exit(1) 45desired_version = "git_revision:" + found[0] 46 47# Determine which version (if any) we currently have. 48sk_path = os.path.join('bin', sk) 49current_sha256 = sha256sum(sk_path) 50sk_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. 55current_version = { 56 'version': '', 57 'sha256': '', 58} 59try: 60 with open(sk_version_path, 'r', encoding='utf8') as f: 61 current_version = json.load(f) 62except OSError: 63 pass 64 65if desired_version != current_version['version']: 66 print('Version "%s" requested by DEPS differs from current version "%s"' % ( 67 desired_version, current_version['version'])) 68elif current_sha256 != current_version['sha256']: 69 print('sha256 sum "%s" does not match last-downloaded version "%s"' % ( 70 current_sha256, current_version['sha256'])) 71else: 72 print('Already up to date.') 73 exit(0) 74 75print('Fetching %s at %s for platform %s' % (sk, desired_version, platform)) 76 77# Download sk. 78skzip = os.path.join(tempfile.mkdtemp(), 'sk.zip') 79with 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 84with zipfile.ZipFile(skzip, 'r') as f: 85 f.extract(sk, 'bin') 86 87if 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. 96current_version['version'] = desired_version 97current_version['sha256'] = sha256sum(sk_path) 98with open(sk_version_path, 'w', encoding='utf8') as f: 99 json.dump(current_version, f, sort_keys=True, indent=2) 100