Skip to content

Commit 86773af

Browse files
committed
Unify exe_arg with file_arg
1 parent 9f4df24 commit 86773af

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

nancy/__init__.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -339,32 +339,15 @@ def find_on_path(start_path: Path, file: Path) -> Optional[Path]:
339339
return obj
340340
return None
341341

342-
def exe_arg(arg: bytes) -> Path:
343-
"""Find an executable file with the given name, or raise an error.
344-
345-
The input tree is searched first.
346-
If no file is found there, the system path is searched.
347-
348-
Args:
349-
arg (bytes): the name to search for.
350-
351-
Returns:
352-
Path: The filename found
353-
"""
354-
exe_name = Path(os.fsdecode(arg))
355-
exe_path = find_on_path(self.base_file.parent, exe_name)
356-
if exe_path is not None:
357-
return exe_path
358-
exe_path_str = shutil.which(exe_name)
359-
if exe_path_str is not None:
360-
return Path(exe_path_str)
361-
raise ValueError(f"cannot find program '{exe_name}'")
362-
363-
def file_arg(arg: bytes) -> Path:
342+
def file_arg(arg: bytes, exe=False) -> Path:
364343
"""Find a file with the given name, or raise an error.
365344
345+
The input tree is searched first. If no file is found there, and
346+
`exe`, the system `PATH` is searched for an executable file.
347+
366348
Args:
367349
arg (bytes): the name to search for.
350+
exe (bool): `True` to search the system `PATH`. Default `False`
368351
369352
Returns:
370353
Path: The filename found
@@ -373,9 +356,14 @@ def file_arg(arg: bytes) -> Path:
373356
file_path = find_on_path(self.base_file.parent, filename)
374357
if file_path is not None:
375358
return file_path
376-
raise ValueError(
377-
f"cannot find '{filename}' while expanding '{self.base_file.parent}'"
378-
)
359+
if not exe:
360+
raise ValueError(
361+
f"cannot find '{filename}' while expanding '{self.base_file.parent}'"
362+
)
363+
exe_path_str = shutil.which(filename)
364+
if exe_path_str is not None:
365+
return Path(exe_path_str)
366+
raise ValueError(f"cannot find program '{filename}'")
379367

380368
def do_expand(text: bytes) -> bytes:
381369
debug("do_expand")
@@ -437,7 +425,7 @@ def run(args: Optional[list[bytes]], input: Optional[bytes]) -> bytes:
437425
if args is None:
438426
raise ValueError("$run needs at least one argument")
439427
debug(command_to_str(b"run", args, input))
440-
exe_path = exe_arg(args[0])
428+
exe_path = file_arg(args[0], exe=True)
441429
exe_args = args[1:]
442430

443431
expanded_input = None

0 commit comments

Comments
 (0)