Skip to content

Commit 9f76eff

Browse files
committed
Add history script
1 parent 5d5aeb3 commit 9f76eff

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

roentgen/history.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""Print the number of SVG files in the icons directory for each version tag."""
2+
3+
import subprocess
4+
from dataclasses import dataclass
5+
from pathlib import Path
6+
7+
from defusedxml.ElementTree import parse as parse_xml
8+
9+
REPO = Path(__file__).parent.parent
10+
TAGS = [f"v0.{minor}.0" for minor in range(1, 14)]
11+
12+
13+
@dataclass
14+
class Version:
15+
"""Project version."""
16+
17+
count: int
18+
"""Number of icons."""
19+
20+
new_icons: list[tuple[str, str | None]] | None = None
21+
"""New icons: (identifier, path commands)."""
22+
23+
24+
def git(*args: str) -> subprocess.CompletedProcess:
25+
"""Run Git command."""
26+
return subprocess.run( # noqa: S603
27+
["git", "-C", str(REPO), *args], # noqa: S607
28+
capture_output=True,
29+
text=True,
30+
check=True,
31+
)
32+
33+
34+
SVG_NS = "http://www.w3.org/2000/svg"
35+
36+
37+
def get_icons(tag: str) -> dict[str, str | None]:
38+
"""Get icon identifiers and path commands for a given version tag."""
39+
git("checkout", tag)
40+
icons_dir = REPO / "icons"
41+
if not icons_dir.exists():
42+
return {}
43+
result = {}
44+
for svg_file in icons_dir.glob("*.svg"):
45+
root = parse_xml(svg_file).getroot()
46+
path_el = root.find(f"{{{SVG_NS}}}path")
47+
icon_id = svg_file.stem.removeprefix("roentgen_")
48+
result[icon_id] = path_el.get("d") if path_el is not None else None
49+
return result
50+
51+
52+
def main() -> None:
53+
"""Print icon counts and new icons for each version tag."""
54+
original = git("rev-parse", "--abbrev-ref", "HEAD").stdout.strip()
55+
if original == "HEAD":
56+
original = git("rev-parse", "HEAD").stdout.strip()
57+
58+
versions: list[Version] = []
59+
60+
git("stash", "--include-untracked")
61+
62+
try:
63+
previous_ids: set[str] = set()
64+
for tag in TAGS:
65+
icons = get_icons(tag)
66+
new_icons = [
67+
(icon_id, icons[icon_id])
68+
for icon_id in sorted(icons.keys() - previous_ids)
69+
]
70+
versions.append(Version(count=len(icons), new_icons=new_icons))
71+
previous_ids = set(icons.keys())
72+
finally:
73+
git("checkout", original)
74+
git("stash", "pop")
75+
76+
for tag, version in zip(TAGS, versions, strict=False):
77+
print( # noqa: T201
78+
f"{tag}: {version.count} icons, {len(version.new_icons or [])} new"
79+
)
80+
81+
82+
if __name__ == "__main__":
83+
main()

0 commit comments

Comments
 (0)