blob: 7fc0f5f161ce091d9ee7fd7c6b23e0f31631e199 [file] [log] [blame]
#!/usr/bin/env python3
# Copyright 2023 Google Inc.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import hashlib
import json
import os
import platform
import re
import stat
import sys
import tempfile
import zipfile
from urllib.request import urlopen
TESTDATA_REL_DIR = 'third_party/externals/googlefonts_testdata'
VERSION_FILE = 'googlefonts_testdata.version'
def sha256sum(path):
with open(path, 'rb') as f:
return hashlib.sha256(
except OSError:
return ''
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
with open('DEPS', 'rb') as f:
deps =
found = re.findall(r"'googlefonts_testdata_version':\s*'(\S+)'", deps)
if len(found) != 1:
print('Unable to find desired revision in DEPS', file=sys.stderr)
desired_version = found[0]
# Determine which version (if any) we currently have.
zip_archive_path = os.path.join(TESTDATA_REL_DIR, ZIP_ARCHIVE_NAME)
current_sha256 = sha256sum(zip_archive_path)
archive_version_path = os.path.join(TESTDATA_REL_DIR, VERSION_FILE)
current_version = {
'version': '',
'sha256': '',
with open(archive_version_path, 'r', encoding='utf8') as f:
current_version = json.load(f)
except OSError:
if desired_version != current_version['version']:
print('Version "%s" requested by DEPS differs from current version "%s"' % (
desired_version, current_version['version']))
elif current_sha256 != current_version['sha256']:
print('sha256 sum "%s" does not match last-downloaded version "%s"' % (
current_sha256, current_version['sha256']))
print('Already up to date.')
print('Fetching %s at %s.' % (ZIP_ARCHIVE_NAME, desired_version))
os.makedirs(os.path.dirname(zip_archive_path), exist_ok=True)
with open(zip_archive_path, 'wb') as f:
url = f'{desired_version}'
with zipfile.ZipFile(zip_archive_path, 'r') as f:
# Write the downloaded version info to a file.
current_version['version'] = desired_version
current_version['sha256'] = sha256sum(zip_archive_path)
with open(archive_version_path, 'w', encoding='utf8') as f:
json.dump(current_version, f, sort_keys=True, indent=2)