54 lines
2.3 KiB
Python
54 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Validate and reproducibly package the repository's custom sets."""
|
|
from pathlib import Path
|
|
from generate_xml import generate
|
|
from publish import customsets_archive, sources
|
|
import xml.etree.ElementTree as ET
|
|
from zipfile import ZIP_DEFLATED, ZipFile, ZipInfo
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def main():
|
|
generated = ROOT / "dist/sets"
|
|
generate(ROOT / "sets", generated)
|
|
sets = sorted(path for path in generated.iterdir() if path.is_dir())
|
|
if not sets:
|
|
raise SystemExit("No set directories found.")
|
|
if any(folder.name.casefold() == 'customsets' for folder in sets):
|
|
raise SystemExit('Set code customsets conflicts with the manual customsets.zip archive.')
|
|
# Validate all sets before writing packages.
|
|
for folder in sets:
|
|
xml_files = sorted(folder.glob("*.xml"))
|
|
if not xml_files:
|
|
raise SystemExit(f"{folder.name}: no XML files found")
|
|
for xml_file in xml_files:
|
|
root = ET.parse(xml_file).getroot()
|
|
codes = {node.text for node in root.findall("./sets/set/name")}
|
|
if root.tag != "cockatrice_carddatabase" or folder.name not in codes:
|
|
raise SystemExit(f"{xml_file}: expected Cockatrice set {folder.name}")
|
|
output = ROOT / "dist"
|
|
output.mkdir(exist_ok=True)
|
|
for stale in output.glob("*.zip"):
|
|
if stale.name not in ({f"{folder.name}.zip" for folder in sets} | {"customsets.zip"}):
|
|
stale.unlink()
|
|
for folder in sets:
|
|
target = output / f"{folder.name}.zip"
|
|
with ZipFile(target, "w", compression=ZIP_DEFLATED) as archive:
|
|
for source in sorted(folder.rglob("*")):
|
|
if source.is_file():
|
|
entry = ZipInfo(source.relative_to(generated).as_posix())
|
|
entry.compress_type = ZIP_DEFLATED
|
|
entry.create_system = 3
|
|
entry.external_attr = 0o100644 << 16
|
|
archive.writestr(entry, source.read_bytes())
|
|
print(f"Built {target.relative_to(ROOT)}")
|
|
|
|
target = output / 'customsets.zip'
|
|
target.write_bytes(customsets_archive(sources(generated)))
|
|
print(f"Built {target.relative_to(ROOT)} (XML only, extract into customsets/)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|