imrpoved message formatting and cleanup

This commit is contained in:
Tilman Kranz 2023-07-20 06:35:14 +02:00
parent 8aed22de69
commit 0f02715bac

View File

@ -1,16 +1,34 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# simple-apt-update - A GUI for basic tasks of package management using apt
# Copyright (C) 2023 Tilman Kranz <t.kranz@tk-sls.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
##
# Imports
import re
import queue
import threading
import gi import gi
import html import html
import logging import logging
import os import os
import queue
import re
import selectors import selectors
import signal import signal
import subprocess import subprocess
import sys import sys
import threading
gi.require_version('Gdk', '3.0') gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
@ -21,12 +39,6 @@ except ImportError:
logging.error("Could not import Gio") logging.error("Could not import Gio")
sys.exit(1) sys.exit(1)
# try:
# from gi.repository import GObject
# except ImportError:
# logging.error("Could not import GObject")
# sys.exit(1)
try: try:
from gi.repository import GLib from gi.repository import GLib
except ImportError: except ImportError:
@ -40,6 +52,9 @@ except ImportError:
sys.exit(1) sys.exit(1)
##
# Classes
class UpdateWindow(Gtk.ApplicationWindow): class UpdateWindow(Gtk.ApplicationWindow):
def clear(self): def clear(self):
self.buffer.set_text("") self.buffer.set_text("")
@ -100,6 +115,11 @@ class UpdateWindow(Gtk.ApplicationWindow):
empty_msg=None, env={}): empty_msg=None, env={}):
self.lock() self.lock()
self.clear() self.clear()
self.output_msg = output_msg
self.empty_msg = empty_msg
self.ignore_stderr = ignore_stderr
self.stdout = ''
self.stderr = ''
self.prepend_mesg( self.prepend_mesg(
"INFO", "INFO",
"Running command \"%s\" ..." % " ".join(args)) "Running command \"%s\" ..." % " ".join(args))
@ -181,14 +201,23 @@ class UpdateWindow(Gtk.ApplicationWindow):
def update_buffer(self): def update_buffer(self):
try: try:
text = self.stdout_queue.get(block=False) text = self.stdout_queue.get(block=False)
self.stdout += text
self.append_mesg("STDOUT", text) self.append_mesg("STDOUT", text)
match = re.fullmatch(r'EXIT (\d+)', text) match = re.fullmatch(r'EXIT (\d+)', text)
if match is not None: if match is not None:
exit_code = int(match.group(1)) exit_code = int(match.group(1))
if exit_code != 0: if exit_code != 0:
self.append_mesg( self.append_mesg(
"ERROR", "ERROR",
"Command exited with code %d" % exit_code) "Command exited with code %d" % exit_code)
elif self.stdout == '' and self.empty_msg is not None:
self.append_mesg("INFO", self.empty_msg)
elif self.stdout != '' and self.output_msg is not None:
self.append_mesg("INFO", self.empty_msg)
self.unlock() self.unlock()
@ -197,6 +226,10 @@ class UpdateWindow(Gtk.ApplicationWindow):
try: try:
text = self.stderr_queue.get(block=False) text = self.stderr_queue.get(block=False)
self.stderr += text
if not self.ignore_stderr:
self.append_mesg("STDERR", text) self.append_mesg("STDERR", text)
except queue.Empty: except queue.Empty:
pass pass
@ -306,6 +339,9 @@ class SimpleAptUpdate(Gtk.Application):
self.window.show_all() self.window.show_all()
##
# Main Program
def main(): def main():
application = SimpleAptUpdate() application = SimpleAptUpdate()
exit_status = application.run() exit_status = application.run()