#!/usr/bin/env python3 """Stage the optional flat-world test profile, without altering the regular pack.""" import json from pathlib import Path import re import shutil import zipfile from pack import ROOT, properties, read_toml, check_index, sha256, require version = properties()['mod_version'] source = ROOT / 'build/packwiz' destination = ROOT / 'build/packwiz-test' jar = ROOT / 'mods/sanctuary-test/build/libs' / f'sanctuary-test-{version}.jar' check_index(source) require(read_toml(source / 'pack.toml')['version'] == version, 'Regular staged pack is stale') with zipfile.ZipFile(jar) as archive: metadata = json.loads(archive.read('fabric.mod.json')) require(metadata['id'] == 'sanctuary_test' and metadata['version'] == version, 'Test module version drift') require(metadata['depends']['sanctuary'] == version, 'Test module needs the matching Sanctuary build') require('LICENSE_sanctuary_test' in archive.namelist(), 'Missing test module license') if destination.exists(): shutil.rmtree(destination) shutil.copytree(source, destination) shutil.copy2(jar, destination / 'mods' / jar.name) entries = read_toml(destination / 'index.toml')['files'] entries.append({'file': 'mods/' + jar.name, 'hash': sha256(jar)}) lines = ['hash-format = "sha256"', ''] for entry in sorted(entries, key=lambda e: e['file']): lines.append('[[files]]') lines.extend(key + ' = ' + json.dumps(value) for key, value in entry.items()) lines.append('') index = destination / 'index.toml' index.write_text('\n'.join(lines), encoding='utf-8') manifest = (source / 'pack.toml').read_text(encoding='utf-8').replace('name = "Sanctuary"', 'name = "Sanctuary Test"', 1) manifest, count = re.subn(r'(?m)^hash = "[a-f0-9]+"$', 'hash = "' + sha256(index) + '"', manifest) require(count == 1, 'Expected a single test pack index hash') (destination / 'pack.toml').write_text(manifest, encoding='utf-8') check_index(destination) print('Optional test pack staged at ' + str(destination))