dfslkjelf
This commit is contained in:
173
cmdnat/pipe.py
173
cmdnat/pipe.py
@@ -22,6 +22,77 @@ from config import get_local_storage_path, get_hydrus_access_key, get_hydrus_url
|
||||
_ALLDEBRID_UNLOCK_CACHE: Dict[str, str] = {}
|
||||
|
||||
|
||||
def _repo_root() -> Path:
|
||||
try:
|
||||
return Path(__file__).resolve().parent.parent
|
||||
except Exception:
|
||||
return Path(os.getcwd())
|
||||
|
||||
|
||||
def _repo_log_dir() -> Path:
|
||||
d = _repo_root() / "Log"
|
||||
try:
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
except Exception:
|
||||
pass
|
||||
return d
|
||||
|
||||
|
||||
def _helper_log_file() -> Path:
|
||||
return _repo_log_dir() / "medeia-mpv-helper.log"
|
||||
|
||||
|
||||
def _lua_log_file() -> Path:
|
||||
return _repo_log_dir() / "medeia-mpv-lua.log"
|
||||
|
||||
|
||||
def _try_enable_mpv_file_logging(mpv_log_path: str, *, attempts: int = 3) -> bool:
|
||||
"""Best-effort enable mpv log-file + verbose level on a running instance.
|
||||
|
||||
Note: mpv may not honor changing log-file at runtime on all builds/platforms.
|
||||
We still try; if it fails, callers can fall back to restart-on-demand.
|
||||
"""
|
||||
if not isinstance(mpv_log_path, str) or not mpv_log_path.strip():
|
||||
return False
|
||||
mpv_log_path = mpv_log_path.strip()
|
||||
|
||||
try:
|
||||
Path(mpv_log_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(mpv_log_path, "a", encoding="utf-8", errors="replace"):
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
ok = False
|
||||
for _ in range(max(1, int(attempts))):
|
||||
try:
|
||||
# Try to set log-file and verbose level.
|
||||
r1 = _send_ipc_command({"command": ["set_property", "options/log-file", mpv_log_path]})
|
||||
r2 = _send_ipc_command({"command": ["set_property", "options/msg-level", "all=v"]})
|
||||
ok = bool((r1 and r1.get("error") == "success") or (r2 and r2.get("error") == "success"))
|
||||
|
||||
# Emit a predictable line so the file isn't empty if logging is active.
|
||||
_send_ipc_command({"command": ["print-text", f"medeia: log enabled -> {mpv_log_path}"]}, silent=True)
|
||||
except Exception:
|
||||
ok = False
|
||||
|
||||
# If mpv has opened the log file, it should have content shortly.
|
||||
try:
|
||||
p = Path(mpv_log_path)
|
||||
if p.exists() and p.is_file() and p.stat().st_size > 0:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
import time
|
||||
time.sleep(0.15)
|
||||
except Exception:
|
||||
break
|
||||
|
||||
return bool(ok)
|
||||
|
||||
|
||||
def _get_alldebrid_api_key(config: Optional[Dict[str, Any]]) -> Optional[str]:
|
||||
try:
|
||||
if not isinstance(config, dict):
|
||||
@@ -838,10 +909,10 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
set_debug(True)
|
||||
set_thread_stream(sys.stdout)
|
||||
try:
|
||||
tmp_dir = Path(os.environ.get("TEMP") or os.environ.get("TMP") or ".")
|
||||
log_dir = _repo_log_dir()
|
||||
mpv_log_path = str((log_dir / "medeia-mpv.log").resolve())
|
||||
except Exception:
|
||||
tmp_dir = Path(".")
|
||||
mpv_log_path = str((tmp_dir / "medeia-mpv.log").resolve())
|
||||
mpv_log_path = str((Path(os.environ.get("TEMP") or os.environ.get("TMP") or ".") / "medeia-mpv.log").resolve())
|
||||
# Ensure file exists early so we can tail it even if mpv writes later.
|
||||
try:
|
||||
Path(mpv_log_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
@@ -851,6 +922,13 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
pass
|
||||
debug(f"MPV log file: {mpv_log_path}")
|
||||
|
||||
# Try to enable mpv file logging on the currently running instance.
|
||||
# (If mpv wasn't started with --log-file, this may not work everywhere.)
|
||||
try:
|
||||
_try_enable_mpv_file_logging(mpv_log_path, attempts=3)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# If mpv is already running, set log options live via IPC.
|
||||
try:
|
||||
mpv_live = MPV()
|
||||
@@ -899,6 +977,24 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
load_mode = parsed.get("load")
|
||||
current_mode = parsed.get("current")
|
||||
|
||||
# Pure log mode: `.pipe -log` should not run any playlist actions and
|
||||
# should not print the playlist table. It should only enable/tail logs
|
||||
# (handled in the `finally` block).
|
||||
only_log = bool(
|
||||
log_requested
|
||||
and not url_arg
|
||||
and index_arg is None
|
||||
and not clear_mode
|
||||
and not list_mode
|
||||
and not play_mode
|
||||
and not pause_mode
|
||||
and not save_mode
|
||||
and not load_mode
|
||||
and not current_mode
|
||||
)
|
||||
if only_log:
|
||||
return 0
|
||||
|
||||
# Handle --current flag: emit currently playing item to pipeline
|
||||
if current_mode:
|
||||
items = _get_playlist()
|
||||
@@ -959,6 +1055,19 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
# Fallback: just list the playlist if we can't determine index
|
||||
list_mode = True
|
||||
|
||||
# If the user explicitly requested -play while queueing a URL, interpret that
|
||||
# as "play the URL I just queued" (not merely "unpause whatever is currently playing").
|
||||
if play_mode and index_arg is None:
|
||||
if mpv_started:
|
||||
# MPV was just started; give it a moment, then play first item.
|
||||
import time
|
||||
time.sleep(0.5)
|
||||
index_arg = "1"
|
||||
else:
|
||||
playlist = _get_playlist(silent=True)
|
||||
if playlist and len(playlist) > 0:
|
||||
index_arg = str(len(playlist))
|
||||
|
||||
# Ensure lyric overlay is running (auto-discovery handled by MPV.lyric).
|
||||
try:
|
||||
mpv = MPV()
|
||||
@@ -1411,11 +1520,64 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
finally:
|
||||
if log_requested and isinstance(mpv_log_path, str) and mpv_log_path.strip():
|
||||
try:
|
||||
tail_lines = _tail_text_file(mpv_log_path, max_lines=160)
|
||||
# Give mpv a short moment to flush logs, then print a tail that is easy to copy.
|
||||
print(f"MPV log file: {mpv_log_path}")
|
||||
|
||||
# Best-effort: re-try enabling file logging at the end too (mpv may have
|
||||
# been unreachable at the start).
|
||||
try:
|
||||
_try_enable_mpv_file_logging(mpv_log_path, attempts=2)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
tail_lines: List[str] = []
|
||||
for _ in range(8):
|
||||
tail_lines = _tail_text_file(mpv_log_path, max_lines=200)
|
||||
if tail_lines:
|
||||
break
|
||||
try:
|
||||
import time
|
||||
time.sleep(0.25)
|
||||
except Exception:
|
||||
break
|
||||
|
||||
if tail_lines:
|
||||
print("MPV log (tail):")
|
||||
for ln in tail_lines:
|
||||
print(ln)
|
||||
else:
|
||||
print("MPV log (tail): <empty>")
|
||||
print("Note: On some Windows builds, mpv cannot start writing to --log-file after launch.")
|
||||
print("If you need full [main2] logs, restart mpv so it starts with --log-file.")
|
||||
|
||||
# Also print the helper log tail (this captures Python helper output that won't
|
||||
# necessarily show up in MPV's own log-file).
|
||||
try:
|
||||
helper_path = _helper_log_file()
|
||||
helper_tail = _tail_text_file(str(helper_path), max_lines=200)
|
||||
print(f"Helper log file: {str(helper_path)}")
|
||||
if helper_tail:
|
||||
print("Helper log (tail):")
|
||||
for ln in helper_tail:
|
||||
print(ln)
|
||||
else:
|
||||
print("Helper log (tail): <empty>")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Also print the Lua-side log tail (mp.msg output isn't always written to mpv's log-file).
|
||||
try:
|
||||
lua_path = _lua_log_file()
|
||||
lua_tail = _tail_text_file(str(lua_path), max_lines=200)
|
||||
print(f"Lua log file: {str(lua_path)}")
|
||||
if lua_tail:
|
||||
print("Lua log (tail):")
|
||||
for ln in lua_tail:
|
||||
print(ln)
|
||||
else:
|
||||
print("Lua log (tail): <empty>")
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
@@ -1486,8 +1648,7 @@ def _start_mpv(items: List[Any], config: Optional[Dict[str, Any]] = None, start_
|
||||
debug("Timed out waiting for MPV IPC connection", file=sys.stderr)
|
||||
return
|
||||
|
||||
# Ensure Lua script is loaded (redundant when started with --script, but safe)
|
||||
mpv.ensure_lua_loaded()
|
||||
# main.lua is loaded at startup via --script; don't reload it here.
|
||||
|
||||
# Ensure lyric overlay is running (auto-discovery handled by MPV.lyric).
|
||||
_ensure_lyric_overlay(mpv)
|
||||
|
||||
Reference in New Issue
Block a user