|
| 1 | +import re |
| 2 | + |
| 3 | +from zipfile import ZipFile, ZIP_DEFLATED |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +# Adapted from https://github.com/python/cpython/blob/3.13/Lib/zipfile/__init__.py#L2328 |
| 7 | +def addToZip(zf, path, zippath): |
| 8 | + if path.is_file(): |
| 9 | + zf.write(path, zippath, ZIP_DEFLATED) |
| 10 | + elif path.is_dir(): |
| 11 | + if zippath: |
| 12 | + zf.write(path, zippath) |
| 13 | + for nm in sorted(path.iterdir()): |
| 14 | + addToZip(zf, |
| 15 | + nm, zippath / nm.name) |
| 16 | + |
| 17 | +DECK_DIR_REGEXP = re.compile(r"^Ultimate Geography \[([A-Z-]*)\] ?(|\[Extended\]|\[Experimental\])$") |
| 18 | +def get_zip_filename(deck_dir, version): |
| 19 | + m = DECK_DIR_REGEXP.match(deck_dir) |
| 20 | + if m is not None: |
| 21 | + lang_code = m.group(1) |
| 22 | + deck_type = m.group(2) |
| 23 | + |
| 24 | + if deck_type == "[Extended]": |
| 25 | + deck_type = "_EXTENDED" |
| 26 | + elif deck_type == "[Experimental]": |
| 27 | + deck_type = "_EXPERIMENTAL" |
| 28 | + elif deck_type == "": |
| 29 | + deck_type = "" |
| 30 | + else: |
| 31 | + raise ValueError(f"Unknown deck type: {deck_type}") |
| 32 | + |
| 33 | + return f"Ultimate_Geography_{version}_{lang_code}{deck_type}.zip" |
| 34 | + |
| 35 | + else: |
| 36 | + raise ValueError(f"Unexpected deck directory name: {deck_dir}") |
| 37 | + |
| 38 | +# Allow passing version string manually! |
| 39 | +def zip_decks(aug_version=None): |
| 40 | + root = Path.cwd() |
| 41 | + |
| 42 | + build_dir = root / "build" |
| 43 | + |
| 44 | + desc_file = root / "src" / "headers" / "desc.html" |
| 45 | + if aug_version is None: |
| 46 | + with open(desc_file) as f: |
| 47 | + desc_text = f.read() |
| 48 | + m = re.search(r"Ultimate Geography (v[0-9]+\.[0-9]+)", desc_text) |
| 49 | + if m is None: |
| 50 | + raise ValueError("Description file does not contain version string!") |
| 51 | + aug_version = m.group(1) |
| 52 | + |
| 53 | + if build_dir.is_dir(): |
| 54 | + for deck_dir in build_dir.iterdir(): |
| 55 | + if deck_dir.is_dir(): |
| 56 | + deck_dir_name = deck_dir.name |
| 57 | + zip_name = build_dir / get_zip_filename(deck_dir_name, aug_version) |
| 58 | + with ZipFile(zip_name, "w") as zf: |
| 59 | + addToZip(zf, deck_dir, Path(deck_dir_name)) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + zip_decks() |
0 commit comments