"""Verify or assemble the reviewed Arrakis-only JARVIS transition.""" import argparse from copy import copy import hashlib from pathlib import Path import re from zipfile import ZipFile BASELINE = '883969e04ef6a3436e6ebef54112d16a1311555226b386ae75a8c1199556c7ca' PLANET_BASELINE = 'ebe8d2edc9f3300a039de1f76b81a2435895569704b2087e0625db699effe5d7' LOBBY_BASELINE = 'e28907c52e675c5b1860f80adbda1601e1f389480b20a6939c992948fc46dcfa' CONFIG_BASELINES = { 'arrakis': 'c90db002680004ac5784fdb3fdb38a6ccc0e6b9018a3bdcb6abfb109fc5d4a14', 'caladan': 'db4b6dbdfde6b3775cfe3d08bf6a56b336fbb29abb49057beed6b94b28ea4aba', 'giedi_prime': 'b54e357ae5be48e7c01cf55269ced46ecd8ba3695615abf444aed17f4e94d54a', 'kaitain': '87ce8fa35ddc54828570e50b9952c5914bb98961a52d3f2aaf3be94179199519', 'sigma_draconis_iv': '3992734a42612c0426bb7f72a86736d76aac8c4990ed372f50f5271760101eb1', 'lobby': None, } VENDORS = { 'towny': ('Towny-0.103.2.0.jar', 'c8d7d59984eaf2606fce4192e860f7f969f3384e5a51b26b97225efbbe747c70'), 'medievalfactions': ('medieval-factions-11.1.0-all.jar', 'a3d5d448a15c3a864e90db659342782584bc3284b434ff926f21019d51e3350e'), } PREFIX = 'com/dunerealms/jarvis/' REPLACED = { PREFIX + name + '.class' for name in ('ImperialRaid', 'ImperialRaid$Order', 'ImperialRaidWorker', 'JarvisTranslator', 'NativeKeyWorker') } | {'plugin.yml'} ADDED = { PREFIX + name + '.class' for name in ('CentralClanDirectory', 'ClanDirectory', 'DuneGroupsAcknowledgements', 'DuneGroupsAcknowledgements$Writer', 'DuneGroupsKeyWorker', 'FactionClanDirectory', 'NativeKeys', 'OrganisationDirectory', 'OrganisationDirectory$Group', 'OrganisationDirectory$Home', 'OrganisationDirectory$War') } def digest(path): return hashlib.sha256(path.read_bytes()).hexdigest() def enable_backend(config, server='arrakis'): if server == 'lobby' and config is None: return 'native:\n server-key: lobby\n backend: dunegroups\n' if config is None: raise ValueError('Expected retained native config') headers = list(re.finditer(r'(?m)^native:[ \t]*(?:#[^\r\n]*)?(\r?\n)', config)) if len(headers) != 1: raise ValueError('Expected exactly one native config section') header = headers[0] nested = re.match(r'(?m)(?:[ \t]+[^\r\n]*\r?\n|[ \t]*\r?\n)*', config[header.end():])[0] key = re.search(r'(?m)^([ ]+)server-key:[ \t]*' + re.escape(server.replace('_', '-')) + r'[ \t]*(?:#[^\r\n]*)?\r?$', nested) if not key or re.search(r'(?m)^\s+backend\s*:', nested): raise ValueError('Expected native config without a backend override') return config[:header.end()] + key[1] + 'backend: dunegroups' + header[1] + config[header.end():] def names(archive): records = archive.namelist() if len(set(records)) != len(records): raise ValueError('Duplicate archive entry') return set(records) def verify_artifact(artifact, source): pinned = {'jarvistranslator.jar', 'DuneGroups.jar', 'SOURCE_COMMIT', 'lobby-native-key-worker.class', 'plugin_dunegroups.py', 'install-plugin-fleet.py', 'write-plugin-manifest.py', 'plugin_menu_pack.py', 'plugin_backend_abi.py', 'plugin_fleet_rollback.py', 'plugin_pack_properties.py', 'plugin_pillager_abi.py'} lines = (artifact / 'SHA256SUMS').read_text().splitlines() sums = dict(line.split(' ', 1)[::-1] for line in lines) if len(lines) != len(sums) or sums.keys() != pinned or any( digest(artifact / name) != checksum for name, checksum in sums.items() ): raise ValueError('Unexpected DuneGroups artifact manifest') if (artifact / 'SOURCE_COMMIT').read_text().strip() != source: raise ValueError('Unexpected DuneGroups source') def transition(baseline, compiled, candidate, assemble=False, server='arrakis'): if server not in CONFIG_BASELINES: raise ValueError('Unexpected server') expected = BASELINE if server == 'arrakis' else LOBBY_BASELINE if server == 'lobby' else PLANET_BASELINE if digest(baseline) != expected: raise ValueError('Expected exact retained JARVIS') replaced = {'plugin.yml', PREFIX+'NativeKeyWorker.class'} if server == 'lobby' else REPLACED added = ADDED - {PREFIX+'FactionClanDirectory.class'} if server == 'lobby' else ADDED with ZipFile(baseline) as old, ZipFile(compiled) as built: kept = names(old) if not replaced <= kept or kept & added or not (replaced | added) <= names(built): raise ValueError('Unexpected JARVIS transition inputs') updates = {name: built.read(name) for name in replaced | added} descriptor, count = re.subn(rb'(?m)^(softdepend: \[[^\r\n]+)\]', rb'\1, DuneGroups]', old.read('plugin.yml')) if count != 1 or old.read('plugin.yml').count(b'DuneGroups'): raise ValueError('Unexpected retained JARVIS soft dependencies') updates['plugin.yml'] = descriptor if server == 'lobby': updates[PREFIX+'NativeKeyWorker.class'] = compiled.with_name('lobby-native-key-worker.class').read_bytes() if assemble: with ZipFile(candidate, 'x') as out: for entry in old.infolist(): name = entry.filename out.writestr(copy(entry), updates[name] if name in updates else old.read(entry)) for name in sorted(added): out.writestr(copy(built.getinfo(name)), updates[name]) with ZipFile(candidate) as incoming: if names(incoming) != kept | added: raise ValueError('Unexpected DuneGroups candidate entries') for name in kept | added: expected = updates[name] if name in updates else old.read(name) if incoming.read(name) != expected: raise ValueError(f'Unreviewed JARVIS change: {name}') if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('baseline', type=Path) parser.add_argument('artifact', type=Path) parser.add_argument('source') parser.add_argument('candidate', type=Path) parser.add_argument('--server', choices=sorted(CONFIG_BASELINES), default='arrakis') args = parser.parse_args() verify_artifact(args.artifact, args.source) transition(args.baseline, args.artifact / 'jarvistranslator.jar', args.candidate, assemble=True, server=args.server)