@@ -127,6 +127,12 @@ class tmux_cmd(object):
127127
128128 """:term:`tmux(1)` command via :py:mod:`subprocess`.
129129
130+ :param tmux_search_paths: Default PATHs to search tmux for, defaults to
131+ ``default_paths`` used in :func:`which`.
132+ :type tmux_search_path: list
133+ :param append_env_path: Append environment PATHs to tmux search paths.
134+ :type append_env_path: bool
135+
130136 Usage::
131137
132138 proc = tmux_cmd('new-session', '-s%' % 'my session')
@@ -150,7 +156,17 @@ class tmux_cmd(object):
150156 """
151157
152158 def __init__ (self , * args , ** kwargs ):
153- cmd = [which ('tmux' )]
159+ tmux_bin = which (
160+ 'tmux' ,
161+ default_paths = kwargs .get ('tmux_search_paths' , [
162+ '/bin' , '/sbin' , '/usr/bin' , '/usr/sbin' , '/usr/local/bin'
163+ ]),
164+ append_env_path = kwargs .get ('append_env_path' , True )
165+ )
166+ if not tmux_bin :
167+ raise (exc .TmuxCommandNotFound )
168+
169+ cmd = [tmux_bin ]
154170 cmd += args # add the command arguments to cmd
155171 cmd = [str (c ) for c in cmd ]
156172
@@ -187,8 +203,10 @@ def __init__(self, *args, **kwargs):
187203 if not self .stdout :
188204 self .stdout = self .stderr [0 ]
189205
190- logger .debug ('self.stdout for %s: \n %s' %
191- (' ' .join (cmd ), self .stdout ))
206+ logger .debug (
207+ 'self.stdout for %s: \n %s' %
208+ (' ' .join (cmd ), self .stdout )
209+ )
192210
193211
194212class TmuxMappingObject (collections .MutableMapping ):
@@ -329,10 +347,9 @@ def get_by_id(self, id):
329347 return None
330348
331349
332- def which (exe = None ,
333- default_paths = [
334- '/bin' , '/sbin' , '/usr/bin' , '/usr/sbin' , '/usr/local/bin' ]
335- ):
350+ def which (exe = None , default_paths = [
351+ '/bin' , '/sbin' , '/usr/bin' , '/usr/sbin' , '/usr/local/bin'
352+ ], append_env_path = True ):
336353 """Return path of bin. Python clone of /usr/bin/which.
337354
338355 from salt.util - https://www.github.com/saltstack/salt - license apache
@@ -341,6 +358,8 @@ def which(exe=None,
341358 :type exe: string
342359 :param default_path: Application to search PATHs for.
343360 :type default_path: list
361+ :param append_env_path: Append PATHs in environmental variables.
362+ :type append_env_path: bool
344363 :rtype: string
345364
346365 """
@@ -356,12 +375,15 @@ def _is_executable_file_or_link(exe):
356375 # Enhance POSIX path for the reliability at some environments, when
357376 # $PATH is changing. This also keeps order, where 'first came, first
358377 # win' for cases to find optional alternatives
359- search_path = os .environ .get ('PATH' ) and \
360- os .environ ['PATH' ].split (os .pathsep ) or list ()
378+ if append_env_path :
379+ search_path = os .environ .get ('PATH' ) and \
380+ os .environ ['PATH' ].split (os .pathsep ) or list ()
381+ else :
382+ search_path = []
383+
361384 for default_path in default_paths :
362385 if default_path not in search_path :
363386 search_path .append (default_path )
364- os .environ ['PATH' ] = os .pathsep .join (search_path )
365387 for path in search_path :
366388 full_path = os .path .join (path , exe )
367389 if _is_executable_file_or_link (full_path ):
0 commit comments