summaryrefslogtreecommitdiff
path: root/raveos-updater/check.py
blob: 79a0ba344f6d47f1ed46d49634300c30389092aa (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
#!/usr/bin/env python3
"""RaveOS frissítés-ellenőrző.

A raveos-core-repo szinkron adatbázisából kiolvassa a RaveOS saját
csomagjainak legfrissebb verzióját, összeveti a telepített verziókkal,
és a /var/lib/raveos-updater/status.json fájlba írja az eredményt.

Használat:
  check.py          # pacman -Sy után ellenőriz (root; a systemd timer hívja)
  check.py --no-sync  # nem szinkronizál, a meglévő DB-t használja (pacman hook)
"""

import json
import os
import subprocess
import sys
from datetime import datetime

REPO = "raveos-core-repo"
STATUS_DIR = "/var/lib/raveos-updater"
STATUS_FILE = os.path.join(STATUS_DIR, "status.json")


def run(args):
    return subprocess.run(args, capture_output=True, text=True)


def main():
    sync = "--no-sync" not in sys.argv[1:]
    error = None

    if sync:
        r = run(["pacman", "-Sy", "--noconfirm"])
        if r.returncode != 0:
            error = "sync failed"

    installed = {}
    repo = {}

    try:
        for line in run(["pacman", "-Q"]).stdout.splitlines():
            name, _, version = line.partition(" ")
            if version:
                installed[name] = version

        for line in run(["pacman", "-Sl", REPO]).stdout.splitlines():
            parts = line.split()
            if len(parts) >= 3:
                repo[parts[1]] = parts[2]
    except FileNotFoundError:
        error = "pacman not found"

    updates = []
    for name, newver in sorted(repo.items()):
        oldver = installed.get(name)
        if oldver is None:
            continue
        r = run(["vercmp", newver, oldver])
        try:
            newer = int(r.stdout.strip()) > 0
        except (ValueError, AttributeError):
            continue
        if newer:
            updates.append({"name": name, "old": oldver, "new": newver})

    status = {
        "count": len(updates),
        "checked": datetime.now().astimezone().isoformat(timespec="seconds"),
        "updates": updates,
    }
    if error:
        status["error"] = error

    os.makedirs(STATUS_DIR, exist_ok=True)
    tmp = STATUS_FILE + ".tmp"
    with open(tmp, "w", encoding="utf-8") as f:
        json.dump(status, f, ensure_ascii=False, indent=2)
    os.replace(tmp, STATUS_FILE)
    os.chmod(STATUS_FILE, 0o644)


if __name__ == "__main__":
    main()