89 lines
3.0 KiB
Bash
Executable File
89 lines
3.0 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
set -euo pipefail
|
||
|
||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||
RUNTIME_DIR="${SUPPLIER_SUMMARY_RUNTIME_DIR:-$REPO_ROOT/runtime/private/supplier-summary-scheduler}"
|
||
PASSWORD_FILE="${SUPPLIER_SUMMARY_PASSWORD_FILE:-$REPO_ROOT/runtime/private/supplier-summary-password}"
|
||
SOURCE_DIR="${SUPPLIER_SUMMARY_DIR:-$HOME/Desktop/加权平均单价}"
|
||
BASE_PYTHON="${SUPPLIER_SUMMARY_SYSTEM_PYTHON:-/usr/bin/python3}"
|
||
VENV_DIR="$RUNTIME_DIR/venv"
|
||
LABEL='hk.caiwuxitong.supplier-summary-import'
|
||
PLIST_PATH="$HOME/Library/LaunchAgents/$LABEL.plist"
|
||
RUN_SCRIPT="$REPO_ROOT/scripts/run-supplier-summary-import-macos.sh"
|
||
DOCKER_BIN="$(command -v docker || true)"
|
||
|
||
if [ -z "$DOCKER_BIN" ]; then
|
||
printf '找不到 docker 命令,无法安装供应商汇总定时任务。\n' >&2
|
||
exit 2
|
||
fi
|
||
DOCKER_DIR="$(dirname "$DOCKER_BIN")"
|
||
|
||
mkdir -p "$RUNTIME_DIR" "$HOME/Library/LaunchAgents"
|
||
chmod 700 "$RUNTIME_DIR"
|
||
|
||
if [ ! -f "$PASSWORD_FILE" ]; then
|
||
if [ -z "${SUPPLIER_SUMMARY_PASSWORD:-}" ]; then
|
||
printf '首次安装需设置 SUPPLIER_SUMMARY_PASSWORD 环境变量。\n' >&2
|
||
exit 2
|
||
fi
|
||
umask 077
|
||
printf '%s\n' "$SUPPLIER_SUMMARY_PASSWORD" > "$PASSWORD_FILE"
|
||
fi
|
||
chmod 600 "$PASSWORD_FILE"
|
||
|
||
if [ ! -x "$VENV_DIR/bin/python" ]; then
|
||
"$BASE_PYTHON" -m venv "$VENV_DIR"
|
||
fi
|
||
"$VENV_DIR/bin/python" -m pip install \
|
||
--disable-pip-version-check \
|
||
--quiet \
|
||
'msoffcrypto-tool>=5.4,<6'
|
||
|
||
"$BASE_PYTHON" - "$PLIST_PATH" "$LABEL" "$REPO_ROOT" "$RUN_SCRIPT" \
|
||
"$RUNTIME_DIR" "$SOURCE_DIR" "$DOCKER_DIR" <<'PY'
|
||
import plistlib
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
plist_path = Path(sys.argv[1])
|
||
label, repo_root, run_script, runtime_dir, source_dir, docker_dir = sys.argv[2:]
|
||
payload = {
|
||
"Label": label,
|
||
"ProgramArguments": ["/bin/bash", run_script],
|
||
"WorkingDirectory": repo_root,
|
||
"EnvironmentVariables": {
|
||
"PATH": f"{docker_dir}:/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin",
|
||
"SUPPLIER_SUMMARY_DIR": source_dir,
|
||
"SUPPLIER_SUMMARY_RUNTIME_DIR": runtime_dir,
|
||
},
|
||
"StartCalendarInterval": {
|
||
"Day": 16,
|
||
"Hour": 17,
|
||
"Minute": 0,
|
||
},
|
||
"RunAtLoad": False,
|
||
"StandardOutPath": str(Path(runtime_dir) / "launchd.out.log"),
|
||
"StandardErrorPath": str(Path(runtime_dir) / "launchd.err.log"),
|
||
}
|
||
with plist_path.open("wb") as handle:
|
||
plistlib.dump(payload, handle, sort_keys=False)
|
||
PY
|
||
|
||
chmod 644 "$PLIST_PATH"
|
||
chmod +x "$RUN_SCRIPT"
|
||
chmod +x "$SCRIPT_DIR/find_supplier_summary.py"
|
||
chmod +x "$SCRIPT_DIR/summarize_supplier_summary_result.py"
|
||
chmod +x "$SCRIPT_DIR/decrypt_supplier_summary.py"
|
||
|
||
domain="gui/$(id -u)"
|
||
launchctl bootout "$domain/$LABEL" >/dev/null 2>&1 || true
|
||
launchctl bootstrap "$domain" "$PLIST_PATH"
|
||
launchctl enable "$domain/$LABEL"
|
||
|
||
printf '已安装定时任务:%s\n' "$LABEL"
|
||
printf '执行时间:每月 16 日 17:00(导入上一个月)\n'
|
||
printf '源目录:%s\n' "$SOURCE_DIR"
|
||
printf 'LaunchAgent:%s\n' "$PLIST_PATH"
|