File: bump_version.py

package info (click to toggle)
gajim 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,320 kB
  • sloc: python: 79,114; sh: 387; xml: 67; makefile: 6
file content (96 lines) | stat: -rwxr-xr-x 2,679 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3

import argparse
import re
import subprocess
import sys
from datetime import datetime
from pathlib import Path

REPO_DIR = Path(__file__).resolve().parent.parent


INIT = REPO_DIR / "gajim" / "__init__.py"
FLATPAK = REPO_DIR / "flatpak" / "org.gajim.Gajim.yaml"
APPDATA = REPO_DIR / "data" / "org.gajim.Gajim.metainfo.xml.in"
CHANGELOG = REPO_DIR / "ChangeLog"

VERSION_RX = r"\d+\.\d+\.\d+"


def get_current_version() -> str:
    with INIT.open("r") as f:
        content = f.read()

    match = re.search(VERSION_RX, content)
    if match is None:
        sys.exit("Unable to find current version")
    return match[0]


def bump_init(current_version: str, new_version: str) -> None:
    with INIT.open("r", encoding="utf8") as f:
        content = f.read()

    content = content.replace(current_version, new_version, 1)

    with INIT.open("w", encoding="utf8") as f:
        f.write(content)


def bump_flatpak(current_version: str, new_version: str) -> None:
    with FLATPAK.open("r", encoding="utf8") as f:
        content = f.read()

    content = content.replace(f"tag: {current_version}", f"tag: {new_version}", 1)

    with FLATPAK.open("w", encoding="utf8") as f:
        f.write(content)


def bump_appdata(new_version: str) -> None:
    with APPDATA.open("r", encoding="utf8") as f:
        lines = f.readlines()

    date = datetime.today().strftime("%Y-%m-%d")
    release_url = f"https://dev.gajim.org/gajim/gajim/-/blob/{new_version}/ChangeLog"
    release_string = (
        f'    <release version="{new_version}" date="{date}">\n'
        f'      <url type="details">{release_url}</url>\n'
        "    </release>"
    )

    with APPDATA.open("w", encoding="utf8") as f:
        for line in lines:
            f.write(line)
            if "<releases>" in line:
                f.write(release_string)
                f.write("\n")


def make_changelog(new_version: str) -> None:
    cmd = ["git-chglog", "--next-tag", new_version]

    result = subprocess.run(
        cmd, cwd=REPO_DIR, text=True, check=True, capture_output=True
    )

    changes = result.stdout
    changes = changes.removeprefix("\n")

    current_changelog = CHANGELOG.read_text()

    with CHANGELOG.open("w") as f:
        f.write(changes + current_changelog)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Bump Version")
    parser.add_argument("version", help="The new version, e.g. 1.5.0")
    args = parser.parse_args()

    current_version = get_current_version()
    bump_init(current_version, args.version)
    bump_flatpak(current_version, args.version)
    bump_appdata(args.version)
    make_changelog(args.version)