自动Cython,使用Cython批量编译.py文件为.pyd文件!
Project description
AutoCython
自动将 Python 源码编译为 Cython 二进制扩展(
.so/.pyd),支持七重 AST 混淆、并发编译、跨平台运行。
概述
AutoCython 是一个 Python 源码保护工具,通过 Cython 编译 + AST 混淆双重手段,将 .py 文件转换为不可逆的二进制扩展模块。适用于商业项目源码保护、SDK 分发、知识产权保护等场景。
特性
- 一键编译 — 单文件 (
-f) 或整目录 (-p) 批量编译为.so/.pyd - 七重 AST 混淆 — docstring 移除、注解清除、局部变量重命名、字符串 XOR 加密、常量折叠、控制流平坦化、虚假分支注入
- 并发编译 — 基于
ThreadPoolExecutor的多线程并行编译,可配置并发数 - 实时进度面板 — 基于 Rich 的实时任务状态表格、进度条、耗时统计
- 跨平台 — 支持 Linux、macOS、Windows,自动适配
.so/.pyd扩展名 - 包路径保真 — 编译包内模块时保留 qualified module name,兼容 Pydantic / FastAPI 等依赖运行时模块命名空间的框架
- 可复现构建 — 通过
--seed参数固定混淆随机种子 - 智能排除 — 自动跳过
__init__.py、虚拟环境、构建目录;支持# AutoCython No Compile标记豁免 - 中英双语 — CLI 帮助信息和进度面板自动适配系统语言
安装
pip install AutoCython-zhang
当前编译链要求
Cython>=3,<4。该约束用于避免旧版 Cython 在编译后破坏Pydantic/FastAPI/dataclass等依赖运行时注解的框架行为。
依赖
| 包 | 用途 |
|---|---|
cython>=3,<4 |
Python → C 编译核心;确保运行时注解不被旧版编译链破坏 |
setuptools |
构建扩展模块 |
rich |
终端实时进度面板 |
使用方法
编译单文件
AutoCython -f demo.py
编译整个目录
AutoCython -p ./my_project
常用选项
AutoCython -p ./src -c 4 # 4 线程并发编译
AutoCython -f main.py -d # 编译后删除源文件
AutoCython -p ./src --seed 42 # 固定混淆种子(可复现)
AutoCython -v # 查看版本
AutoCython -h # 查看帮助
排除文件
在文件头两行内添加注释即可跳过编译:
# AutoCython No Compile
作为库调用
from AutoCython.compile import compile_to_binary
# 编译单文件
output = compile_to_binary("demo.py", del_source=False, obfuscate=True, obfuscate_seed=42)
print(f"生成: {output}")
API 概览
核心函数
| 函数 | 模块 | 描述 |
|---|---|---|
compile() |
AutoCython.AutoCython |
主入口:解析参数并调度编译任务 |
compile_to_binary() |
AutoCython.compile |
将单个 .py 文件编译为二进制扩展 |
obfuscate_source() |
AutoCython.obfuscate |
对源码执行七重 AST 混淆变换 |
run_tasks() |
AutoCython.run_tasks |
并发任务执行引擎(含实时进度面板) |
find_python_files() |
AutoCython.tools |
递归扫描目录下的可编译 .py 文件 |
parse_arguments() |
AutoCython.tools |
CLI 参数解析(中英双语) |
get_platform_extension() |
AutoCython.compile |
返回当前平台的二进制扩展名 |
get_system_language() |
AutoCython.tools |
检测系统语言(zh/en) |
目录结构
AutoCython/
├── AutoCython/
│ ├── __init__.py # 包入口,导出 compile() 和 main()
│ ├── _version.py # 单一版本号来源
│ ├── AutoCython.py # 主编译调度逻辑
│ ├── compile.py # Cython 编译核心(临时目录隔离)
│ ├── obfuscate.py # 七重 AST 混淆引擎
│ ├── run_tasks.py # 并发任务执行 + Rich 实时面板
│ └── tools.py # CLI 参数解析、文件扫描、国际化
├── tests/
│ ├── test_autocython.py # 主入口集成测试
│ ├── test_compile.py # 编译功能单元测试
│ ├── test_obfuscate.py # 混淆引擎单元测试
│ ├── test_obfuscate_advanced.py # 混淆高级场景测试
│ ├── test_obfuscate_anti.py # 混淆反模式/边界测试
│ ├── test_compile_anti.py # 编译反模式测试
│ ├── test_run_tasks.py # 任务执行器测试
│ ├── test_tools.py # 工具函数测试
│ └── kd_dist/ # 知识蒸馏测试集(编译矩阵 + 行为等价)
├── pyproject.toml # 项目配置与构建定义
├── requirements.txt # 依赖清单
├── LICENSE # MIT 许可证
└── DESIGN.md # 设计文档
相关文档
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autocython_zhang-2.3.6.tar.gz.
File metadata
- Download URL: autocython_zhang-2.3.6.tar.gz
- Upload date:
- Size: 18.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebf08d10374545e16879ca4218b0f5d28f82a60eb62127e7c86d97856b3673df
|
|
| MD5 |
201fce131ff12576b89a66747fecd2ce
|
|
| BLAKE2b-256 |
94cc0ff2192ddf45c8a7c93b4c4c17e65957d05d8a8d6ce6541573fb50d91881
|
File details
Details for the file autocython_zhang-2.3.6-py3-none-any.whl.
File metadata
- Download URL: autocython_zhang-2.3.6-py3-none-any.whl
- Upload date:
- Size: 20.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44fb134d00d97e3ab0a2944f3fa8b823060f697a10005595b00f42631e9c01fd
|
|
| MD5 |
a5b509dd6f5202b24ec72243acc71dc7
|
|
| BLAKE2b-256 |
776a156803553dc0e4b5c72ac70fe78641e5933c142c8367667c6ef2651225af
|