Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[close #59]: Creating subsystem does not show anything and just freezes #60

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 48 additions & 28 deletions apx_gui/core/apx_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from typing import Any
from collections.abc import Callable
from gi.repository import Vte, GLib
from gi.repository import Vte, GLib # type: ignore
from time import sleep


Expand All @@ -51,6 +51,13 @@ def _get_apx_command(self) -> str:
else:
return self.__apx_bin

def _get_apx_command_as_args(self) -> list[str]:
args: list[str] = []
if self._is_running_in_container():
args.append(self.__host_spawn_bin)
args.append("apx")
return args

@property
def __apx_bin(self) -> str:
"""
Expand Down Expand Up @@ -136,7 +143,6 @@ def create(self) -> tuple[bool, "Stack"]:
)
new_res: tuple[bool, str] = self._run_command(new_command)


list_command: str = f"apx stacks list --json"
list_res: tuple[bool, str] = self._run_command(list_command)
if not list_res[0]:
Expand Down Expand Up @@ -181,26 +187,31 @@ def __init__(
self.stack: Stack = stack
self.status: str = status
self.enter_command: list[str] = enter_command
if enter_command == []:
self.enter_command = shlex.split(f"{self._get_apx_command()} {name} enter")
self.exported_programs: dict[str, dict[str, str]] = exported_programs or {}

def create(
self,
_terminal,
terminal: Vte.Terminal,
) -> tuple[bool, "Subsystem"]:
new_command = (
f"{self._get_apx_command()}",
"subsystems",
"new",
"--name",
f"{self.name}",
"--stack",
f"{self.stack.name}",
new_command = self._get_apx_command_as_args()
new_command.extend(
[
"subsystems",
"new",
"--name",
f"{self.name}",
"--stack",
f"{self.stack.name}",
]
)
# the following apx command is safe to ignore errors, we´ll check the
# subsystem status by getting the list of subsystems
self.run_vte_command(new_command, _terminal, self._Create_Callback)
res: bool = self.run_vte_command(new_command, terminal, self._create_callback)
return res, self

def _create_callback(self,*args):
def _create_callback(self, *args):
list_command: str = f"subsystems list --json"
list_res: tuple[bool, str] = self._run_apx_command(list_command)
if not list_res[0]:
Expand All @@ -222,23 +233,33 @@ def _create_callback(self,*args):
def run_vte_command(
self,
args,
__terminal,
__callbackfunc,
) -> tuple[bool, str]:
terminal,
callback_fn,
) -> bool:
"""
Run the 'apx' command with the specified arguments.
"""
__terminal.connect("child-exited", __callbackfunc)
Term = __terminal.spawn_sync(
Vte.PtyFlags.DEFAULT,
".",
args,
[],
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
None,
None,
None,
)
terminal.connect("child-exited", callback_fn)

res: bool = False
try:
print(f"Running command: {args}")
res = terminal.spawn_sync(
Vte.PtyFlags.DEFAULT,
".",
args,
[],
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
None,
None,
None,
)
print(f"Spawn result: {res}")
except Exception as e:
print(f"Exception: {e}")
return False

return res

@property
def running(self) -> bool:
Expand Down Expand Up @@ -372,4 +393,3 @@ def update(
f"--update '{cmd_update}' --upgrade '{cmd_upgrade}'"
)
return self._run_apx_command(command)

6 changes: 3 additions & 3 deletions apx_gui/gtk/tab-subsystem.ui
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
<object class="AdwPreferencesGroup">
<property name="title" translatable="yes">Subsystem actions</property>
<child>
<object class="AdwActionRow" id="row_startstop">
<property name="activatable-widget">btn_startstop</property>
<object class="AdwActionRow" id="row_start_stop">
<property name="activatable-widget">btn_start_stop</property>
<child type="suffix">
<object class="GtkButton" id="btn_startstop">
<object class="GtkButton" id="btn_start_stop">
<property name="valign">center</property>
<style>
<class name="flat"/>
Expand Down
2 changes: 1 addition & 1 deletion apx_gui/widgets/entry_subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

from gi.repository import Gtk, Adw
from gi.repository import Gtk, Adw # pyright: ignore
from uuid import UUID

from gettext import gettext as _
Expand Down
18 changes: 9 additions & 9 deletions apx_gui/widgets/tab_subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

from gi.repository import Gtk, Gdk, GLib, Adw, Vte
from gi.repository import Gtk, Gdk, GLib, Adw, Vte # pyright: ignore
from uuid import UUID

from gettext import gettext as _
Expand All @@ -39,10 +39,10 @@ class TabSubsystem(Gtk.Box):
row_stack: Adw.ActionRow = Gtk.Template.Child() # pyright: ignore
row_pkgmanager: Adw.ActionRow = Gtk.Template.Child() # pyright: ignore
row_programs: Adw.ExpanderRow = Gtk.Template.Child() # pyright: ignore
row_startstop: Adw.ActionRow = Gtk.Template.Child() # pyright: ignore
row_start_stop: Adw.ActionRow = Gtk.Template.Child() # pyright: ignore
row_reset: Adw.ActionRow = Gtk.Template.Child() # pyright: ignore
row_delete: Adw.ActionRow = Gtk.Template.Child() # pyright: ignore
btn_startstop: Gtk.Button = Gtk.Template.Child() # pyright: ignore
btn_start_stop: Gtk.Button = Gtk.Template.Child() # pyright: ignore
btn_autoremove: Gtk.Button = Gtk.Template.Child() # pyright: ignore
btn_clean: Gtk.Button = Gtk.Template.Child() # pyright: ignore
btn_toggle_console: Gtk.Button = Gtk.Template.Child() # pyright: ignore
Expand All @@ -62,7 +62,7 @@ def __init__(
self.__aid: UUID = subsystem.aid
self.__subsystem: Subsystem = subsystem

self.btn_startstop.connect("clicked", self.__on_startstop_clicked)
self.btn_start_stop.connect("clicked", self.__on_start_stop_clicked)
self.btn_autoremove.connect("clicked", self.__on_autoremove_clicked)
self.btn_clean.connect("clicked", self.__on_clean_clicked)

Expand Down Expand Up @@ -92,11 +92,11 @@ def __rebuild_ui(self) -> None:
)

if self.subsystem.running:
self.row_startstop.set_title(_("Stop subsystem"))
self.btn_startstop.set_icon_name("media-playback-stop-symbolic")
self.row_start_stop.set_title(_("Stop subsystem"))
self.btn_start_stop.set_icon_name("media-playback-stop-symbolic")
else:
self.row_startstop.set_title(_("Start subsystem"))
self.btn_startstop.set_icon_name("media-playback-start-symbolic")
self.row_start_stop.set_title(_("Start subsystem"))
self.btn_start_stop.set_icon_name("media-playback-start-symbolic")

def __create_console(self) -> Vte.Terminal:
console: Vte.Terminal = Vte.Terminal()
Expand Down Expand Up @@ -211,7 +211,7 @@ def on_response(dialog: Adw.MessageDialog, response: str) -> None:
dialog.connect("response", on_response)
dialog.present()

def __on_startstop_clicked(self, button: Gtk.Button) -> None:
def __on_start_stop_clicked(self, button: Gtk.Button) -> None:
def on_response(dialog: Adw.MessageDialog, response: str) -> None:
if response == "ok":
self.__window.toast(
Expand Down
35 changes: 18 additions & 17 deletions apx_gui/windows/create_subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
# SPDX-License-Identifier: GPL-3.0-only

from gi.repository import Gtk, Adw, Vte, GLib, Gdk
from gi.repository import Gtk, Adw, Vte, Gdk # pyright: ignore
from gettext import gettext as _

from apx_gui.core.apx_entities import Subsystem, Stack
Expand Down Expand Up @@ -125,33 +125,34 @@ def __on_console_button(self, *args):
self.console_box_visible = not self.console_box_visible
self.console_box.set_visible(self.console_box_visible)


def __on_create_clicked(self, button: Gtk.Button) -> None:
def create_subsystem() -> tuple[bool, Subsystem]:
subsystem: Subsystem = Subsystem(
"",
self.row_name.get_text(),
self.__stacks[self.row_stack.get_selected()],
"",
[],
{},
)
self.NewSubsystem = subsystem
subsystem.create(self.__terminal)

button.set_visible(False)
self.stack_main.set_visible_child_name("creating")
create_subsystem()

subsystem: Subsystem = Subsystem(
"",
self.row_name.get_text(),
self.__stacks[self.row_stack.get_selected()],
"",
[],
{},
)
res, subsystem = subsystem.create(self.__terminal)
if not res:
self.stack_main.set_visible_child_name("error")
return

self.new_subsystem = subsystem

def on_vte_child_exited(self, terminal, status, *args):
terminal.get_parent().remove(terminal)
status = not bool(status)

if status:
self.__window.append_subsystem(self.NewSubsystem)
self.__window.append_subsystem(self.new_subsystem)
self.close()
self.__window.toast(
_("Subsystem {} created successfully").format(self.NewSubsystem.name)
_("Subsystem {} created successfully").format(self.new_subsystem.name)
)
return

Expand Down