xref: /aosp_15_r20/external/skia/bin/fetch-fonts-testdata (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1#!/usr/bin/env python3
2
3# Copyright 2023 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
20TESTDATA_REL_DIR = 'third_party/externals/googlefonts_testdata'
21ZIP_ARCHIVE_NAME = 'googlefonts_testdata.zip'
22VERSION_FILE = 'googlefonts_testdata.version'
23
24def sha256sum(path):
25  try:
26    with open(path, 'rb') as f:
27      return hashlib.sha256(f.read()).hexdigest()
28  except OSError:
29    return ''
30
31
32os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
33
34with open('DEPS', 'rb') as f:
35  deps = f.read().decode()
36found = re.findall(r"'googlefonts_testdata_version':\s*'(\S+)'", deps)
37if len(found) != 1:
38  print('Unable to find desired revision in DEPS', file=sys.stderr)
39  exit(1)
40desired_version = found[0]
41
42# Determine which version (if any) we currently have.
43zip_archive_path = os.path.join(TESTDATA_REL_DIR, ZIP_ARCHIVE_NAME)
44current_sha256 = sha256sum(zip_archive_path)
45archive_version_path = os.path.join(TESTDATA_REL_DIR, VERSION_FILE)
46
47current_version = {
48  'version': '',
49  'sha256': '',
50}
51try:
52  with open(archive_version_path, 'r', encoding='utf8') as f:
53    current_version = json.load(f)
54except OSError:
55  pass
56
57if desired_version != current_version['version']:
58  print('Version "%s" requested by DEPS differs from current version "%s"' % (
59      desired_version, current_version['version']))
60elif current_sha256 != current_version['sha256']:
61  print('sha256 sum "%s" does not match last-downloaded version "%s"' % (
62      current_sha256, current_version['sha256']))
63else:
64  print('Already up to date.')
65  exit(0)
66
67print('Fetching %s at %s.' % (ZIP_ARCHIVE_NAME, desired_version))
68
69os.makedirs(os.path.dirname(zip_archive_path), exist_ok=True)
70with open(zip_archive_path, 'wb') as f:
71  url = f'https://chrome-infra-packages.appspot.com/dl/chromium/third_party/googlefonts_testdata/+/{desired_version}'
72  f.write(urlopen(url).read())
73
74with zipfile.ZipFile(zip_archive_path, 'r') as f:
75  f.extractall(TESTDATA_REL_DIR)
76
77# Write the downloaded version info to a file.
78current_version['version'] = desired_version
79current_version['sha256'] = sha256sum(zip_archive_path)
80with open(archive_version_path, 'w', encoding='utf8') as f:
81  json.dump(current_version, f, sort_keys=True, indent=2)
82