51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
from shutil import which
|
|
|
|
from tools.console import prompt_input
|
|
|
|
|
|
def require_executable(name: str) -> str:
|
|
executable = which(name)
|
|
if executable:
|
|
return executable
|
|
fallback = _windows_default_executable(name)
|
|
if fallback is not None:
|
|
print(f"\n{name} was not found on PATH; using {fallback}.")
|
|
return str(fallback)
|
|
if not sys.stdin.isatty():
|
|
raise RuntimeError(_missing_tool_message(name))
|
|
|
|
print(f"\n{name} was not found on PATH.")
|
|
while True:
|
|
answer = prompt_input(
|
|
f"Absolute path to {name} for this run (blank = cancel): "
|
|
).strip().strip('"')
|
|
if not answer:
|
|
raise RuntimeError(_missing_tool_message(name))
|
|
path = Path(answer).expanduser()
|
|
if not path.is_absolute():
|
|
print("Please enter an absolute path.")
|
|
elif not path.is_file():
|
|
print(f"File not found: {path}")
|
|
else:
|
|
return str(path.resolve())
|
|
|
|
|
|
def _missing_tool_message(name: str) -> str:
|
|
return (
|
|
f"{name} was not found on PATH. Install it and make sure the "
|
|
f"'{name}' command works before running this script."
|
|
)
|
|
|
|
|
|
def _windows_default_executable(name: str) -> Path | None:
|
|
if os.name != "nt" or name.casefold().removesuffix(".exe") != "mkvmerge":
|
|
return None
|
|
|
|
path = Path(r"C:\Program Files\MKVToolNix\mkvmerge.exe")
|
|
return path.resolve() if path.is_file() else None
|