summarylogtreecommitdiffstats
path: root/update.py
blob: 3d7de98fb1b37be2046136ad02cd05e6fa63c823 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
# coding: utf-8
# SPDX-License-Identifier: Apache-2.0

"""
Update PKGBUILD from the Multiviewer release API.

So easy, you can update the package (almost) in your sleep!

:author: AntiCompositeNumber
"""

import argparse
import requests
import subprocess
import re
import sys


def get_download_data() -> tuple[str, str, str]:
    download_page_url = "https://api.multiviewer.dev/api/v1/releases/latest"
    r = requests.get(download_page_url)
    r.raise_for_status()

    data = r.json()
    release = next(
        release for release in data["downloads"] if release["platform"] == "linux"
    )
    url = release["url"]
    assert "linux-x64" in url
    assert url.endswith(".zip")
    return url, data["version"], str(release["id"])


def check_version(version: str) -> None:
    with open("PKGBUILD") as f:
        pkgbuild = f.read()
        old_version = re.search(r"pkgver=(.*)$", pkgbuild, re.MULTILINE)

    assert old_version is not None
    vercmp = subprocess.run(
        ["vercmp", old_version[1], version], capture_output=True, check=True
    )
    if vercmp.stdout.startswith(b"-"):
        return
    else:
        sys.exit(f"Version {old_version[1]} is already up to date.")


def check_domain(url: str) -> None:
    with open("PKGBUILD") as f:
        pkgbuild = f.read()

    old_domain = re.search(r"source=(\"(.*)\"", pkgbuild, re.MULTILINE)[1].split("/")[2]
    new_domain = url.split("/")[2]
    if old_domain != new_domain:
        print("Domains do not match, please change f{old_domain} to f{new_domain}")


def update_pkgbuild(url: str, version: str, build: str) -> None:
    with open("PKGBUILD", "r") as f:
        pkgbuild = f.read()
        pkgbuild = re.sub(
            r"pkgver=.*$", f"pkgver={version}", pkgbuild, flags=re.MULTILINE, count=1
        )
        pkgbuild = re.sub(
            r"_build=.*$", f"_build={build}", pkgbuild, flags=re.MULTILINE, count=1
        )
        pkgbuild = re.sub(
            r"pkgrel=.*$", "pkgrel=1", pkgbuild, flags=re.MULTILINE, count=1
        )
    with open("PKGBUILD", "w") as f:
        f.write(pkgbuild)


def main(argv):
    parser = argparse.ArgumentParser(
        description="Automatically update the version and download URL for the f1multiviewer-bin PKGBUIILD"
    )
    parser.add_argument(
        "-i", "--install", action="store_true", help="Install built package locally"
    )
    parser.add_argument(
        "-c",
        "--commit",
        action="store_true",
        help="Commit PKGBUILD changes to Git with an automatic commit message",
    )
    args = parser.parse_args(argv)

    url, version, build = get_download_data()
    print(f"Found v{version} at {url}")
    check_version(version)
    if input("Update PKGBUILD? [Y/n] ").lower() == "n":
        sys.exit(1)
    update_pkgbuild(url, version, build)
    subprocess.run("updpkgsums", check=True)
    subprocess.run("makepkg --printsrcinfo > .SRCINFO", shell=True, check=True)
    if args.install or input("Install new version? [Y/n]").lower() != "n":
        subprocess.run(["makepkg", "-i", "--clean"], check=True)
    else:
        subprocess.run(["makepkg", "--clean"], check=True)
    if args.commit or input("Commit changes? [Y/n]").lower() != "n":
        subprocess.run(
            ["git", "commit", "PKGBUILD", ".SRCINFO", "-m", version], check=True
        )
    else:
        subprocess.run(["git", "add", "PKGBUILD", ".SRCINFO"], check=True)
    print(
        "Update complete. Please verify PKGBUILD, commit any additional changes, and push to AUR."
    )


if __name__ == "__main__":
    main(sys.argv[1:])