|
8 | 8 | import logging |
9 | 9 | import os |
10 | 10 | import re |
| 11 | +import shutil |
11 | 12 | import subprocess |
12 | 13 | import sys |
13 | 14 | import typing as t |
@@ -203,14 +204,6 @@ class tmux_cmd: |
203 | 204 | """ |
204 | 205 | :term:`tmux(1)` command via :py:mod:`subprocess`. |
205 | 206 |
|
206 | | - Parameters |
207 | | - ---------- |
208 | | - tmux_search_paths : list, optional |
209 | | - Default PATHs to search tmux for, defaults to ``default_paths`` used |
210 | | - in :func:`which`. |
211 | | - append_env_path : bool |
212 | | - Append environment PATHs to tmux search paths. True by default. |
213 | | -
|
214 | 207 | Examples |
215 | 208 | -------- |
216 | 209 |
|
@@ -239,14 +232,7 @@ class tmux_cmd: |
239 | 232 | """ |
240 | 233 |
|
241 | 234 | def __init__(self, *args: t.Any, **kwargs: t.Any) -> None: |
242 | | - tmux_bin = which( |
243 | | - "tmux", |
244 | | - default_paths=kwargs.get( |
245 | | - "tmux_search_paths", |
246 | | - ["/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin"], |
247 | | - ), |
248 | | - append_env_path=kwargs.get("append_env_path", True), |
249 | | - ) |
| 235 | + tmux_bin = shutil.which("tmux") |
250 | 236 | if not tmux_bin: |
251 | 237 | raise (exc.TmuxCommandNotFound) |
252 | 238 |
|
@@ -461,73 +447,6 @@ def get_by_id(self, id: str) -> Optional[O]: |
461 | 447 | return None |
462 | 448 |
|
463 | 449 |
|
464 | | -def which( |
465 | | - exe: str, |
466 | | - default_paths: t.List[str] = [ |
467 | | - "/bin", |
468 | | - "/sbin", |
469 | | - "/usr/bin", |
470 | | - "/usr/sbin", |
471 | | - "/usr/local/bin", |
472 | | - ], |
473 | | - append_env_path: bool = True, |
474 | | -) -> t.Optional[str]: |
475 | | - """ |
476 | | - Return path of bin. Python clone of /usr/bin/which. |
477 | | -
|
478 | | - Parameters |
479 | | - ---------- |
480 | | - exe : str |
481 | | - Application to search PATHs for. |
482 | | - default_paths : list |
483 | | - Paths to check inside of |
484 | | - append_env_path : bool, optional |
485 | | - Append list of directories to check in from PATH environment variable. |
486 | | - Default True. Setting False only for testing / diagnosing. |
487 | | -
|
488 | | - Returns |
489 | | - ------- |
490 | | - str |
491 | | - path of application, if found in paths. |
492 | | -
|
493 | | - Notes |
494 | | - ----- |
495 | | - from salt.util - https://www.github.com/saltstack/salt - license apache |
496 | | - """ |
497 | | - |
498 | | - def _is_executable_file_or_link(exe: str) -> bool: |
499 | | - # check for os.X_OK doesn't suffice because directory may executable |
500 | | - return os.access(exe, os.X_OK) and (os.path.isfile(exe) or os.path.islink(exe)) |
501 | | - |
502 | | - if _is_executable_file_or_link(exe): |
503 | | - # executable in cwd or fullpath |
504 | | - return exe |
505 | | - |
506 | | - # Enhance POSIX path for the reliability at some environments, when |
507 | | - # $PATH is changing. This also keeps order, where 'first came, first |
508 | | - # win' for cases to find optional alternatives |
509 | | - if append_env_path: |
510 | | - search_path = ( |
511 | | - os.environ.get("PATH") and os.environ["PATH"].split(os.pathsep) or list() |
512 | | - ) |
513 | | - else: |
514 | | - search_path = [] |
515 | | - |
516 | | - for default_path in default_paths: |
517 | | - if default_path not in search_path: |
518 | | - search_path.append(default_path) |
519 | | - for path in search_path: |
520 | | - full_path = os.path.join(path, exe) |
521 | | - if _is_executable_file_or_link(full_path): |
522 | | - return full_path |
523 | | - logger.info( |
524 | | - "'{}' could not be found in the following search path: " |
525 | | - "'{}'".format(exe, search_path) |
526 | | - ) |
527 | | - |
528 | | - return None |
529 | | - |
530 | | - |
531 | 450 | def get_version() -> LooseVersion: |
532 | 451 | """ |
533 | 452 | Return tmux version. |
|
0 commit comments