"""Verify or assemble the reviewed Arrakis-only JARVIS transition.""" import argparse from copy import copy import hashlib from pathlib import Path from zipfile import ZipFile BASELINE = '883969e04ef6a3436e6ebef54112d16a1311555226b386ae75a8c1199556c7ca' 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 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', '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): if digest(baseline) != BASELINE: raise ValueError('Expected exact Arrakis retained JARVIS') 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} old_descriptor = old.read('plugin.yml').replace(b'\r\n', b'\n') new_descriptor = updates['plugin.yml'].replace(b'\r\n', b'\n') if new_descriptor.replace(b', DuneGroups]', b']') != old_descriptor or b', DuneGroups]' not in new_descriptor: raise ValueError('Expected only the DuneGroups soft dependency in the descriptor') 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) args = parser.parse_args() verify_artifact(args.artifact, args.source) transition(args.baseline, args.artifact / 'jarvistranslator.jar', args.candidate, assemble=True)