mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 22:46:20 -05:00
Allowing multiple names for which function.
This commit is contained in:
parent
a02d1c4527
commit
33fa1444c6
3 changed files with 15 additions and 7 deletions
|
|
@ -150,7 +150,7 @@ SINGLE_FILE_PATH = which('single-file',
|
|||
'/usr/bin/single-file', # rpi ubuntu
|
||||
'/usr/local/bin/single-file', # debian
|
||||
warn=True)
|
||||
CHROMIUM = which('chromium-browser',
|
||||
CHROMIUM = which('chromium-browser', 'chromium',
|
||||
'/usr/bin/chromium-browser', # rpi ubuntu
|
||||
'/usr/bin/chromium', # debian
|
||||
warn=True
|
||||
|
|
|
|||
|
|
@ -13,21 +13,22 @@ def is_executable(path: Path) -> bool:
|
|||
return path.is_file() and os.access(path, os.X_OK)
|
||||
|
||||
|
||||
def which(name: str, *possible_paths: str, warn: bool = False) -> Path:
|
||||
def which(*possible_paths: str, warn: bool = False) -> Path:
|
||||
"""
|
||||
Find an executable in the system $PATH. If the executable cannot be found in $PATH, then return the first
|
||||
executable found in possible_paths.
|
||||
|
||||
Returns None if no executable can be found.
|
||||
"""
|
||||
path = shutil.which(name)
|
||||
if path:
|
||||
return Path(path).absolute()
|
||||
|
||||
possible_paths = map(Path, possible_paths)
|
||||
for path in possible_paths:
|
||||
if is_executable(path):
|
||||
# `path` was an absolute file which is executable.
|
||||
return path.absolute()
|
||||
path = shutil.which(path)
|
||||
if path and (path := Path(path).absolute()) and is_executable(path):
|
||||
# `path` was a name or relative path.
|
||||
return path
|
||||
|
||||
if warn and not PYTEST and not DOCKERIZED:
|
||||
logger.warning(f'Cannot find executable {name}')
|
||||
logger.warning(f'Cannot find executable {possible_paths[0]}')
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import os
|
|||
from pathlib import Path
|
||||
|
||||
from wrolpi import cmd
|
||||
from wrolpi.test.common import skip_circleci
|
||||
|
||||
|
||||
def test_which(test_directory):
|
||||
|
|
@ -20,3 +21,9 @@ def test_which(test_directory):
|
|||
assert cmd.which('asdf', str(foo)) is None
|
||||
os.chmod(foo, 0o777)
|
||||
assert cmd.which('asdf', str(foo)) == foo
|
||||
|
||||
|
||||
@skip_circleci
|
||||
def test_which_chromium():
|
||||
# Multiple names can be tried.
|
||||
assert cmd.which('chromium-browser', 'chromium') == Path('/usr/bin/chromium')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue