Files
SceneMarshal/scripts/scene-marshal
T

127 lines
4.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# scene-marshal —— N100 上的唯一入口:把参数透传进 python 容器执行 SceneMarshal
#
# 用法:
# scene-marshal # 处理当前目录(auto)
# scene-marshal variety # 指定 main.py 模式
# scene-marshal -p "/hdd/sda1/Quark-Down/苍兰诀 (2022)"
# scene-marshal -p A -p B check # 多目录(一次确认/一份报告)
# scene-marshal archive -p X --to 电视节目/TV-日韩
# scene-marshal -h # 透传 main.py 帮助
#
# 说明:
# - 未给 -p/--path 且不是 -h 时,默认取当前目录;
# - 每个 -p 都单独 realpath 展开并逐个校验白名单(旧脚本只校验最后一个);
# - 有 TTY 时使用 docker exec -it(可交互确认/输入标题年份),无 TTY 时自动降级。
set -euo pipefail
CONTAINER="${SCENEMARSHAL_CONTAINER:-${MEDIA_RENAME_CONTAINER:-python}}"
# N100 上每个项目一个目录:/home/ali/docker/tools/python/SceneMarshal ↔ 容器 /python/SceneMarshal
PROJECT_DIR="${SCENEMARSHAL_PROJECT_DIR:-${MEDIA_PROJECT_DIR:-/python/SceneMarshal}}"
HOST_PROJECT="${SCENEMARSHAL_HOST_PROJECT:-${MEDIA_HOST_PROJECT:-/home/ali/docker/tools/python/SceneMarshal}}"
SCRIPT="$PROJECT_DIR/src/main.py"
ALLOWED_PREFIXES=(/hdd/sda1 /python)
die() { printf '\033[0;31m❌ %s\033[0m\n' "$*" >&2; exit 1; }
assert_allowed() {
local path="$1" prefix
for prefix in "${ALLOWED_PREFIXES[@]}"; do
if [[ "$path" == "$prefix" || "$path" == "$prefix"/* ]]; then
return 0
fi
done
die "路径越界: $path 未挂载给容器(仅允许 ${ALLOWED_PREFIXES[*]})"
}
args=()
targets=()
path_found=0
want_help=0
i=1
while (( i <= $# )); do
arg="${!i}"
case "$arg" in
-h|--help)
want_help=1
args+=("$arg")
;;
-p|--path)
args+=("$arg")
path_found=1
i=$((i + 1))
(( i <= $# )) || die "$arg 后面缺少路径参数"
raw="${!i}"
resolved="$(realpath -e -- "$raw" 2>/dev/null)" || die "路径不存在或无法解析: $raw"
assert_allowed "$resolved"
targets+=("$resolved")
args+=("$resolved")
;;
*)
args+=("$arg")
;;
esac
i=$((i + 1))
done
if (( ! path_found && ! want_help )); then
resolved="$(realpath -e -- "$(pwd)" 2>/dev/null)" || die "无法解析当前目录"
assert_allowed "$resolved"
targets+=("$resolved")
args+=("-p" "$resolved")
fi
exec_opts=(-u "${MEDIA_UID:-1000}:${MEDIA_GID:-1000}")
if [[ -t 0 && -t 1 ]]; then
exec_opts+=(-it)
fi
# docker exec 的客户端一旦断开,容器里的进程不会自动退出(会变孤儿继续跑)。
# 所以这里自己兜底:SSH 会话被挂断(SIGHUP)或收到 TERM/INT 时,
# 顺手把容器内同一批目标的进程结束掉(多个 -p 逐个匹配)。
cleanup_container() {
(( ${#targets[@]} )) || return 0
local patterns=() t
for t in "${targets[@]}"; do
patterns+=("$SCRIPT -p $t")
done
docker exec -u 1000:1000 "$CONTAINER" sh -c '
me=$$
for p in /proc/[0-9]*; do
pid=${p#/proc/}
[ "$pid" = "$me" ] && continue
[ -r "$p/cmdline" ] || continue
c=$(tr "\0" " " < "$p/cmdline" 2>/dev/null) || continue
for pat in "$@"; do
case "$c" in
*"$pat"*) kill -TERM "$pid" 2>/dev/null || true; break ;;
esac
done
done
' sh "${patterns[@]}" >/dev/null 2>&1 || true
}
trap 'cleanup_container; exit 129' HUP
trap 'cleanup_container; exit 130' INT
trap 'cleanup_container; exit 143' TERM
# 优先用项目内的 .venv;没有就退回容器系统 python(镜像已预装依赖)
PYTHON_BIN="${SCENEMARSHAL_PYTHON_BIN:-${MEDIA_PYTHON_BIN:-}}"
if [[ -z "$PYTHON_BIN" ]]; then
# 宿主机看 .venv/bin/python 是失效软链(指向容器内路径),只能查 pyvenv.cfg
if [[ -f "$HOST_PROJECT/.venv/pyvenv.cfg" ]]; then
PYTHON_BIN="$PROJECT_DIR/.venv/bin/python"
else
PYTHON_BIN="python3"
fi
fi
set +e
docker exec "${exec_opts[@]}" "$CONTAINER" "$PYTHON_BIN" "$SCRIPT" "${args[@]}"
code=$?
set -e
trap - HUP INT TERM
exit "$code"