2020-04-13 13:12:41 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays APT package update information (<to upgrade>/<to remove >)
|
2020-04-13 13:25:08 +02:00
|
|
|
Requires the following packages:
|
2020-04-13 13:12:41 +02:00
|
|
|
* aptitude
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-04-13 13:25:08 +02:00
|
|
|
import re
|
2020-04-13 13:12:41 +02:00
|
|
|
import threading
|
|
|
|
|
2020-04-13 13:25:08 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.decorators
|
2020-04-13 13:12:41 +02:00
|
|
|
|
2020-04-13 13:25:08 +02:00
|
|
|
import util.cli
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
PATTERN = "{} packages upgraded, {} newly installed, {} to remove and {} not upgraded."
|
|
|
|
|
2020-04-13 13:12:41 +02:00
|
|
|
|
|
|
|
def parse_result(to_parse):
|
|
|
|
# We want to line with the iforamtion about package upgrade
|
2020-05-03 11:15:52 +02:00
|
|
|
line_to_parse = to_parse.split("\n")[-4]
|
|
|
|
result = re.search(
|
|
|
|
"(.+) packages upgraded, (.+) newly installed, (.+) to remove", line_to_parse
|
|
|
|
)
|
2020-04-13 13:12:41 +02:00
|
|
|
|
2020-04-13 13:25:08 +02:00
|
|
|
return int(result.group(1)), int(result.group(3))
|
2020-04-13 13:12:41 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-13 13:25:08 +02:00
|
|
|
def get_apt_check_info(module):
|
|
|
|
widget = module.widget()
|
2020-04-13 13:12:41 +02:00
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
res = util.cli.execute("aptitude full-upgrade --simulate --assume-yes")
|
|
|
|
widget.set("error", None)
|
2020-04-13 13:12:41 +02:00
|
|
|
except (RuntimeError, FileNotFoundError) as e:
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("error", "unable to query APT: {}".format(e))
|
2020-04-13 13:12:41 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
to_upgrade = 0
|
|
|
|
to_remove = 0
|
|
|
|
try:
|
|
|
|
to_upgrade, to_remove = parse_result(res)
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("to_upgrade", to_upgrade)
|
|
|
|
widget.set("to_remove", to_remove)
|
2020-04-13 13:25:08 +02:00
|
|
|
except Exception as e:
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("error", "parse error: {}".format(e))
|
|
|
|
|
|
|
|
core.event.trigger("update", [module.id], redraw_only=True)
|
2020-04-13 13:12:41 +02:00
|
|
|
|
|
|
|
|
2020-04-13 13:25:08 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.every(minutes=30)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.updates))
|
2020-04-13 13:25:08 +02:00
|
|
|
self.__thread = None
|
2020-04-13 13:12:41 +02:00
|
|
|
|
|
|
|
def updates(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
if widget.get("error"):
|
|
|
|
return widget.get("error")
|
|
|
|
return "{} to upgrade, {} to remove".format(
|
|
|
|
widget.get("to_upgrade", 0), widget.get("to_remove", 0)
|
2020-04-13 13:25:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def update(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
if self.__thread and self.__thread.isAlive():
|
|
|
|
return
|
2020-04-13 13:12:41 +02:00
|
|
|
|
2020-04-13 13:25:08 +02:00
|
|
|
self.__thread = threading.Thread(target=get_apt_check_info, args=(self,))
|
|
|
|
self.__thread.start()
|
2020-04-13 13:12:41 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
cnt = 0
|
2020-05-03 11:15:52 +02:00
|
|
|
ret = "good"
|
|
|
|
for t in ["to_upgrade", "to_remove"]:
|
2020-04-13 13:12:41 +02:00
|
|
|
cnt += widget.get(t, 0)
|
|
|
|
if cnt > 50:
|
2020-05-03 11:15:52 +02:00
|
|
|
ret = "critical"
|
2020-04-13 13:12:41 +02:00
|
|
|
elif cnt > 0:
|
2020-05-03 11:15:52 +02:00
|
|
|
ret = "warning"
|
|
|
|
if widget.get("error"):
|
|
|
|
ret = "critical"
|
2020-04-13 13:12:41 +02:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-13 13:12:41 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|