From 8a50eb6f81ee31764950d70ecd47cb0ae17a784b Mon Sep 17 00:00:00 2001 From: Marco Genasci Date: Thu, 6 Jan 2022 08:06:57 +0100 Subject: [PATCH] New module emerge_status Display information about the currently running emerge process. Signed-off-by: Marco Genasci --- .../modules/contrib/emerge_status.py | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 bumblebee_status/modules/contrib/emerge_status.py diff --git a/bumblebee_status/modules/contrib/emerge_status.py b/bumblebee_status/modules/contrib/emerge_status.py new file mode 100644 index 0000000..3758585 --- /dev/null +++ b/bumblebee_status/modules/contrib/emerge_status.py @@ -0,0 +1,113 @@ +"""Display information about the currently running emerge process. + +Requires the following executable: + * emerge + +Parameters: + * emerge_status.format: Format string (defaults to '{current}/{total} {action} {category}/{pkg}') + +This code is based on emerge_status module from p3status [1] original created by AnwariasEu. + +[1] https://github.com/ultrabug/py3status/blob/master/py3status/modules/emerge_status.py +""" + +import re +import copy + +import core.module +import core.widget +import core.decorators + +import util.cli +import util.format + + +class Module(core.module.Module): + @core.decorators.every(seconds=10) + def __init__(self, config, theme): + super().__init__(config, theme, []) + self.__format = self.parameter( + "format", "{current}/{total} {action} {category}/{pkg}" + ) + self.__ret_default = { + "action": "", + "category": "", + "current": 0, + "pkg": "", + "total": 0, + } + + def update(self): + response = {} + ret = copy.deepcopy(self.__ret_default) + if self.__emerge_running(): + ret = self.__get_progress() + + widget = self.widget("status") + if not widget: + widget = self.add_widget(name="status") + + if ret["total"] == 0: + widget.full_text("emrg calculating...") + else: + widget.full_text( + " ".join( + self.__format.format( + current=ret["current"], + total=ret["total"], + action=ret["action"], + category=ret["category"], + pkg=ret["pkg"], + ).split() + ) + ) + else: + self.clear_widgets() + + def __emerge_running(self): + """ + Check if emerge is running. + Returns true if at least one instance of emerge is running. + """ + try: + util.cli.execute("pgrep emerge") + return True + except Exception: + return False + + def __get_progress(self): + """ + Get current progress of emerge. + Returns a dict containing current and total value. + """ + input_data = [] + ret = {} + + # traverse emerge.log from bottom up to get latest information + last_lines = util.cli.execute("tail -50 /var/log/emerge.log") + input_data = last_lines.split("\n") + input_data.reverse() + + for line in input_data: + if "*** terminating." in line: + # copy content of ret_default, not only the references + ret = copy.deepcopy(self.__ret_default) + break + else: + status_re = re.compile( + r"\((?P[\d]+) of (?P[\d]+)\) " + r"(?P[a-zA-Z/]+( [a-zA-Z]+)?) " + r"\((?P[\w\-]+)/(?P

[\w.]+)" + ) + res = status_re.search(line) + if res is not None: + ret["action"] = res.group("a").lower() + ret["category"] = res.group("ca") + ret["current"] = res.group("cu") + ret["pkg"] = res.group("p") + ret["total"] = res.group("t") + break + return ret + + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4