Naposledy aktivní 1704241163

xbps update checker for waybar

Revize 347f680a59d4efa0c2c1f087d59b50d23fac7be0

xbps-check-updates.py Raw
1#!/bin/env python
2
3### Add to ~/.config/waybar/config like so
4# "custom/xbps-updates": {
5# "format": "{}  ",
6# "interval": 3600,
7# "return-type": "json",
8# "exec": "xbps-checkup-updates | jq --unbuffered --compact-output",
9# "tooltip": true,
10# }
11
12import json
13import os
14from typing import Any, NamedTuple
15
16## updated with hourly cronjob that contains:
17# xbps-install -S
18# cd $HOME/void-packages && git pull
19# xbps-checkvers -I > /tmp/xbps_updates_available
20## by befault output is formatted like:
21# pkg name old_ver new_ver template name repository (if known)
22# minizip 1.2.13_1 1.3_1 minizip ?
23UPDATE_FILE = "/tmp/xbps_updates_available"
24
25
26class Update(NamedTuple):
27 package: str
28 current_ver: str
29 new_ver: str
30
31 def __str__(self) -> str:
32 return f"{self.package}: {self.current_ver} -> {self.new_ver}"
33
34
35def main():
36 total_packages = int(os.popen("xbps-query -l | wc -l").read())
37 output = {
38 "text": "",
39 "alt": "",
40 "tooltip": "",
41 "percentage": Any,
42 }
43 updates = []
44 # get list of updates from UPDATE_FILE
45 with open(UPDATE_FILE) as file:
46 for line in file:
47 split_line = line.split(" ")
48 updates.append(Update(split_line[0], split_line[1], split_line[2]))
49 num_updates = len(updates)
50
51 # format updates for waybar
52 output["text"] = str(num_updates)
53 if num_updates <= 0:
54 output["tooltip"] = "Up to date!"
55 else:
56 output["tooltip"] = f"Updates available:\n{'\n'.join(str(x) for x in updates)}"
57 output["alt"] = f"Updates: {num_updates} / {total_packages}"
58 output["percentage"] = num_updates / total_packages
59
60 print(json.dumps(output))
61
62
63if __name__ == "__main__":
64 main()
65