@@ -127,6 +127,12 @@ class tmux_cmd(object):
127
127
128
128
""":term:`tmux(1)` command via :py:mod:`subprocess`.
129
129
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
+
130
136
Usage::
131
137
132
138
proc = tmux_cmd('new-session', '-s%' % 'my session')
@@ -150,7 +156,17 @@ class tmux_cmd(object):
150
156
"""
151
157
152
158
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 ]
154
170
cmd += args # add the command arguments to cmd
155
171
cmd = [str (c ) for c in cmd ]
156
172
@@ -187,8 +203,10 @@ def __init__(self, *args, **kwargs):
187
203
if not self .stdout :
188
204
self .stdout = self .stderr [0 ]
189
205
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
+ )
192
210
193
211
194
212
class TmuxMappingObject (collections .MutableMapping ):
@@ -329,10 +347,9 @@ def get_by_id(self, id):
329
347
return None
330
348
331
349
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 ):
336
353
"""Return path of bin. Python clone of /usr/bin/which.
337
354
338
355
from salt.util - https://www.github.com/saltstack/salt - license apache
@@ -341,6 +358,8 @@ def which(exe=None,
341
358
:type exe: string
342
359
:param default_path: Application to search PATHs for.
343
360
:type default_path: list
361
+ :param append_env_path: Append PATHs in environmental variables.
362
+ :type append_env_path: bool
344
363
:rtype: string
345
364
346
365
"""
@@ -356,12 +375,15 @@ def _is_executable_file_or_link(exe):
356
375
# Enhance POSIX path for the reliability at some environments, when
357
376
# $PATH is changing. This also keeps order, where 'first came, first
358
377
# 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
+
361
384
for default_path in default_paths :
362
385
if default_path not in search_path :
363
386
search_path .append (default_path )
364
- os .environ ['PATH' ] = os .pathsep .join (search_path )
365
387
for path in search_path :
366
388
full_path = os .path .join (path , exe )
367
389
if _is_executable_file_or_link (full_path ):
0 commit comments