|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Prepend a new <release> entry to the appdata template. |
| 3 | +
|
| 4 | +Pulls the version, date, and bullet list from the most recent |
| 5 | +debian/changelog entry. Intended to be run manually after a release |
| 6 | +has been tagged and the changelog updated. Review and edit the |
| 7 | +generated description before committing. |
| 8 | +""" |
| 9 | +import re |
| 10 | +import sys |
| 11 | +from email.utils import parsedate_to_datetime |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +REPO_ROOT = Path(__file__).resolve().parent |
| 15 | +CHANGELOG = REPO_ROOT / 'debian' / 'changelog' |
| 16 | +APPDATA = REPO_ROOT / 'data' / 'org.x.Warpinator.appdata.xml.in.in' |
| 17 | + |
| 18 | +HEADER_RE = re.compile(r'^\S+\s+\(([^)]+)\)') |
| 19 | +TRAILER_RE = re.compile(r'^ -- .+?<.+?>\s+(.+)$') |
| 20 | +BULLET_RE = re.compile(r'^\s*\*\s+(.+?)\s*$') |
| 21 | + |
| 22 | + |
| 23 | +def parse_top_entry(path): |
| 24 | + with open(path, 'r', encoding='utf-8') as f: |
| 25 | + lines = f.readlines() |
| 26 | + |
| 27 | + m = HEADER_RE.match(lines[0]) |
| 28 | + if not m: |
| 29 | + sys.exit(f"Couldn't parse version from first line of {path}") |
| 30 | + version = m.group(1) |
| 31 | + |
| 32 | + bullets = [] |
| 33 | + date = None |
| 34 | + for line in lines[1:]: |
| 35 | + trailer = TRAILER_RE.match(line) |
| 36 | + if trailer: |
| 37 | + date = parsedate_to_datetime(trailer.group(1).strip()).strftime('%Y-%m-%d') |
| 38 | + break |
| 39 | + bullet = BULLET_RE.match(line) |
| 40 | + if bullet: |
| 41 | + bullets.append(bullet.group(1)) |
| 42 | + |
| 43 | + if date is None: |
| 44 | + sys.exit(f"Couldn't find trailer line in top entry of {path}") |
| 45 | + |
| 46 | + return version, date, bullets |
| 47 | + |
| 48 | + |
| 49 | +def xml_escape(s): |
| 50 | + return s.replace('&', '&').replace('<', '<').replace('>', '>') |
| 51 | + |
| 52 | + |
| 53 | +def build_release_block(version, date, bullets): |
| 54 | + lines = [f' <release version="{version}" date="{date}">', |
| 55 | + ' <description>'] |
| 56 | + if len(bullets) <= 1: |
| 57 | + text = bullets[0] if bullets else 'Maintenance release.' |
| 58 | + lines.append(f' <p>{xml_escape(text)}</p>') |
| 59 | + else: |
| 60 | + lines.append(' <ul>') |
| 61 | + for b in bullets: |
| 62 | + lines.append(f' <li>{xml_escape(b)}</li>') |
| 63 | + lines.append(' </ul>') |
| 64 | + lines += [' </description>', ' </release>', ''] |
| 65 | + return '\n'.join(lines) |
| 66 | + |
| 67 | + |
| 68 | +def main(): |
| 69 | + version, date, bullets = parse_top_entry(CHANGELOG) |
| 70 | + |
| 71 | + text = APPDATA.read_text(encoding='utf-8') |
| 72 | + if re.search(rf'<release version="{re.escape(version)}"', text): |
| 73 | + sys.exit(f"Release {version} is already present in {APPDATA.name}") |
| 74 | + |
| 75 | + marker = ' <releases>\n' |
| 76 | + if marker not in text: |
| 77 | + sys.exit(f"Couldn't find <releases> block in {APPDATA.name}") |
| 78 | + |
| 79 | + block = build_release_block(version, date, bullets) |
| 80 | + APPDATA.write_text(text.replace(marker, marker + block, 1), encoding='utf-8') |
| 81 | + |
| 82 | + print(f"Added release {version} ({date}) to {APPDATA.relative_to(REPO_ROOT)}") |
| 83 | + print(f" bullets: {len(bullets)}") |
| 84 | + print("Review the description and edit wording before committing.") |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + main() |
0 commit comments