Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .x-cmd/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# .x-cmd/Dockerfile — `po` CLI 的干净环境测试镜像
#
# 用法:
# docker build -f .x-cmd/Dockerfile -t po-test .
# docker run --rm po-test # 跑 E2E 测试(.x-cmd/test.sh,real 模式)
# docker run --rm po-test pytest tests/test_code/test_cli.py # 跑 pytest 套件
# docker run --rm po-test po --help # 直接体验 po 命令
# docker run --rm -it po-test bash # 交互 shell
#
# 说明:
# - 基于 python:3.10-slim,已装 PyMuPDF 运行时系统库(libgl1 / libglib2.0-0),
# 解决 slim 镜像缺 libGL.so.1 的问题
# - apt 与 pip 均走 aliyun 镜像(国内网络);海外环境可删除两处 mirror 配置
# - 只装核心 CLI 依赖(popdf/poexcel/poimage/pofile/wftools/pomarkdown/pocode),
# 全量 install_requires 含 poocr/pospider/moviepy 等重型包,安装极慢且非核心所需
# —— 这正是 office/__init__ 懒加载的价值:核心功能不依赖全家桶
# - 安装的是构建上下文里的本地代码;要测远端分支可改为:
# pip install git+https://github.com/x-cmd-sourcecode/python-office@x-cmd-261025-po-cli

FROM python:3.10-slim

# --- apt: aliyun 镜像 + PyMuPDF 运行时依赖 ---
RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g; s|security.debian.org|mirrors.aliyun.com|g' \
/etc/apt/sources.list.d/debian.sources 2>/dev/null; \
sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list 2>/dev/null; \
apt-get update -qq && \
apt-get install -y -qq --no-install-recommends libgl1 libglib2.0-0 && \
rm -rf /var/lib/apt/lists/*

WORKDIR /app

# --- 安装本仓库(提供 po 入口与版本元数据)+ 核心依赖 ---
COPY . /app/python-office
RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ \
--no-deps /app/python-office && \
pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ \
popdf poexcel poimage pofile wftools pomarkdown pocode poprogress \
rich loguru pytest

# --- 默认跑 E2E 测试(repo 副本优先于已安装副本) ---
ENV PYTHONPATH=/app/python-office
WORKDIR /app/python-office
CMD ["bash", ".x-cmd/test.sh"]
269 changes: 269 additions & 0 deletions .x-cmd/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# .x-cmd/test.sh — E2E smoke tests for the `po` CLI (python-office)
#
# Runs the command line the way a user does and checks exit codes, output
# shape and produced files.
#
# .x-cmd/test.sh # real mode when deps are installed,
# # otherwise stub mode automatically
# PO_TEST_STUB=1 .x-cmd/test.sh # force stub mode (no po* deps needed)
#
# Real mode exercises actual file transforms (txt2pdf → merge2pdf,
# fake2excel → excel2markdown …) and verifies the output files exist.
# Stub mode preloads MagicMock modules for the po* deps, so parser, help,
# version, platform guard and --json plumbing are still tested end-to-end.
# -----------------------------------------------------------------------------
set -u

SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
PY=${PO_PYTHON:-python3}
export PYTHONPATH="$REPO_ROOT${PYTHONPATH:+:$PYTHONPATH}"
cd "$REPO_ROOT"

PASS=0; FAIL=0; SKIP=0; N=0
WORK=$(mktemp -d /tmp/po-cli-test.XXXXXX)
trap 'rm -rf "$WORK"' EXIT

# --- helpers -------------------------------------------------------------------
result() { # result <ok|not-ok|skip> <description>
N=$((N + 1))
case "$1" in
ok) PASS=$((PASS + 1)); printf 'ok %3d - %s\n' "$N" "$2" ;;
not-ok) FAIL=$((FAIL + 1)); printf 'not ok %3d - %s\n' "$N" "$2" ;;
skip) SKIP=$((SKIP + 1)); printf 'skip %3d - %s\n' "$N" "$2" ;;
esac
}

# --- mode detection & launcher --------------------------------------------------
# office/__init__ lazy-loads the api modules, so the probe imports exactly the
# modules the core battery dispatches; when that fails we run the same entry
# point through a stub-preloading launcher (mirrors pytest).
STUBS_NEEDED=0
if ! "$PY" -c "import office.api.pdf, office.api.excel, office.api.tools, office.api.image, office.api.file, office.api.markdown, office.api.finance" >/dev/null 2>&1; then
STUBS_NEEDED=1
fi
[ "${PO_TEST_STUB:-0}" = "1" ] && STUBS_NEEDED=1

mkdir -p "$WORK/bin"
cat > "$WORK/bin/po_env.py" <<'PYEOF'
"""Shared launcher: optionally stub the po* deps, then run office.cli.main."""
import sys
from unittest import mock

STUB_NAMES = (
"popdf", "poexcel", "poimage", "pofile", "wftools", "povideo",
"poprogress", "poocr", "pocode", "pocode.api", "pocode.api.color",
"poemail", "pospider", "pomarkdown", "poword", "poppt",
"PyOfficeRobot", "search4file", "akshare", "moviepy",
)


def install_stubs():
import types
for name in STUB_NAMES:
if name in sys.modules:
continue
module = types.ModuleType(name)
module.__path__ = []
module.__getattr__ = lambda attr, _n=name: mock.MagicMock(name="%s.%s" % (_n, attr))
sys.modules[name] = module
# deterministic error-path fixture for the --json failure test
sys.modules["wftools"].url2ip = mock.MagicMock(side_effect=RuntimeError("stub boom"))


def main(argv):
want_dump = "--dump-registry" in argv
if want_dump:
argv.remove("--dump-registry")
if "--stub" in argv:
argv.remove("--stub")
install_stubs()
import os
if os.environ.get("PO_TEST_INJECT_GUARD") and not want_dump:
from office.cli import commands
commands.MODULES["wintest"] = {
"module": "office.api.never_imported",
"help": "platform guard probe (test only)",
"platforms": ["win32"],
"commands": {"probe": {"help": "probe", "params": []}},
}
if want_dump:
from office.cli import commands
for module_name in sorted(commands.MODULES):
for command_name in sorted(commands.MODULES[module_name]["commands"]):
print("%s %s" % (module_name, command_name))
return 0
from office.cli.main import main as cli_main
return cli_main(argv)


if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
PYEOF

if [ "$STUBS_NEEDED" = "1" ]; then
MODE=stub
po() { "$PY" "$WORK/bin/po_env.py" --stub "$@"; }
else
MODE=real
if command -v po >/dev/null 2>&1; then
po() { command po "$@"; } # installed entry point
else
po() { "$PY" -m office "$@"; } # repo checkout, deps installed
fi
fi
run_python() {
if [ "$STUBS_NEEDED" = "1" ]; then
"$PY" "$WORK/bin/po_env.py" --stub "$@"
else
"$PY" "$WORK/bin/po_env.py" "$@"
fi
}
printf '# po CLI smoke tests — mode: %s (python: %s)\n\n' "$MODE" "$("$PY" --version 2>&1)"

# --- 1. version / top-level help -------------------------------------------------
VOUT=$WORK/version.out
if po --version >"$VOUT" 2>"$WORK/version.err" && head -1 "$VOUT" | grep -q '^po '; then
result ok "po --version prints version (exit 0)"
else
result not-ok "po --version prints version (exit 0): $(cat "$VOUT" "$WORK/version.err" 2>/dev/null)"
fi

HOUT=$WORK/help.out
if po --help >"$HOUT" 2>&1 && grep -q "usage:" "$HOUT" \
&& grep -qw excel "$HOUT" && grep -qw image "$HOUT"; then
result ok "po --help lists modules"
else
result not-ok "po --help lists modules"
fi

# --- 2. per-module and per-command --help (all modules / 60+ commands) -----------
REGISTRY=$(run_python --dump-registry 2>/dev/null)
CMD_TOTAL=$(echo "$REGISTRY" | grep -c . || true)
if [ "${CMD_TOTAL:-0}" -ge 40 ]; then
result ok "registry dumps $CMD_TOTAL commands (>= 40)"
else
result not-ok "registry dumps >= 40 commands (got ${CMD_TOTAL:-0})"
fi

MODULE_HELP_FAIL=0
for module_name in $(echo "$REGISTRY" | awk '{print $1}' | sort -u); do
po "$module_name" --help >/dev/null 2>&1 || MODULE_HELP_FAIL=$((MODULE_HELP_FAIL + 1))
done
if [ "$MODULE_HELP_FAIL" = 0 ]; then
result ok "every module --help exits 0"
else
result not-ok "module --help failures: $MODULE_HELP_FAIL"
fi

CMD_HELP_FAIL=0
while read -r module_name command_name; do
po "$module_name" "$command_name" --help >/dev/null 2>&1 \
|| CMD_HELP_FAIL=$((CMD_HELP_FAIL + 1))
done <<EOF
$REGISTRY
EOF
if [ "$CMD_HELP_FAIL" = 0 ]; then
result ok "all $CMD_TOTAL command --help exit 0"
else
result not-ok "command --help failures: $CMD_HELP_FAIL/$CMD_TOTAL"
fi

# --- 3. usage errors & platform guard ---------------------------------------------
po pdf definitely-not-a-command >/dev/null 2>&1
[ $? = 2 ] && result ok "unknown command → exit 2 (usage error)" \
|| result not-ok "unknown command → exit 2 (usage error)"

po >/dev/null 2>&1
[ $? = 2 ] && result ok "no arguments → exit 2 (usage error)" \
|| result not-ok "no arguments → exit 2 (usage error)"

GUARD_ERR=$WORK/guard.err
PO_TEST_INJECT_GUARD=1 run_python wintest probe >/dev/null 2>"$GUARD_ERR"; RC=$?
if [ "$RC" = 1 ] && grep -q "only available on" "$GUARD_ERR"; then
result ok "platform guard blocks win-only module (exit 1, clean msg, no import)"
else
result not-ok "platform guard (rc=$RC): $(cat "$GUARD_ERR")"
fi

# --- 4. --json output shape (finance.t0 is pure stdlib → works in both modes) -----
T0=$WORK/t0.json
po --json finance t0 10 11 100 >"$T0" 2>/dev/null; RC=$?
LINES=$(wc -l < "$T0" | tr -d ' ')
if [ "$RC" = 0 ] && [ "$LINES" = 1 ] \
&& "$PY" - "$T0" <<'PYCHK'
import json, sys
d = json.load(open(sys.argv[1]))
assert d["ok"] is True and abs(float(d["result"]) - 88.9) < 0.01, d
PYCHK
then
result ok "--json finance t0: one-line JSON, ok=true, result≈88.9"
else
result not-ok "--json finance t0: rc=$RC $(cat "$T0")"
fi

# deterministic error path: stub mode raises via fixture; real mode feeds a
# nonexistent file to merge2pdf (popdf must raise, not silently succeed)
JERR=$WORK/jerr.json
if [ "$MODE" = stub ]; then
po --json tools url2ip example.com >/dev/null 2>"$JERR"; RC=$?
else
(cd "$WORK" && po --json pdf merge2pdf does-not-exist.pdf >/dev/null 2>"$JERR"); RC=$?
fi
JERR_LAST=$(tail -n 1 "$JERR") # 末行为 JSON;前面可能有库的导入期告警
if [ "$RC" = 1 ] && "$PY" -c '
import json, sys
d = json.loads(sys.argv[1])
assert d["ok"] is False and d["error"], d
' "$JERR_LAST"; then
result ok "--json error path: exit 1, stderr JSON ok=false (last line)"
else
result not-ok "--json error path (rc=$RC): $(cat "$JERR")"
fi

# --- 5. real file transforms (real mode only) --------------------------------------
if [ "$MODE" = real ]; then
cd "$WORK"

printf 'hello python-office\nline two\n' > a.txt
po pdf txt2pdf a.txt --output-file a.pdf >/dev/null 2>&1 \
&& [ -s a.pdf ] && result ok "pdf txt2pdf produces non-empty a.pdf" \
|| result not-ok "pdf txt2pdf produces non-empty a.pdf"

po pdf merge2pdf a.pdf a.pdf --output-file m.pdf >/dev/null 2>&1 \
&& [ -s m.pdf ] && result ok "pdf merge2pdf produces non-empty m.pdf" \
|| result not-ok "pdf merge2pdf produces non-empty m.pdf"

po excel fake2excel --columns name company --rows 5 --path f.xlsx >/dev/null 2>&1 \
&& [ -s f.xlsx ] && result ok "excel fake2excel produces non-empty f.xlsx" \
|| result not-ok "excel fake2excel produces non-empty f.xlsx"

po markdown excel2markdown f.xlsx --output-file f.md >/dev/null 2>&1 \
&& [ -s f.md ] && grep -q '|' f.md \
&& result ok "markdown excel2markdown produces table" \
|| result not-ok "markdown excel2markdown produces table"

PW=$WORK/pw.json
if po --json tools passwordtools --len 16 >"$PW" 2>/dev/null \
&& "$PY" - "$PW" <<'PYCHK'
import json, sys
d = json.load(open(sys.argv[1]))
assert d["ok"] and len(d["result"]) == 16, d
PYCHK
then
result ok "tools passwordtools --json returns 16-char password"
else
result not-ok "tools passwordtools --json: $(cat "$PW" 2>/dev/null)"
fi
else
result skip "pdf txt2pdf / merge2pdf (needs real deps)"
result skip "excel fake2excel (needs real deps)"
result skip "markdown excel2markdown (needs real deps)"
result skip "tools passwordtools real (needs real deps)"
fi

# --- summary -------------------------------------------------------------------------
printf '\n# %d passed, %d failed, %d skipped (mode=%s)\n' "$PASS" "$FAIL" "$SKIP" "$MODE"
[ "$FAIL" = 0 ]
16 changes: 16 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ pip install poexcel # 只用 Excel 处理

---

## 💻 命令行:`po` 命令

安装 python-office 后自带终端命令,不写代码也能用:

```bash
po pdf merge2pdf a.pdf b.pdf --output-file merged.pdf
po excel excel2pdf 报表.xlsx 报表.pdf
po tools url2ip www.python-office.com

po --json tools url2ip www.python-office.com # 单行 JSON 输出,方便脚本和 agent 调用
```

`po --help` 查看全部模块与子命令(`python -m office` 为等价入口)。

---

## 🚀 Quick Start(5 行代码体验)

```python
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ pip install poexcel # Excel processing only

---

## 💻 CLI: the `po` command

Installing python-office also gives you a terminal command — no Python code needed:

```bash
po pdf merge2pdf a.pdf b.pdf --output-file merged.pdf
po excel excel2pdf report.xlsx report.pdf
po tools url2ip www.python-office.com

po --json tools url2ip www.python-office.com # one-line JSON for scripts & agents
```

`po --help` lists all modules and commands (`python -m office` works as an alias).

---

## 🚀 Quick Start

```python
Expand Down
Loading