From 384532539caa3e6da6196569bd75fac4f6dbe348 Mon Sep 17 00:00:00 2001 From: Christian Hopps Date: Fri, 13 Sep 2024 19:43:19 -0400 Subject: [PATCH] code: wait on tmux channel for no-tty cli operation Signed-off-by: Christian Hopps --- munet/cli.py | 22 +++++++++++++++++++++- pyproject.toml | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/munet/cli.py b/munet/cli.py index 01a7091..1b31f91 100644 --- a/munet/cli.py +++ b/munet/cli.py @@ -756,6 +756,8 @@ async def remote_cli(unet, prompt, title, background): unet.cli_sockpath = sockpath logging.info("server created on :\n%s\n", sockpath) + wait_tmux = bool(os.getenv("TMUX", "")) and not sys.stdin.isatty() + # Open a new window with a new CLI python_path = await unet.async_get_exec_path(["python3", "python"]) us = os.path.realpath(__file__) @@ -765,7 +767,25 @@ async def remote_cli(unet, prompt, title, background): if prompt: cmd += f" --prompt='{prompt}'" cmd += " " + unet.cli_sockpath - unet.run_in_window(cmd, title=title, background=False) + + channel = None + if wait_tmux: + from .base import Commander + + channel = "{}-{}".format(os.getpid(), Commander.tmux_wait_gen) + logger.info("XXX channel is %s", channel) + # If we don't have a tty to pause on pause for tmux windows to exit + if channel is not None: + Commander.tmux_wait_gen += 1 + + unet.run_in_window(cmd, title=title, background=False, wait_for=channel) + + if wait_tmux and channel: + from .base import commander + + await commander.async_cmd_raises( + [commander.get_exec_path("tmux"), "wait", channel] + ) except Exception as error: logging.error("cli server: unexpected exception: %s", error) diff --git a/pyproject.toml b/pyproject.toml index dc74d19..891f0e4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "munet" -version = "0.14.12" +version = "0.14.13" description = "A package to facilitate network simulations" authors = ["Christian Hopps "] license = "GPL-2.0-or-later"