import argparse import hashlib from pathlib import Path from zipfile import ZIP_DEFLATED, ZipFile BASELINE = '5c632e3db85fc58e55c1917b29916ef1865b3c9eca3e3e70b9131ef44dad1aaf' CLASSES = tuple(f'com/dunerealms/jarvis/{name}.class' for name in ('ArrakisBrewing', 'SurvivalFoodRecipes')) def unique_names(archive): names = archive.namelist() if len(names) != len(set(names)): raise ValueError('Duplicate archive entry') return set(names) def extract(compiled, output): with ZipFile(compiled) as jar, ZipFile(output, 'x', ZIP_DEFLATED) as patch: unique_names(jar) for name in CLASSES: patch.writestr(name, jar.read(name)) def assemble(baseline, patch, output): if hashlib.sha256(baseline.read_bytes()).hexdigest() != BASELINE: raise ValueError('Expected the exact retained Lobby JARVIS baseline') with ZipFile(baseline) as original, ZipFile(patch) as updates: names = unique_names(original) if unique_names(updates) != set(CLASSES) or not set(CLASSES) <= names: raise ValueError('Expected only the two reviewed Lobby recipe listeners') replacements = {name: updates.read(name) for name in CLASSES} if any(not data.startswith(b'\xca\xfe\xba\xbe') or data == original.read(name) for name, data in replacements.items()): raise ValueError('Invalid or unchanged Lobby listener class') with ZipFile(output, 'x', ZIP_DEFLATED) as candidate: for entry in original.infolist(): data = replacements[entry.filename] if entry.filename in replacements else original.read(entry) candidate.writestr(entry, data) def verify_candidate(baseline, candidate, artifact, source): expected_names = {'lobby-food-classes.zip', 'plugin_lobby_food.py', 'SOURCE_COMMIT', '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'} sums = dict(line.split(' ', 1)[::-1] for line in (artifact / 'SHA256SUMS').read_text().splitlines()) if sums.keys() != expected_names or any( hashlib.sha256((artifact / name).read_bytes()).hexdigest() != sha for name, sha in sums.items() ) or (artifact / 'SOURCE_COMMIT').read_text().strip() != source: raise ValueError('Lobby listener patch does not match the selected source and checksums') if hashlib.sha256(baseline.read_bytes()).hexdigest() != BASELINE: raise ValueError('Expected the exact retained Lobby JARVIS baseline') with ZipFile(baseline) as original, ZipFile(candidate) as incoming, ZipFile(artifact / 'lobby-food-classes.zip') as updates: if unique_names(updates) != set(CLASSES) or unique_names(incoming) != unique_names(original): raise ValueError('Unexpected Lobby JARVIS archive contents') for name in incoming.namelist(): expected = updates.read(name) if name in CLASSES else original.read(name) if incoming.read(name) != expected: raise ValueError(f'Unexpected Lobby JARVIS entry: {name}') if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('input', type=Path) parser.add_argument('output', type=Path) parser.add_argument('--baseline', type=Path) args = parser.parse_args() if args.baseline is None: extract(args.input, args.output) else: assemble(args.baseline, args.input, args.output)