Skip to content

Commit 7ce9d5b

Browse files
committed
!squash more server: raise_if_dead, is_alive()
1 parent 3362b44 commit 7ce9d5b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/libtmux/server.py

+27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"""
77
import logging
88
import os
9+
import shutil
10+
import subprocess
911
import typing as t
1012

1113
from libtmux.common import tmux_cmd
@@ -115,6 +117,31 @@ def __init__(
115117
if colors:
116118
self.colors = colors
117119

120+
def is_alive(self) -> bool:
121+
try:
122+
res = self.cmd("list-sessions")
123+
print(f"returncode: {res.returncode}")
124+
print(f"stdout: {res.stdout}")
125+
print(f"stderr: {res.stderr}")
126+
return res.returncode == 0
127+
except Exception:
128+
return False
129+
130+
def raise_if_dead(self) -> None:
131+
tmux_bin = shutil.which("tmux")
132+
if tmux_bin is None:
133+
raise exc.TmuxCommandNotFound()
134+
135+
cmd_args: t.List[str] = ["list-sessions"]
136+
if self.socket_name:
137+
cmd_args.insert(0, f"-L{self.socket_name}")
138+
if self.socket_path:
139+
cmd_args.insert(0, f"-S{self.socket_path}")
140+
if self.config_file:
141+
cmd_args.insert(0, f"-f{self.config_file}")
142+
143+
subprocess.check_call([tmux_bin] + cmd_args)
144+
118145
def cmd(self, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
119146
"""
120147
Execute tmux command and return output.

0 commit comments

Comments
 (0)