Compare commits

...

4 Commits

Author SHA1 Message Date
a578c0b7ac refactor: 统一构建/发布脚本输出样式,更简约
- 添加 build.sh 调用脚本
- 移除装饰性分隔线和标题
- 移除进度输出,只保留必要的错误提示
- 使用 >>> 前缀标识脚本步骤
2026-03-15 12:54:40 +08:00
78063b9e07 fix: 修正 pyarmor_runtime 目录位置到 scripts 内部
- 修改 build.py 中混淆后文件移动逻辑,先移动 scripts 目录,再将 pyarmor_runtime 移动到 scripts 内部
- 更新 spec.md 中关于混淆后文件结构的描述
- 更新 config.yaml 中测试规范,强调严禁跳过测试

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 12:42:08 +08:00
edbdeec90d fix: 支持从任意路径调用 lyxy_document_reader.py
- 从 __file__ 动态计算项目根目录
- 使用绝对路径引用 bootstrap.py
- 设置正确的 PYTHONPATH 和 cwd
- 添加路径解析测试

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 12:06:44 +08:00
a5c0b67360 refactor: 简化代码,消除重复逻辑
- 删除 tests/test_readers/conftest.py 中重复的 temp_html fixture
- 为 generate_uv_command/generate_python_command 添加 include_pyarmor 参数
- 新增 generate_uv_args 函数用于生成 subprocess 可用的参数列表
- lyxy_document_reader.py 复用 generate_uv_args 函数
2026-03-15 10:28:04 +08:00
10 changed files with 180 additions and 197 deletions

View File

@@ -58,27 +58,17 @@ def get_git_user_info() -> tuple[str, str]:
try:
name = get_git_config("user.name")
except subprocess.CalledProcessError:
print("""
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
错误: git user.name 未设置
请先配置 git 用户名:
git config --global user.name "Your Name"
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
""")
print("错误: git user.name 未设置")
print("请先配置 git 用户名:")
print(' git config --global user.name "Your Name"')
sys.exit(1)
try:
email = get_git_config("user.email")
except subprocess.CalledProcessError:
print("""
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
错误: git user.email 未设置
请先配置 git 邮箱:
git config --global user.email "your@email.com"
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
""")
print("错误: git user.email 未设置")
print("请先配置 git 邮箱:")
print(' git config --global user.email "your@email.com"')
sys.exit(1)
return name, email
@@ -92,10 +82,8 @@ def clean_and_create_build_dir(build_dir: str) -> None:
build_dir: 构建目录路径
"""
if os.path.exists(build_dir):
print(f"清理旧构建目录: {build_dir}")
shutil.rmtree(build_dir)
os.makedirs(build_dir)
print(f"创建构建目录: {build_dir}")
def copy_skill_md(source_path: str, target_dir: str, version: str, author: str) -> None:
@@ -203,8 +191,6 @@ def copy_skill_md(source_path: str, target_dir: str, version: str, author: str)
with open(target_path, "w", encoding="utf-8") as f:
f.write(new_content)
print(f"生成: {target_path} (version: {version}, author: {author})")
def obfuscate_scripts_dir(source_dir: str, target_dir: str) -> None:
"""
@@ -218,16 +204,9 @@ def obfuscate_scripts_dir(source_dir: str, target_dir: str) -> None:
try:
__import__("pyarmor")
except ImportError:
print("""
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
错误: PyArmor 未安装
请使用以下命令:
uv run --with pyarmor python build.py
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
""")
print("错误: PyArmor 未安装")
print("请使用以下命令:")
print(" uv run --with pyarmor python build.py")
sys.exit(1)
# 临时目录
@@ -246,8 +225,6 @@ def obfuscate_scripts_dir(source_dir: str, target_dir: str) -> None:
source_dir
]
print(f" 执行: {' '.join(cmd)}")
try:
result = subprocess.run(
cmd,
@@ -256,38 +233,49 @@ def obfuscate_scripts_dir(source_dir: str, target_dir: str) -> None:
text=True
)
except subprocess.CalledProcessError as e:
print(f"\nPyArmor 混淆失败:")
print("错误: PyArmor 混淆失败")
print(f" 返回码: {e.returncode}")
print(f" 标准输出: {e.stdout}")
print(f" 错误输出: {e.stderr}")
sys.exit(1)
# 移动混淆后的文件到最终位置
scripts_dst_dir = os.path.join(target_dir, "scripts")
pyarmor_runtime_dir = None
# 先移动 scripts 目录
for item in os.listdir(temp_dir):
src = os.path.join(temp_dir, item)
if item == "scripts":
dst = os.path.join(target_dir, item)
if os.path.exists(dst):
if os.path.isdir(dst):
shutil.rmtree(dst)
else:
os.remove(dst)
shutil.move(src, dst)
elif item.startswith("pyarmor_runtime"):
pyarmor_runtime_dir = item
# 再移动 pyarmor_runtime 到 scripts 内部
if pyarmor_runtime_dir:
src = os.path.join(temp_dir, pyarmor_runtime_dir)
dst = os.path.join(scripts_dst_dir, pyarmor_runtime_dir)
if os.path.exists(dst):
if os.path.isdir(dst):
shutil.rmtree(dst)
else:
os.remove(dst)
shutil.move(src, dst)
# 清理临时目录
os.rmdir(temp_dir)
print(" 混淆完成")
def main() -> None:
"""
主函数:执行完整的混淆打包流程
"""
print("=" * 60)
print("Skill 打包构建 (混淆模式)")
print("=" * 60)
# 路径配置
project_root = os.path.dirname(os.path.abspath(__file__))
@@ -297,37 +285,19 @@ def main() -> None:
# 生成版本号
version = generate_timestamp()
print(f"版本号: {version}")
# 读取 git 用户信息
git_name, git_email = get_git_user_info()
author = f"{git_name} <{git_email}>"
print(f"作者: {author}")
print()
# 清理并创建 build 目录
clean_and_create_build_dir(build_dir)
print()
# 复制 SKILL.md动态注入元数据
copy_skill_md(skill_md_path, build_dir, version, author)
print()
# 混淆代码
print("────────────────────────────────────────")
print(" 使用 PyArmor 混淆代码 (Normal Mode)")
print("────────────────────────────────────────")
obfuscate_scripts_dir(scripts_source_dir, build_dir)
print()
# 完成信息
print("=" * 60)
print("构建完成!")
print(f"版本号: {version}")
print(f"作者: {author}")
print("混淆模式: 已生成 .pyx 和 pyarmor_runtime")
print(f"输出目录: {build_dir}")
print("=" * 60)
if __name__ == "__main__":

15
build.sh Normal file
View File

@@ -0,0 +1,15 @@
#!/bin/bash
#
# 混淆构建脚本
#
# 使用方式:
# ./build.sh
#
set -e
cd "$(dirname "$0")"
echo ">>> 构建"
uv run --with pyarmor python build.py
echo ">>> 完成"

View File

@@ -8,7 +8,7 @@ context: |
- 主机环境: 禁止污染配置,需操作须请求用户
- 开发文档: README.md,每次迭代按需更新开发文档; 禁emoji/特殊字符
- skill文档: SKILL.md,每次迭代按需更新skill文档
- 测试: 所有需求必须设计全面测试
- 测试: 所有需求必须设计全面测试,严禁跳过测试,无法进行的测试交用户决策
- 任务: 除非用户直接要求,禁止创建git变更任务(push/commit等); git读取允许(status/log/diff等)
- 代码: 模块文件150-300行; 错误需自定义异常+清晰信息+位置上下文
- 项目阶段: 未上线,无用户,破坏性变更无需迁移说明

View File

@@ -70,7 +70,7 @@
- **THEN** 系统不需要 --obfuscate 参数,直接执行混淆构建
### Requirement: PyArmor 混淆执行
系统 SHALL 调用 PyArmor 工具对 scripts 目录进行混淆。
系统 SHALL 调用 PyArmor 工具对 scripts 目录进行混淆,然后将 pyarmor_runtime 目录移动到 scripts 内部
#### Scenario: PyArmor 成功执行
- **WHEN** PyArmor 可用
@@ -78,7 +78,7 @@
#### Scenario: 混淆后文件输出
- **WHEN** PyArmor 混淆完成
- **THEN** build/ 目录包含混淆后的文件和 pyarmor_runtime 子目录
- **THEN** build/ 目录包含混淆后的 scripts 目录,且 pyarmor_runtime 子目录位于 scripts/ 内部
### Requirement: PyArmor 未安装友好提示
系统 SHALL 在 PyArmor 未安装时提供清晰的错误提示,引导用户正确使用 `uv run --with pyarmor`

View File

@@ -28,14 +28,9 @@ def check_build_dir(build_dir: str) -> None:
SystemExit: 目录不存在时退出
"""
if not os.path.exists(build_dir):
print("""
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
错误: build/ 目录不存在
请先运行 build.py:
uv run python build.py
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
""")
print("错误: build/ 目录不存在")
print("请先运行 build.py:")
print(" uv run python build.py")
sys.exit(1)
@@ -50,14 +45,9 @@ def check_build_skill_md(build_skill_md_path: str) -> None:
SystemExit: 文件不存在时退出
"""
if not os.path.exists(build_skill_md_path):
print("""
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
错误: build/SKILL.md 不存在
请先运行 build.py:
uv run python build.py
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
""")
print("错误: build/SKILL.md 不存在")
print("请先运行 build.py:")
print(" uv run python build.py")
sys.exit(1)
@@ -101,13 +91,8 @@ def parse_version_from_skill_md(skill_md_path: str) -> str:
# metadata 块结束
in_metadata = False
print("""
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
错误: 无法从 build/SKILL.md 解析版本号
请检查 build/SKILL.md 是否包含 metadata.version 字段
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
""")
print("错误: 无法从 build/SKILL.md 解析版本号")
print("请检查 build/SKILL.md 是否包含 metadata.version 字段")
sys.exit(1)
@@ -149,21 +134,14 @@ def clone_repo(temp_dir: str) -> str:
SystemExit: clone 失败时退出
"""
repo_dir = os.path.join(temp_dir, "skills-repo")
print(f"Clone 仓库: {TARGET_REPO_URL}")
print(f" 到: {repo_dir}")
try:
run_git_command(temp_dir, ["clone", "--depth", "1", TARGET_REPO_URL, "skills-repo"])
except subprocess.CalledProcessError as e:
print(f"""
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
错误: Clone 仓库失败
返回码: {e.returncode}
标准输出: {e.stdout}
错误输出: {e.stderr}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
""")
print(f"错误: Clone 仓库失败")
print(f" 返回码: {e.returncode}")
print(f" 标准输出: {e.stdout}")
print(f" 错误输出: {e.stderr}")
sys.exit(1)
return repo_dir
@@ -182,7 +160,6 @@ def clear_target_dir(repo_dir: str) -> str:
target_dir = os.path.join(repo_dir, TARGET_PATH)
if os.path.exists(target_dir):
print(f"清空目标目录: {target_dir}")
shutil.rmtree(target_dir)
os.makedirs(target_dir, exist_ok=True)
@@ -197,18 +174,14 @@ def copy_build_contents(build_dir: str, target_dir: str) -> None:
build_dir: build 源目录
target_dir: 目标目录
"""
print(f"复制 build/ 内容 -> {target_dir}")
for item in os.listdir(build_dir):
src = os.path.join(build_dir, item)
dst = os.path.join(target_dir, item)
if os.path.isdir(src):
shutil.copytree(src, dst)
print(f" 目录: {item}")
else:
shutil.copy2(src, dst)
print(f" 文件: {item}")
def git_commit_and_push(repo_dir: str, version: str) -> None:
@@ -224,23 +197,15 @@ def git_commit_and_push(repo_dir: str, version: str) -> None:
"""
commit_message = f"publish: lyxy-document-reader {version}"
print(f"Git 提交: {commit_message}")
try:
run_git_command(repo_dir, ["add", "."])
run_git_command(repo_dir, ["commit", "-m", commit_message])
print(" 推送中...")
run_git_command(repo_dir, ["push"])
except subprocess.CalledProcessError as e:
print(f"""
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
错误: Git 操作失败
返回码: {e.returncode}
标准输出: {e.stdout}
错误输出: {e.stderr}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
""")
print(f"错误: Git 操作失败")
print(f" 返回码: {e.returncode}")
print(f" 标准输出: {e.stdout}")
print(f" 错误输出: {e.stderr}")
sys.exit(1)
@@ -248,10 +213,6 @@ def main() -> None:
"""
主函数:执行完整的发布流程
"""
print("=" * 60)
print("Skill 发布")
print("=" * 60)
# 路径配置
project_root = os.path.dirname(os.path.abspath(__file__))
build_dir = os.path.join(project_root, "build")
@@ -263,37 +224,20 @@ def main() -> None:
# 解析版本号
version = parse_version_from_skill_md(build_skill_md_path)
print(f"版本号: {version}")
print()
# 使用临时目录
with tempfile.TemporaryDirectory(prefix="lyxy-publish-") as temp_dir:
print(f"临时目录: {temp_dir}")
print()
# Clone 仓库
repo_dir = clone_repo(temp_dir)
print()
# 清空目标路径
target_dir = clear_target_dir(repo_dir)
print()
# 复制内容
copy_build_contents(build_dir, target_dir)
print()
# Git 提交并推送
git_commit_and_push(repo_dir, version)
print()
# 完成信息
print("=" * 60)
print("发布完成!")
print(f"版本号: {version}")
print(f"目标仓库: {TARGET_REPO_URL}")
print(f"目标路径: {TARGET_PATH}")
print("=" * 60)
if __name__ == "__main__":

View File

@@ -10,21 +10,9 @@ set -e
cd "$(dirname "$0")"
echo "============================================"
echo "Skill 混淆构建 + 发布"
echo "============================================"
echo
# 1. 混淆构建
echo "[1/2] 执行混淆构建..."
echo ">>> 构建 + 发布"
echo "[1/2] 构建..."
uv run --with pyarmor python build.py
echo
# 2. 发布
echo "[2/2] 执行发布..."
echo "[2/2] 发布..."
uv run python publish.py
echo
echo "============================================"
echo "完成!"
echo "============================================"
echo ">>> 完成"

View File

@@ -92,7 +92,8 @@ def generate_uv_command(
dependencies: list,
input_path: str,
python_version: Optional[str] = None,
script_path: str = "scripts/lyxy_document_reader.py"
script_path: str = "scripts/lyxy_document_reader.py",
include_pyarmor: bool = True
) -> str:
"""
生成 uv run 命令。
@@ -102,6 +103,7 @@ def generate_uv_command(
input_path: 输入文件路径或 URL
python_version: 需要的 python 版本None 表示不指定
script_path: 脚本路径
include_pyarmor: 是否包含 pyarmor 依赖
Returns:
uv run 命令字符串
@@ -111,7 +113,7 @@ def generate_uv_command(
if python_version:
parts.append(f"--python {python_version}")
# 始终添加 pyarmor 依赖(混淆后脚本需要)
if include_pyarmor:
parts.append("--with pyarmor")
for dep in dependencies:
@@ -126,10 +128,45 @@ def generate_uv_command(
return " ".join(parts)
def generate_uv_args(
dependencies: list,
script_path: str,
python_version: Optional[str] = None,
include_pyarmor: bool = True
) -> list:
"""
生成 uv run 命令参数列表(用于 subprocess.run
Args:
dependencies: 依赖包列表
script_path: 脚本路径
python_version: 需要的 python 版本None 表示不指定
include_pyarmor: 是否包含 pyarmor 依赖
Returns:
uv run 命令参数列表
"""
args = ["uv", "run"]
if python_version:
args.extend(["--python", python_version])
if include_pyarmor:
args.extend(["--with", "pyarmor"])
for dep in dependencies:
args.extend(["--with", dep])
args.append(script_path)
return args
def generate_python_command(
dependencies: list,
input_path: str,
script_path: str = "scripts/lyxy_document_reader.py"
script_path: str = "scripts/lyxy_document_reader.py",
include_pyarmor: bool = True
) -> Tuple[str, str]:
"""
生成 python 命令和 pip 安装命令。
@@ -138,14 +175,17 @@ def generate_python_command(
dependencies: 依赖包列表
input_path: 输入文件路径或 URL
script_path: 脚本路径
include_pyarmor: 是否包含 pyarmor 依赖
Returns:
(python_command, pip_command) 元组
"""
python_cmd = f"python {script_path} {input_path}"
# 构建 pip install 命令,处理带引号的依赖,始终包含 pyarmor
pip_parts = ["pip install", "pyarmor"]
# 构建 pip install 命令,处理带引号的依赖
pip_parts = ["pip install"]
if include_pyarmor:
pip_parts.append("pyarmor")
for dep in dependencies:
pip_parts.append(dep)
pip_cmd = " ".join(pip_parts)

View File

@@ -8,8 +8,13 @@ import subprocess
import sys
from pathlib import Path
# 确定项目根目录和脚本路径
script_file = Path(__file__).resolve()
scripts_dir = script_file.parent
project_root = scripts_dir.parent
bootstrap_path = str(scripts_dir / "bootstrap.py")
# 将 scripts/ 目录添加到 sys.path
scripts_dir = Path(__file__).resolve().parent
if str(scripts_dir) not in sys.path:
sys.path.append(str(scripts_dir))
@@ -75,6 +80,7 @@ def main():
detect_file_type_light,
get_platform,
get_dependencies,
generate_uv_args,
)
from readers import READERS
@@ -93,29 +99,22 @@ def main():
python_version, dependencies = get_dependencies(reader_cls, platform_id)
# 生成 uv 命令参数列表
uv_args = ["uv", "run"]
if python_version:
uv_args.extend(["--python", python_version])
# 始终添加 pyarmor 依赖(混淆后脚本需要)
uv_args.extend(["--with", "pyarmor"])
for dep in dependencies:
uv_args.extend(["--with", dep])
# 目标脚本是 bootstrap.py
uv_args.append("scripts/bootstrap.py")
uv_args = generate_uv_args(
dependencies=dependencies,
script_path=bootstrap_path,
python_version=python_version,
include_pyarmor=True
)
# 添加所有命令行参数
uv_args.extend(sys.argv[1:])
# 设置环境变量
env = os.environ.copy()
env["PYTHONPATH"] = "."
env["PYTHONPATH"] = str(project_root)
# 自启动:使用 subprocess 替代 execvpeWindows 兼容)
result = subprocess.run(uv_args, env=env)
result = subprocess.run(uv_args, env=env, cwd=str(project_root))
sys.exit(result.returncode)

View File

@@ -0,0 +1,53 @@
"""测试路径解析功能 - 验证从任意路径调用脚本。"""
import sys
from pathlib import Path
class TestPathResolution:
"""测试路径解析逻辑。"""
def test_project_root_detection(self):
"""测试项目根目录检测逻辑。"""
# 模拟 lyxy_document_reader.py 中的路径计算逻辑
# 获取当前测试文件的路径,然后向上找到项目根
test_file = Path(__file__).resolve()
tests_dir = test_file.parent.parent # tests/
project_root = tests_dir.parent # 项目根
# 验证我们能正确找到项目根
assert (project_root / "scripts").exists()
assert (project_root / "scripts" / "lyxy_document_reader.py").exists()
assert (project_root / "scripts" / "bootstrap.py").exists()
def test_bootstrap_path_absolute(self):
"""测试 bootstrap.py 路径是绝对路径。"""
# 模拟 lyxy_document_reader.py 中的路径计算
test_file = Path(__file__).resolve()
project_root = test_file.parent.parent.parent # 从 tests/test_cli/ 向上两级
scripts_dir = project_root / "scripts"
bootstrap_path = scripts_dir / "bootstrap.py"
# 验证路径是绝对路径
assert bootstrap_path.is_absolute()
assert bootstrap_path.exists()
def test_path_independent_from_cwd(self, monkeypatch, tmp_path):
"""测试路径计算不依赖当前工作目录。"""
# 保存原始路径
test_file = Path(__file__).resolve()
project_root = test_file.parent.parent.parent
scripts_dir = project_root / "scripts"
# 切换到临时目录
monkeypatch.chdir(tmp_path)
# 即使在临时目录,我们仍然能通过 __file__ 找到正确的路径
# 这里我们模拟 lyxy_document_reader.py 中的逻辑
# 注意:实际中 __file__ 是脚本本身的路径,不是测试文件的路径
# 这里我们验证原理__file__ 给出的是脚本位置,与 cwd 无关
# 验证 scripts_dir 和 bootstrap_path 的计算只依赖 __file__
# 这个测试验证的是概念,不是实际的脚本导入
assert scripts_dir.is_absolute()
assert (scripts_dir / "bootstrap.py").exists()

View File

@@ -4,32 +4,6 @@ import pytest
from pathlib import Path
@pytest.fixture
def temp_html(tmp_path):
"""创建临时 HTML 文件的 fixture 工厂。
Args:
content: HTML 内容字符串
encoding: 文件编码,默认 'utf-8'
Returns:
str: 临时文件路径
"""
def _create_html(content="<html><body><p>Test</p></body></html>", encoding='utf-8'):
file_path = tmp_path / "test.html"
# 如果内容不包含完整的 HTML 结构,添加基本结构
if not content.strip().startswith('<html'):
content = f"<html><head><meta charset='{encoding}'></head><body>{content}</body></html>"
with open(file_path, 'w', encoding=encoding) as f:
f.write(content)
return str(file_path)
return _create_html
# 静态测试文件目录
FIXTURES_DIR = Path(__file__).parent / "fixtures"