99
1010import logging
1111import re
12+ import shlex
1213import shutil
1314import subprocess
1415import sys
@@ -248,17 +249,24 @@ class tmux_cmd:
248249 Renamed from ``tmux`` to ``tmux_cmd``.
249250 """
250251
251- def __init__ (self , * args : t .Any ) -> None :
252- tmux_bin = shutil .which ("tmux" )
253- if not tmux_bin :
252+ def __init__ (self , * args : t .Any , tmux_bin : str | None = None ) -> None :
253+ resolved = tmux_bin or shutil .which ("tmux" )
254+ if not resolved :
254255 raise exc .TmuxCommandNotFound
255256
256- cmd = [tmux_bin ]
257+ cmd = [resolved ]
257258 cmd += args # add the command arguments to cmd
258259 cmd = [str (c ) for c in cmd ]
259260
260261 self .cmd = cmd
261262
263+ if logger .isEnabledFor (logging .DEBUG ):
264+ cmd_str = shlex .join (cmd )
265+ logger .debug (
266+ "tmux command dispatched" ,
267+ extra = {"tmux_cmd" : cmd_str },
268+ )
269+
262270 try :
263271 self .process = subprocess .Popen (
264272 cmd ,
@@ -269,11 +277,13 @@ def __init__(self, *args: t.Any) -> None:
269277 )
270278 stdout , stderr = self .process .communicate ()
271279 returncode = self .process .returncode
280+ except FileNotFoundError :
281+ raise exc .TmuxCommandNotFound from None
272282 except Exception :
273283 logger .error ( # noqa: TRY400
274284 "tmux subprocess failed" ,
275285 extra = {
276- "tmux_cmd" : subprocess . list2cmdline (cmd ),
286+ "tmux_cmd" : shlex . join (cmd ),
277287 },
278288 )
279289 raise
@@ -297,7 +307,7 @@ def __init__(self, *args: t.Any) -> None:
297307 logger .debug (
298308 "tmux command completed" ,
299309 extra = {
300- "tmux_cmd" : subprocess . list2cmdline (cmd ),
310+ "tmux_cmd" : shlex . join (cmd ),
301311 "tmux_exit_code" : self .returncode ,
302312 "tmux_stdout" : self .stdout [:100 ],
303313 "tmux_stderr" : self .stderr [:100 ],
@@ -307,7 +317,7 @@ def __init__(self, *args: t.Any) -> None:
307317 )
308318
309319
310- def get_version () -> LooseVersion :
320+ def get_version (tmux_bin : str | None = None ) -> LooseVersion :
311321 """Return tmux version.
312322
313323 If tmux is built from git master, the version returned will be the latest
@@ -316,12 +326,19 @@ def get_version() -> LooseVersion:
316326 If using OpenBSD's base system tmux, the version will have ``-openbsd``
317327 appended to the latest version, e.g. ``2.4-openbsd``.
318328
329+ Parameters
330+ ----------
331+ tmux_bin : str, optional
332+ Path to tmux binary. If *None*, uses the system tmux from
333+ :func:`shutil.which`.
334+
319335 Returns
320336 -------
321337 :class:`distutils.version.LooseVersion`
322- tmux version according to :func:`shtuil.which`'s tmux
338+ tmux version according to *tmux_bin* if provided, otherwise the
339+ system tmux from :func:`shutil.which`
323340 """
324- proc = tmux_cmd ("-V" )
341+ proc = tmux_cmd ("-V" , tmux_bin = tmux_bin )
325342 if proc .stderr :
326343 if proc .stderr [0 ] == "tmux: unknown option -- V" :
327344 if sys .platform .startswith ("openbsd" ): # openbsd has no tmux -V
@@ -346,93 +363,105 @@ def get_version() -> LooseVersion:
346363 return LooseVersion (version )
347364
348365
349- def has_version (version : str ) -> bool :
366+ def has_version (version : str , tmux_bin : str | None = None ) -> bool :
350367 """Return True if tmux version installed.
351368
352369 Parameters
353370 ----------
354371 version : str
355372 version number, e.g. '3.2a'
373+ tmux_bin : str, optional
374+ Path to tmux binary. If *None*, uses the system tmux.
356375
357376 Returns
358377 -------
359378 bool
360379 True if version matches
361380 """
362- return get_version () == LooseVersion (version )
381+ return get_version (tmux_bin = tmux_bin ) == LooseVersion (version )
363382
364383
365- def has_gt_version (min_version : str ) -> bool :
384+ def has_gt_version (min_version : str , tmux_bin : str | None = None ) -> bool :
366385 """Return True if tmux version greater than minimum.
367386
368387 Parameters
369388 ----------
370389 min_version : str
371390 tmux version, e.g. '3.2a'
391+ tmux_bin : str, optional
392+ Path to tmux binary. If *None*, uses the system tmux.
372393
373394 Returns
374395 -------
375396 bool
376397 True if version above min_version
377398 """
378- return get_version () > LooseVersion (min_version )
399+ return get_version (tmux_bin = tmux_bin ) > LooseVersion (min_version )
379400
380401
381- def has_gte_version (min_version : str ) -> bool :
402+ def has_gte_version (min_version : str , tmux_bin : str | None = None ) -> bool :
382403 """Return True if tmux version greater or equal to minimum.
383404
384405 Parameters
385406 ----------
386407 min_version : str
387408 tmux version, e.g. '3.2a'
409+ tmux_bin : str, optional
410+ Path to tmux binary. If *None*, uses the system tmux.
388411
389412 Returns
390413 -------
391414 bool
392415 True if version above or equal to min_version
393416 """
394- return get_version () >= LooseVersion (min_version )
417+ return get_version (tmux_bin = tmux_bin ) >= LooseVersion (min_version )
395418
396419
397- def has_lte_version (max_version : str ) -> bool :
420+ def has_lte_version (max_version : str , tmux_bin : str | None = None ) -> bool :
398421 """Return True if tmux version less or equal to minimum.
399422
400423 Parameters
401424 ----------
402425 max_version : str
403426 tmux version, e.g. '3.2a'
427+ tmux_bin : str, optional
428+ Path to tmux binary. If *None*, uses the system tmux.
404429
405430 Returns
406431 -------
407432 bool
408433 True if version below or equal to max_version
409434 """
410- return get_version () <= LooseVersion (max_version )
435+ return get_version (tmux_bin = tmux_bin ) <= LooseVersion (max_version )
411436
412437
413- def has_lt_version (max_version : str ) -> bool :
438+ def has_lt_version (max_version : str , tmux_bin : str | None = None ) -> bool :
414439 """Return True if tmux version less than minimum.
415440
416441 Parameters
417442 ----------
418443 max_version : str
419444 tmux version, e.g. '3.2a'
445+ tmux_bin : str, optional
446+ Path to tmux binary. If *None*, uses the system tmux.
420447
421448 Returns
422449 -------
423450 bool
424451 True if version below max_version
425452 """
426- return get_version () < LooseVersion (max_version )
453+ return get_version (tmux_bin = tmux_bin ) < LooseVersion (max_version )
427454
428455
429- def has_minimum_version (raises : bool = True ) -> bool :
456+ def has_minimum_version (raises : bool = True , tmux_bin : str | None = None ) -> bool :
430457 """Return True if tmux meets version requirement. Version >= 3.2a.
431458
432459 Parameters
433460 ----------
434461 raises : bool
435462 raise exception if below minimum version requirement
463+ tmux_bin : str, optional
464+ Path to tmux binary. If *None*, uses the system tmux.
436465
437466 Returns
438467 -------
@@ -456,12 +485,13 @@ def has_minimum_version(raises: bool = True) -> bool:
456485 Versions will now remove trailing letters per
457486 `Issue 55 <https://github.com/tmux-python/tmuxp/issues/55>`_.
458487 """
459- if get_version () < LooseVersion (TMUX_MIN_VERSION ):
488+ current_version = get_version (tmux_bin = tmux_bin )
489+ if current_version < LooseVersion (TMUX_MIN_VERSION ):
460490 if raises :
461491 msg = (
462492 f"libtmux only supports tmux { TMUX_MIN_VERSION } and greater. This "
463- f"system has { get_version () } installed. Upgrade your tmux to use "
464- "libtmux, or use libtmux v0.48.x for older tmux versions."
493+ f"system has { current_version } installed. Upgrade your "
494+ "tmux to use libtmux, or use libtmux v0.48.x for older tmux versions."
465495 )
466496 raise exc .VersionTooLow (msg )
467497 return False
0 commit comments