label listed updates in text output

This commit is contained in:
Tilman Kranz 2023-07-21 14:02:12 +02:00
parent 943af1a316
commit 22a18a2069

View File

@ -112,7 +112,7 @@ class UpdateWindow(Gtk.ApplicationWindow):
def insert(self, text, iter):
self.buffer.insert(iter, text + "\n")
def run_thread(self, args, env):
def run_thread(self, args, env, prefix=None):
if self.thread is None:
do_run = True
elif not self.thread.is_alive():
@ -123,7 +123,7 @@ class UpdateWindow(Gtk.ApplicationWindow):
if do_run:
self.thread = threading.Thread(
target=self.run,
args=(args, env,))
args=(args, env, prefix,))
self.thread.start()
@ -137,6 +137,7 @@ class UpdateWindow(Gtk.ApplicationWindow):
ignore_stderr=False,
output_msg=None,
empty_msg=None,
prefix=None,
env={},
clear=True
):
@ -154,9 +155,15 @@ class UpdateWindow(Gtk.ApplicationWindow):
"INFO",
"Running command \"%s\" ..." % " ".join(args))
GLib.timeout_add(250, self.run_thread, args, env)
GLib.timeout_add(
250,
self.run_thread,
args,
env,
prefix
)
def run(self, args, env={}):
def run(self, args, env={}, prefix=None):
p = subprocess.Popen(
args,
stdout=subprocess.PIPE,
@ -175,7 +182,10 @@ class UpdateWindow(Gtk.ApplicationWindow):
if data:
if key.fileobj is p.stdout:
self.stdout_queue.put(data)
if prefix is None:
self.stdout_queue.put(data)
else:
self.stdout_queue.put(prefix + " " + data)
else:
self.stderr_queue.put(data)
@ -222,7 +232,8 @@ class UpdateWindow(Gtk.ApplicationWindow):
args,
ignore_stderr=True,
clear=clear,
output_msg="Found the following package upgrades:",
prefix="UPDATE",
output_msg="Done listing available upgrades.",
empty_msg="No package upgrades found."
)
@ -238,29 +249,39 @@ class UpdateWindow(Gtk.ApplicationWindow):
def on_quit(self, *args):
self.application.quit()
def process_exit(self, exit_code, empty_msg=None, output_msg=None):
self.thread.join()
if exit_code != 0:
self.append_mesg(
"ERROR",
"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.output_msg)
self.unlock()
def update_buffer(self):
try:
text = self.stdout_queue.get(block=False)
match = re.fullmatch(r'EXIT (\d+)', text)
match = re.fullmatch(r'(EXIT|UPDATE) (.*)', text)
if match is None:
self.stdout += text
self.append_mesg("STDOUT", text)
elif match.group(1) == 'EXIT':
exit_code = int(match.group(2))
self.process_exit(exit_code)
elif match.group(1) == 'UPDATE':
text = match.group(2)
self.stdout += text
self.append_mesg("UPDATE", text)
else:
exit_code = int(match.group(1))
self.thread.join()
if exit_code != 0:
self.append_mesg(
"ERROR",
"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.output_msg)
self.unlock()
self.stdout += text
self.append_mesg("UPDATE", text)
except queue.Empty:
pass