6
6
"""
7
7
import logging
8
8
import os
9
+ import shutil
10
+ import subprocess
9
11
import typing as t
10
12
11
13
from libtmux .common import tmux_cmd
@@ -115,6 +117,31 @@ def __init__(
115
117
if colors :
116
118
self .colors = colors
117
119
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
+
118
145
def cmd (self , * args : t .Any , ** kwargs : t .Any ) -> tmux_cmd :
119
146
"""
120
147
Execute tmux command and return output.
@@ -207,7 +234,10 @@ def list_sessions(self) -> t.List[Session]:
207
234
@property
208
235
def sessions (self ) -> t .List [Session ]:
209
236
"""Property / alias to return :meth:`~.list_sessions`."""
210
- return self .list_sessions ()
237
+ try :
238
+ return self .list_sessions ()
239
+ except Exception :
240
+ return []
211
241
212
242
#: Alias :attr:`sessions` for :class:`~libtmux.common.TmuxRelationalObject`
213
243
children = sessions # type: ignore
@@ -348,7 +378,7 @@ def _update_panes(self) -> "Server":
348
378
return self
349
379
350
380
@property
351
- def attached_sessions (self ) -> t .Optional [ t . List [Session ] ]:
381
+ def attached_sessions (self ) -> t .List [Session ]:
352
382
"""
353
383
Return active :class:`Session` objects.
354
384
@@ -357,19 +387,22 @@ def attached_sessions(self) -> t.Optional[t.List[Session]]:
357
387
list of :class:`Session`
358
388
"""
359
389
360
- sessions = self ._sessions
361
- attached_sessions = list ()
390
+ try :
391
+ sessions = self ._sessions
392
+ attached_sessions = list ()
362
393
363
- for session in sessions :
364
- attached = session .get ("session_attached" )
365
- # for now session_active is a unicode
366
- if attached != "0" :
367
- logger .debug ("session %s attached" , session .get ("name" ))
368
- attached_sessions .append (session )
369
- else :
370
- continue
394
+ for session in sessions :
395
+ attached = session .get ("session_attached" )
396
+ # for now session_active is a unicode
397
+ if attached != "0" :
398
+ logger .debug ("session %s attached" , session .get ("name" ))
399
+ attached_sessions .append (session )
400
+ else :
401
+ continue
371
402
372
- return [Session (server = self , ** s ) for s in attached_sessions ] or None
403
+ return [Session (server = self , ** s ) for s in attached_sessions ] or []
404
+ except Exception :
405
+ return []
373
406
374
407
def has_session (self , target_session : str , exact : bool = True ) -> bool :
375
408
"""
0 commit comments