Debian Codegraph @

bin/fetch-fonts-testdata

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