plugins/tools/plugin_builder_shared_profiles.py

46 lines
1.9 KiB
Python

"""Graft the shared inventory guard onto the retained Lobby Builder JAR."""
import argparse
import os
from pathlib import Path
import subprocess
from tempfile import TemporaryDirectory
from plugin_wild_update import digest, read_archive, write_archive
PREFIX = "com/example/dunelobby/"
BUILDER = PREFIX + "BuilderTransfer.class"
GUARD = PREFIX + "BuilderSharedProfiles.class"
BASELINE = "18f8878b18f249ff97728542a63effedb1d19f473168b9ab7ec8ef8f11c25538"
def assemble(baseline, donor, donor_sha, output, classpath):
existing = baseline.read_bytes()
compiled = donor.read_bytes()
if digest(existing) != BASELINE or digest(compiled) != donor_sha:
raise ValueError("Unexpected Builder baseline or donor checksum")
archive = read_archive(existing)
payload = read_archive(compiled)
if GUARD in archive:
raise ValueError("Builder already contains the shared profile guard")
java_home = os.environ.get("JAVA_HOME")
executable = "java.exe" if os.name == "nt" else "java"
java = str(Path(java_home) / "bin" / executable) if java_home else executable
helper = Path(__file__).with_name("BuilderSharedProfilesOverlay.java")
with TemporaryDirectory() as temp:
old, new = (Path(temp) / name for name in ("retained.class", "guarded.class"))
old.write_bytes(archive[BUILDER])
subprocess.run([java, "--class-path", classpath, str(helper), str(old), str(new)], check=True)
archive[BUILDER] = new.read_bytes()
archive[GUARD] = payload[GUARD]
with output.open("xb") as candidate:
candidate.write(write_archive(archive))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("baseline", type=Path)
parser.add_argument("donor", type=Path)
parser.add_argument("donor_sha")
parser.add_argument("output", type=Path)
parser.add_argument("classpath", help="ASM core and tree JARs")
assemble(**vars(parser.parse_args()))