Repository navigation
Expand file tree
/
Copy pathtoolfind.py
More file actions
65 lines (50 loc) · 2.1 KB
/
Copy pathtoolfind.py
File metadata and controls
65 lines (50 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""toolfind.py — locate the helper programs the kit shells out to.
ffmpeg (movies) is optional. A release bundle
carries a portable ffmpeg under ``tools/`` next to the executables (see
``packaging/``); a source checkout may have one dropped into
``packaging/tools/``. Both are tried before PATH, so the bundled copy wins.
python toolfind.py # print what was found
python toolfind.py --self-test
"""
from __future__ import annotations
import os
import shutil
import sys
from pathlib import Path
from typing import Iterable, List, Optional
HERE = Path(__file__).resolve().parent
def _search_dirs() -> List[Path]:
dirs: List[Path] = []
if getattr(sys, "frozen", False):
dirs.append(Path(getattr(sys, "_MEIPASS", Path(sys.executable).resolve().parent)) / "tools")
dirs.append(Path(sys.executable).resolve().parent / "tools")
dirs += [HERE / "tools", HERE / "packaging" / "tools"]
return dirs
def find_tool(name: str, extra_dirs: Iterable[Path] = ()) -> Optional[str]:
"""Full path of ``name`` (``ffmpeg``), or None."""
exe = name + (".exe" if os.name == "nt" else "")
for d in list(extra_dirs) + _search_dirs():
p = Path(d) / exe
if p.is_file() and os.access(p, os.X_OK):
return str(p)
return shutil.which(name)
def self_test() -> int:
import tempfile
ok = True
def check(cond, msg):
nonlocal ok
print(("ok " if cond else "FAIL ") + msg)
ok &= bool(cond)
with tempfile.TemporaryDirectory() as td:
fake = Path(td) / ("fakeprog" + (".exe" if os.name == "nt" else ""))
fake.write_text("#!/bin/sh\n")
fake.chmod(0o755)
check(find_tool("fakeprog", [Path(td)]) == str(fake), "a bundled tools/ copy is found first")
check(find_tool("no-such-program-xs2") is None, "a missing program is None, not an error")
print("self-test", "passed" if ok else "FAILED")
return 0 if ok else 1
if __name__ == "__main__":
if sys.argv[1:] == ["--self-test"]:
sys.exit(self_test())
for n in ("ffmpeg",):
print(f"{n:11s} {find_tool(n) or 'not found'}")