chamesle-updater (3487B)
1 #!/usr/bin/python3 2 # tray icon that shows up when there are updates (system and flathub), click to open the store's updates tab 3 import gettext, os, re, subprocess, threading 4 5 import gi 6 gi.require_version("Gtk", "3.0") 7 gi.require_version("Notify", "0.7") 8 from gi.repository import GLib, Gtk, Notify 9 10 gettext.textdomain("chamesle") 11 _, ngettext = gettext.gettext, gettext.ngettext 12 13 FIRST, EVERY = 3 * 60, 6 * 60 * 60 14 AUTO_STATE = "/var/lib/chamesle/auto-update" 15 SEEN = os.path.join(GLib.get_user_cache_dir(), "chamesle-updater-seen") 16 17 18 def count(): 19 apk = sum(1 for line in subprocess.run(["apk", "version", "-l", "<"], capture_output=True, text=True).stdout.splitlines() 20 if re.search(r"\s<\s", line)) 21 flat = len(subprocess.run(["flatpak", "remote-ls", "--updates", "--app", "--columns=application"], 22 capture_output=True, text=True).stdout.split()) 23 return apk + flat 24 25 26 class Notifier: 27 def __init__(self): 28 Notify.init(_("Updates")) 29 self.icon = Gtk.StatusIcon(icon_name="software-update-available", visible=False) 30 self.icon.connect("activate", lambda *a: self.open()) 31 self.icon.connect("popup-menu", self.menu) 32 self.told = 0 33 GLib.timeout_add_seconds(FIRST, self.check) 34 35 def check(self): 36 self.auto_done() 37 threading.Thread(target=lambda: GLib.idle_add(self.show, count()), daemon=True).start() 38 GLib.timeout_add_seconds(EVERY, self.check) 39 return False 40 41 def show(self, n): 42 self.icon.set_visible(n > 0) 43 self.icon.set_tooltip_text(ngettext("%d update available", "%d updates available", n) % n) 44 # one notification per new batch, not every 6 hours 45 if n > self.told: 46 note = Notify.Notification.new(_("Updates available"), 47 ngettext("%d update ready to install.", "%d updates ready to install.", n) % n, 48 "software-update-available") 49 note.add_action("open", _("Update"), lambda *a: self.open()) 50 note.show() 51 self.told = n 52 53 def auto_done(self): 54 # once for each time updates installed on their own 55 try: 56 when, n = open(AUTO_STATE).read().split()[:2] 57 seen = open(SEEN).read().strip() if os.path.exists(SEEN) else "" 58 except (OSError, ValueError): 59 return 60 if when == seen: 61 return 62 with open(SEEN, "w") as f: 63 f.write(when) 64 if seen and int(n) > 0: 65 Notify.Notification.new(_("Chamesle updated itself"), 66 ngettext("%d update installed. Some start working after a restart.", 67 "%d updates installed. Some start working after a restart.", int(n)) % int(n), 68 "software-update-available").show() 69 70 def open(self): 71 subprocess.Popen(["chamesle-store", "--updates"]) 72 73 def menu(self, icon, button, time): 74 m = Gtk.Menu() 75 for label, cb in ((_("Show updates"), lambda *a: self.open()), (_("Check now"), lambda *a: self.check())): 76 item = Gtk.MenuItem(label=label) 77 item.connect("activate", cb) 78 m.append(item) 79 m.show_all() 80 m.popup(None, None, None, None, button, time) 81 82 83 if __name__ == "__main__": 84 if os.path.exists("/run/chamesle-live"): 85 raise SystemExit(0) 86 Notifier() 87 Gtk.main()