115 lines
6.1 KiB
Python
115 lines
6.1 KiB
Python
"""Install the mushroom proximity guard and regenerate Ordos only."""
|
|
|
|
import argparse
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
from zipfile import ZIP_DEFLATED, ZipFile
|
|
|
|
from planet_ore_release import archive_names, sha
|
|
|
|
|
|
spec = importlib.util.spec_from_file_location('reset_planet_ores', Path(__file__).with_name('reset-planet-ores.py'))
|
|
reset = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(reset)
|
|
|
|
PLANET = 'sigma_draconis_iv'
|
|
UNIT = f'dune-{PLANET}.service'
|
|
TERRAIN = 'c1a564402a9cfea8be942d4945cdc55af36b7ffb441707ce683a177ab397494e'
|
|
PACK = '0116d155df34e6c9c0482c0f7e21404ea69ea1357819005acfdb110ab748b139'
|
|
LANDSCAPE = 'com/example/duneterrain/TemperateLandscape.class'
|
|
RANGE = 'com/example/duneterrain/OrdosSulfurRange.class'
|
|
ASSETS = {'OrdosSulfurRange.class', 'OrdosSulfurTransition.java', 'planet_ore_release.py',
|
|
'reset-planet-ores.py', 'reset-ordos-sulfur.py', 'SOURCE_COMMIT'}
|
|
|
|
|
|
def assemble(baseline, bundle, candidate, java):
|
|
if sha(baseline) != TERRAIN:
|
|
raise ValueError('Expected the exact retained Ordos Terrain')
|
|
with ZipFile(baseline) as original, tempfile.TemporaryDirectory() as temp:
|
|
if RANGE in archive_names(original):
|
|
raise ValueError('Ordos sulfur guard is already installed')
|
|
previous, updated = Path(temp) / 'previous.class', Path(temp) / 'candidate.class'
|
|
previous.write_bytes(original.read(LANDSCAPE))
|
|
subprocess.run([java, str(bundle / 'OrdosSulfurTransition.java'), str(previous), str(updated)], check=True)
|
|
with ZipFile(candidate, 'x', ZIP_DEFLATED) as incoming:
|
|
for entry in original.infolist():
|
|
incoming.writestr(entry, updated.read_bytes() if entry.filename == LANDSCAPE else original.read(entry))
|
|
incoming.writestr(RANGE, (bundle / 'OrdosSulfurRange.class').read_bytes())
|
|
verify_candidate(baseline, bundle, candidate)
|
|
|
|
|
|
def verify_candidate(baseline, bundle, candidate):
|
|
with ZipFile(baseline) as original, ZipFile(candidate) as incoming:
|
|
names = archive_names(original)
|
|
if archive_names(incoming) != names | {RANGE}:
|
|
raise ValueError('Unexpected assembled Ordos Terrain entries')
|
|
if incoming.read(LANDSCAPE) == original.read(LANDSCAPE):
|
|
raise ValueError('Ordos sulfur selector was not updated')
|
|
if incoming.read(RANGE) != (bundle / 'OrdosSulfurRange.class').read_bytes():
|
|
raise ValueError('Sulfur guard differs from the canonical build')
|
|
if any(incoming.read(name) != original.read(name) for name in names - {LANDSCAPE}):
|
|
raise ValueError('Unrelated Ordos Terrain entry changed')
|
|
|
|
|
|
def run(bundle, source, candidate, candidate_hash, apply, receipt):
|
|
root = reset.checked(reset.ROOT / PLANET)
|
|
if reset.state(UNIT) != 'active' or reset.service('show', UNIT, '--property=WorkingDirectory', '--value') != str(root):
|
|
raise ValueError('Unexpected Ordos service or working directory')
|
|
before = reset.snapshot_server(root)
|
|
existing = [name for name, details in before['jars'].items() if details[0] == TERRAIN]
|
|
if before['settings'].get('level-name') != PLANET or len(existing) != 1:
|
|
raise ValueError('Expected Ordos primary world and exact installed Terrain')
|
|
if not re.search(rf'^\s+generator:\s*[\'\"]?DuneTerrain:{PLANET}[\'\"]?\s*$', (root / 'bukkit.yml').read_text(), re.M):
|
|
raise ValueError('Expected the retained Ordos generator')
|
|
world = reset.checked(root / PLANET)
|
|
if reset.fingerprint(world / reset.PACK)[0] != PACK:
|
|
raise ValueError('Expected the currently deployed Ordos ore palette pack')
|
|
dimension = reset.checked(world / reset.DIMENSION)
|
|
identity = {name: reset.fingerprint(dimension / name) for name in reset.IDENTITY}
|
|
target = root / 'plugins' / existing[0]
|
|
previous = {target: reset.fingerprint(target)}
|
|
if not re.fullmatch(r'[0-9a-f]{64}', candidate_hash) or reset.fingerprint(candidate)[0] != candidate_hash:
|
|
raise ValueError('Assembled Terrain differs from the independently reviewed candidate hash')
|
|
verify_candidate(target, bundle, candidate)
|
|
report = {'source': source, 'planet': PLANET, 'world': str(world), 'unit': UNIT,
|
|
'identity': identity, 'incoming': {str(target): candidate_hash},
|
|
'chunkFileCount': len(reset.chunk_files(dimension)), 'status': 'planned'}
|
|
if not apply:
|
|
return report
|
|
return reset.apply_transition(root, world, dimension, UNIT, before, identity, previous,
|
|
{target: candidate}, report, receipt)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--bundle', required=True, type=Path)
|
|
parser.add_argument('--expected-source', required=True)
|
|
parser.add_argument('--candidate', required=True, type=Path)
|
|
parser.add_argument('--candidate-sha256')
|
|
parser.add_argument('--assemble-from', type=Path)
|
|
parser.add_argument('--java', default='java')
|
|
parser.add_argument('--apply-reset-no-backup', action='store_true')
|
|
parser.add_argument('--receipt', type=Path)
|
|
args = parser.parse_args()
|
|
reset.verify_bundle(reset.checked(args.bundle), args.expected_source, ASSETS)
|
|
if args.assemble_from is not None:
|
|
if args.apply_reset_no_backup or args.receipt is not None:
|
|
parser.error('Assembly is a separate offline operation')
|
|
assemble(args.assemble_from, args.bundle, args.candidate, args.java)
|
|
print(sha(args.candidate))
|
|
else:
|
|
if args.candidate_sha256 is None or args.apply_reset_no_backup != (args.receipt is not None):
|
|
parser.error('Planning requires a candidate hash; apply also requires a private receipt path')
|
|
params = (args.bundle, args.expected_source, args.candidate, args.candidate_sha256)
|
|
if args.apply_reset_no_backup:
|
|
import fcntl
|
|
with Path('/run/lock/dunerealms-planet-ore-reset.lock').open('a') as lock:
|
|
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
result = run(*params, True, args.receipt)
|
|
else:
|
|
result = run(*params, False, None)
|
|
print(json.dumps(result, indent=2))
|

Staff