#!/usr/bin/env python3 """Reconstruct the MIT JEI source fork from an immutable upstream commit and patch.""" from pathlib import Path import hashlib, io, shutil, subprocess, tarfile ROOT=Path(__file__).resolve().parents[1] COMMIT='aae2dfcfb82e6b5bce787ba72bb2eda7339b274f' PATCH=ROOT/'mods/jei/patches/26.3.patch' DEST=ROOT/'build/jei-fork' STAMP=COMMIT+':'+hashlib.sha256(PATCH.read_bytes()+Path(__file__).read_bytes()).hexdigest() if (DEST/'.sanctuary-source').exists() and (DEST/'.sanctuary-source').read_text()==STAMP: raise SystemExit(0) mirror=ROOT/'build/jei-26.2-upstream' if not (mirror/'.git').exists(): subprocess.run(['git','clone','--no-checkout','https://github.com/mezz/JustEnoughItems.git',str(mirror)],check=True) subprocess.run(['git','-C',str(mirror),'cat-file','-e',COMMIT+'^{commit}'],check=True) archive=subprocess.check_output(['git','-C',str(mirror),'archive',COMMIT]) tmp=ROOT/'build/jei-fork-preparing' if tmp.exists(): shutil.rmtree(tmp) tmp.mkdir(parents=True) with tarfile.open(fileobj=io.BytesIO(archive)) as tar: for entry in tar: target=(tmp/entry.name).resolve() if tmp.resolve() not in target.parents or not (entry.isfile() or entry.isdir()): raise RuntimeError('Unexpected upstream archive entry: '+entry.name) if entry.isdir(): target.mkdir(parents=True,exist_ok=True) else: target.parent.mkdir(parents=True,exist_ok=True) target.write_bytes(tar.extractfile(entry).read()) subprocess.run(['patch','-E','-p1','--batch','--forward','-i',str(PATCH)],cwd=tmp,check=True,stdout=subprocess.DEVNULL) (tmp/'.sanctuary-source').write_text(STAMP) if DEST.exists(): shutil.rmtree(DEST) tmp.rename(DEST) print('JEI source verified and patched: '+COMMIT)