Skip to content

Commit a66cb9c

Browse files
authored
Merge pull request #684 from aplaice/zip_decks
Add script for zipping decks for releases/sharing
2 parents d017d75 + 3291ed4 commit a66cb9c

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,12 @@ Content changes, such as adding a note, replacing an image, or translating the d
392392
1. Run `pipenv run build`.
393393
1. In Anki, synchronise all your devices then upgrade the standard English deck by following the recommended procedure, which was agreed upon in the discussion thread. Synchronise all your devices again once the upgrade is complete.
394394
1. With the help of the Anki card browser, update the notes/cards stats in both `desc.html` and `README.md`, and commit the changes (including the version bump).
395-
1. Run `pipenv run build` again.
395+
1. Run `pipenv run build` again. If there is an experimental deck to be released, also run `pipenv run build_experimental`.
396396
1. Reimport the standard English deck in Anki and synchronise with AnkiWeb.
397397
1. Add each folder in the `build` directory to a separate ZIP archive named as follows:
398398
- `Ultimate Geography [EN]` ==> `Ultimate_Geography_v[x.y]_EN.zip`.
399399
- `Ultimate Geography [EN] [Extended]` ==> `Ultimate_Geography_v[x.y]_EN_EXTENDED.zip`.
400+
This can be done with `pipenv run zip`.
400401
1. On GitHub, create a new **release** named after the version number.
401402
1. Draft the release notes, making sure to add a link to the upgrade steps in the `README` and/or [in the wiki](https://github.com/anki-geo/ultimate-geography/wiki/Upgrade-instructions).
402403
1. Attach all the ZIP files and save the draft release notes.

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ python_version = "3.11"
1414
[scripts]
1515
build = "brain_brew run recipes/source_to_anki.yaml"
1616
build_experimental = "brain_brew run recipes/source_to_anki_[experimental].yaml"
17+
zip = "python utils/zip_decks.py"

utils/zip_decks.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)