Files
arcade/scripts/check-games.py

89 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""Check game ownership, shared interface versions, translations and update metadata."""
import json
import argparse
from pathlib import Path
import re
from game_packages import declared_apps
ROOT = Path(__file__).resolve().parents[1]
def translation_keys(code, prefix):
# Package imports and integer fields read from server JSON are not labels.
# Strip only those argument occurrences: the same key used by the UI must
# still have a translation.
code = re.sub(r'#import(?:\s*,\s*file)?\s*"[^"\n]*"', '', code)
code = re.sub(r'^\s*import\s+(?:[^\n{]+|\{[^}]*\})\s+from\s+"[^"\n]*"\s*;?',
'', code, flags=re.M)
code = re.sub(r'\bShopInteger\s*\(\s*[^,\n]+,\s*"[^"\n]*"', '', code)
return set(re.findall(r'"(' + re.escape(prefix) + r'[a-z0-9_]+)"', code))
def compatible(game, standard):
for field in ('contract_version', 'module_api'):
assert game[field] == standard[field], f'{game["name"]}: incompatible {field}'
assert type(game['host_api']) is int and standard['oldest_host_api'] <= game['host_api'] <= standard['host_api'], \
f'{game["name"]}: incompatible host_api'
def check(ziran, lock_flags):
standard = json.loads((ROOT / 'standards/game-v1.json').read_text())
ledger = json.loads((ROOT / 'apps/versions.json').read_text())
# Arcade's own players refuse games newer than GAME_HOST_API, so it moves
# with the standard and the ledger whenever the host interface does.
player = re.search(r'^GAME_HOST_API :: (\d+)$', (ROOT / 'src/game_contract.zi').read_text(), re.M)
assert player and int(player[1]) == standard['host_api'] == ledger['host_api'], \
'src/game_contract.zi GAME_HOST_API, standards/game-v1.json and apps/versions.json must name the same host interface'
oldest = re.search(r'^GAME_OLDEST_HOST_API :: (\d+)$', (ROOT / 'src/game_contract.zi').read_text(), re.M)
assert oldest and int(oldest[1]) == standard['oldest_host_api'] == ledger['oldest_host_api'], \
'The player, standard and ledger must agree on the oldest supported host interface'
for app in declared_apps(ROOT, ziran, lock_flags):
source, manifest, game = app['source'], app['manifest'], app['descriptor']
compatible(game, standard)
component = ledger['node_apps'][game['name']]
assert game['id'] == component['id'] and game['host_api'] == component['host_api']
assert game['kind'] == 'game' and 'arcade' in game['harnesses']
assert set(game['harnesses']) <= {'arcade', 'inbe'}
assert game['save_schema'] == 1
assert (source / game['source']).is_file() and (source / game['preview']).is_file()
changelog = manifest.with_name('CHANGELOG.md').read_text()
version = re.search(r'^## \[(\d+\.\d+\.\d+)\] - \d{4}-\d{2}-\d{2}$', changelog, re.M)
assert version and version[1] == component['version'], 'Run ./update_version.sh'
assert component['sequence'] > 0
required = set()
for file in [*manifest.parent.glob('*.zi'), *manifest.parent.glob('*.tsx')]:
required.update(translation_keys(file.read_text(), game['locale_prefix']))
locales = {}
for file in (source / game['locales']).glob('*.txt'):
text = file.read_text()
locales[file.stem] = {match[1] for match in re.finditer(r'^\[([^\]]+)\]$', text, re.M)}
entries = {}
heading = None
value = []
for line in text.splitlines():
if line.startswith('[') and line.endswith(']'):
if heading is not None:
entries[heading] = '\n'.join(value).strip()
heading = line[1:-1]
value = []
elif heading is not None and line != '---':
value.append(line)
if heading is not None:
entries[heading] = '\n'.join(value).strip()
empty = [key for key in required if not entries.get(key, '').strip()]
assert not empty, f'{file.name}: empty translations {empty[:3]}'
assert len(locales) >= 12
for language, keys in locales.items():
assert required <= keys, f'{language}: missing {sorted(required - keys)}'
return ledger
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--ziran', default=str(ROOT / 'scripts/ziran.sh'))
parser.add_argument('--locked', action='store_true')
arguments = parser.parse_args()
check(arguments.ziran, ['--locked'] if arguments.locked else [])
print('Game contract, package sources, translated text and independent version ledger passed')