Add PyArmor obfuscation packaging flow
This commit is contained in:
1 parent
5166678590
commit
11de0f75c7
4 files changed
+292
-5
No files matched your search
@@ -0,0 +1,121 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _write(path: Path, text: str = "x = 1\n") -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text, encoding="utf-8")
|
||||
|
||||
|
||||
def _make_minimal_project(root: Path) -> None:
|
||||
_write(root / "douyin" / "__init__.py")
|
||||
_write(root / "douyin" / "core" / "version.py")
|
||||
_write(root / "gui" / "gui_app.py")
|
||||
_write(root / "gui" / "web_gui.py")
|
||||
_write(root / "gui" / "components" / "left_config_area.py")
|
||||
_write(root / "operations" / "__init__.py")
|
||||
_write(root / "operations" / "ocr_operations.py")
|
||||
_write(root / "bin" / "run_cli.py")
|
||||
_write(root / "tools" / "license_dev.py", "PRIVATE_KEY = 'must-not-ship'\n")
|
||||
|
||||
|
||||
def test_create_pyarmor_staging_uses_only_runtime_sources(tmp_path, monkeypatch):
|
||||
from tools.obfuscation_staging import create_pyarmor_staging
|
||||
|
||||
monkeypatch.setattr("tools.obfuscation_staging.shutil.which", lambda name: "pyarmor.exe")
|
||||
root = tmp_path / "project"
|
||||
staging = tmp_path / "stage"
|
||||
_make_minimal_project(root)
|
||||
calls: list[list[str]] = []
|
||||
|
||||
def fake_runner(cmd, *, cwd, check):
|
||||
calls.append([str(x) for x in cmd])
|
||||
assert cwd == root
|
||||
assert check is True
|
||||
_write(staging / "douyin" / "__init__.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "gui_app.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "web_gui.py", "# obfuscated\n")
|
||||
_write(staging / "operations" / "__init__.py", "# obfuscated\n")
|
||||
_write(staging / "bin" / "run_cli.py", "# obfuscated\n")
|
||||
_write(staging / "pyarmor_runtime_000000" / "__init__.py", "# runtime\n")
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
|
||||
result = create_pyarmor_staging(root, staging, runner=fake_runner)
|
||||
|
||||
assert result.staging_root == staging
|
||||
assert result.runtime_package.startswith("pyarmor_runtime")
|
||||
command_text = " ".join(calls[0])
|
||||
assert "pyarmor" in command_text
|
||||
assert "douyin" in command_text
|
||||
assert "gui" in command_text
|
||||
assert "operations" in command_text
|
||||
assert "bin/run_cli.py" in command_text
|
||||
assert "tools" not in command_text
|
||||
assert "license_dev.py" not in command_text
|
||||
|
||||
|
||||
def test_validate_pyarmor_staging_requires_runtime_package(tmp_path):
|
||||
from tools.obfuscation_staging import validate_pyarmor_staging
|
||||
|
||||
staging = tmp_path / "stage"
|
||||
_write(staging / "douyin" / "__init__.py")
|
||||
_write(staging / "gui" / "gui_app.py")
|
||||
_write(staging / "gui" / "web_gui.py")
|
||||
_write(staging / "operations" / "__init__.py")
|
||||
_write(staging / "bin" / "run_cli.py")
|
||||
|
||||
with pytest.raises(RuntimeError, match="pyarmor_runtime"):
|
||||
validate_pyarmor_staging(staging)
|
||||
|
||||
|
||||
def test_validate_pyarmor_staging_rejects_sensitive_tools(tmp_path):
|
||||
from tools.obfuscation_staging import validate_pyarmor_staging
|
||||
|
||||
staging = tmp_path / "stage"
|
||||
_write(staging / "douyin" / "__init__.py")
|
||||
_write(staging / "gui" / "gui_app.py")
|
||||
_write(staging / "gui" / "web_gui.py")
|
||||
_write(staging / "operations" / "__init__.py")
|
||||
_write(staging / "bin" / "run_cli.py")
|
||||
_write(staging / "pyarmor_runtime_000000" / "__init__.py")
|
||||
_write(staging / "tools" / "license_dev.py", "PRIVATE_KEY = 'leak'\n")
|
||||
|
||||
with pytest.raises(RuntimeError, match="forbidden"):
|
||||
validate_pyarmor_staging(staging)
|
||||
|
||||
|
||||
def test_create_pyarmor_staging_moves_flat_entry_script_back_to_bin(tmp_path, monkeypatch):
|
||||
from tools.obfuscation_staging import create_pyarmor_staging
|
||||
|
||||
monkeypatch.setattr("tools.obfuscation_staging.shutil.which", lambda name: "pyarmor.exe")
|
||||
root = tmp_path / "project"
|
||||
staging = tmp_path / "stage"
|
||||
_make_minimal_project(root)
|
||||
|
||||
def fake_runner(cmd, *, cwd, check):
|
||||
_write(staging / "douyin" / "__init__.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "gui_app.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "web_gui.py", "# obfuscated\n")
|
||||
_write(staging / "operations" / "__init__.py", "# obfuscated\n")
|
||||
_write(staging / "run_cli.py", "# flat obfuscated entry\n")
|
||||
_write(staging / "pyarmor_runtime_000000" / "__init__.py", "# runtime\n")
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
|
||||
create_pyarmor_staging(root, staging, runner=fake_runner)
|
||||
|
||||
assert (staging / "bin" / "run_cli.py").read_text(encoding="utf-8") == "# flat obfuscated entry\n"
|
||||
assert not (staging / "run_cli.py").exists()
|
||||
|
||||
|
||||
def test_windows_packaging_script_exposes_obfuscate_switch():
|
||||
script = Path("packaging/pyinstaller/build_windows.ps1").read_text(encoding="utf-8")
|
||||
|
||||
assert "[switch]$Obfuscate" in script
|
||||
assert "tools.obfuscation_staging" in script
|
||||
assert "--paths\", $BuildSourceRoot" in script
|
||||
assert "Join-Path $BuildSourceRoot \"gui\\gui_app.py\"" in script
|
||||
assert "Join-Path $BuildSourceRoot \"bin\\run_cli.py\"" in script
|
||||
Reference in new issue
Block a user