diff --git a/compile_machine_code.py b/compile_machine_code.py new file mode 100644 index 0000000..cce551d --- /dev/null +++ b/compile_machine_code.py @@ -0,0 +1,294 @@ +#!/usr/bin/env python3 +""" +跨平台 Python 模块编译脚本 + +将核心 Python 模块编译为机器码(.pyd 或 .so)以提升性能。 + +支持的平台: +- Windows: 生成 .pyd 文件 +- Linux: 生成 .so 文件 + +使用方法: + python compile_machine_code.py [options] + +选项: + --compile, -c 编译指定的模块(默认) + --list, -l 列出已编译的模块 + --clean, -k 清理编译生成的文件 + --help, -h 显示帮助信息 + +注意: + 1. 需要安装 C 编译器 (Windows 上需要 Visual Studio Build Tools, Linux 上需要 GCC) + 2. 需要安装 mypyc: pip install mypyc + 3. 编译后的文件是平台相关的,不能跨平台复制 + 4. 建议在部署的目标环境上运行此脚本 +""" +import os +import sys +import glob +import subprocess +import shutil +import argparse + +# 检测当前平台 +PLATFORM = sys.platform +if PLATFORM.startswith('win'): + EXTENSION = '.pyd' + BUILD_PREFIX = 'cp314-win_amd64' + BUILD_PATH = os.path.join('build', f'lib.win-amd64-cpython-314') +elif PLATFORM.startswith('linux'): + EXTENSION = '.so' + BUILD_PREFIX = 'cp314-x86_64-linux-gnu' + BUILD_PATH = os.path.join('build', f'lib.linux-x86_64-cpython-314') +else: + print(f"不支持的平台: {PLATFORM}") + sys.exit(1) + +# 要编译的模块列表 +# 注意:Mypyc 对动态特性支持有限,只选择计算密集或类型明确的模块 +MODULES = [ + # 工具模块 + 'core/utils/json_utils.py', # JSON 处理 + 'core/utils/executor.py', # 代码执行引擎 + 'core/utils/singleton.py', # 单例模式基类 + 'core/utils/exceptions.py', # 自定义异常 + 'core/utils/logger.py', # 日志模块 + + # 核心管理模块 + 'core/managers/command_manager.py', # 指令匹配和分发 + 'core/managers/admin_manager.py', # 管理员管理 + 'core/managers/permission_manager.py', # 权限管理 + 'core/managers/plugin_manager.py', # 插件管理器 + 'core/managers/redis_manager.py', # Redis 管理器 + 'core/managers/image_manager.py', # 图片管理器 + + # 核心基础模块 + 'core/ws.py', # WebSocket 核心 + 'core/bot.py', # Bot 核心抽象 + 'core/config_loader.py', # 配置加载 + 'core/config_models.py', # 配置模型 + 'core/permission.py', # 权限枚举 + + # API 模块 - 注意:这些类会被 Bot 类多继承使用 + # 因此不适合编译,否则会导致 "multiple bases have instance lay-out conflict" 错误 + # 'core/api/base.py', # API 基础类 + # 'core/api/account.py', # 账号相关 API + # 'core/api/friend.py', # 好友相关 API + # 'core/api/group.py', # 群组相关 API + # 'core/api/media.py', # 媒体相关 API + # 'core/api/message.py', # 消息相关 API + + # 数据模型(适合编译的高频使用数据类) + 'models/message.py', # 消息段模型 + 'models/sender.py', # 发送者模型 + 'models/objects.py', # API 响应数据模型 + + # 事件处理相关 + 'core/handlers/event_handler.py', # 事件处理器 + + # 注意:以下文件不适合编译 + # - 主程序文件(main.py) + # - 测试文件(tests/目录) + # - 插件文件(plugins/目录) + # - 编译脚本(compile_machine_code.py等) + # - 临时文件(scratch_files/目录) + # - 抽象基类(models/events/base.py) + # - 事件工厂(models/events/factory.py) + # - 包含复杂动态特性的文件 +] + +def list_compiled_modules(): + """列出已编译的模块""" + print(f"\n已编译的 {PLATFORM} 模块:") + print("=" * 50) + + # 查找所有编译后的文件 + compiled_files = [] + for ext in [EXTENSION, f'__mypyc{EXTENSION}']: + compiled_files.extend(glob.glob(f'**/*{ext}', recursive=True)) + + # 过滤掉虚拟环境中的文件 + compiled_files = [f for f in compiled_files if 'venv' not in f] + + if compiled_files: + for f in sorted(compiled_files): + size = os.path.getsize(f) // 1024 # KB + print(f"{f} ({size} KB)") + else: + print(f"未找到已编译的 {EXTENSION} 文件") + + print(f"\n总计: {len(compiled_files)} 个文件") + +def clean_compiled_files(): + """清理编译生成的文件""" + print(f"\n清理编译生成的 {EXTENSION} 文件...") + + # 查找所有编译后的文件 + compiled_files = [] + for ext in [EXTENSION, f'__mypyc{EXTENSION}']: + compiled_files.extend(glob.glob(f'**/*{ext}', recursive=True)) + + # 过滤掉虚拟环境中的文件 + compiled_files = [f for f in compiled_files if 'venv' not in f] + + if compiled_files: + for f in sorted(compiled_files): + try: + os.remove(f) + print(f"已删除: {f}") + except Exception as e: + print(f"删除失败 {f}: {e}") + + # 清理 build 目录 + if os.path.exists('build'): + try: + shutil.rmtree('build') + print("已删除 build 目录") + except Exception as e: + print(f"删除 build 目录失败: {e}") + else: + print(f"没有可清理的 {EXTENSION} 文件") + +def get_platform_specific_module_name(module_path): + """获取平台特定的模块文件名""" + module_name = module_path.replace('.py', '') + return f"{module_name}.{BUILD_PREFIX}{EXTENSION}" + +def compile_module(module_path): + """编译单个模块""" + print(f"\n编译: {module_path}") + + try: + # 直接调用 mypyc 命令行工具 + result = subprocess.run( + [sys.executable, '-m', 'mypyc', module_path], + capture_output=True, + text=True, + check=True + ) + + # 获取平台特定的模块名 + platform_module = get_platform_specific_module_name(module_path) + mypyc_platform_module = platform_module.replace(EXTENSION, f'__mypyc{EXTENSION}') + + # 检查编译产物是否在当前目录 + if os.path.exists(platform_module): + print(f" ✓ 编译成功: {platform_module}") + return True + else: + # 检查 build 目录中是否有编译产物 + build_module_path = os.path.join(BUILD_PATH, platform_module) + build_mypyc_path = os.path.join(BUILD_PATH, mypyc_platform_module) + + if os.path.exists(build_module_path): + # 如果在 build 目录中,复制到正确位置 + os.makedirs(os.path.dirname(platform_module), exist_ok=True) + shutil.copy2(build_module_path, platform_module) + shutil.copy2(build_mypyc_path, mypyc_platform_module) + print(f" ✓ 编译成功(已从 build 目录复制): {platform_module}") + return True + else: + print(f" ✗ 编译失败:找不到编译产物") + if result.stdout: + print(f" 编译输出:{result.stdout[:500]}...") + if result.stderr: + print(f" 错误信息:{result.stderr[:500]}...") + return False + + except subprocess.CalledProcessError as e: + print(f" ✗ 编译失败,退出码: {e.returncode}") + if e.stdout: + print(f" 编译输出:{e.stdout[:500]}...") + if e.stderr: + print(f" 错误信息:{e.stderr[:500]}...") + return False + except Exception as e: + print(f" ✗ 编译失败,意外错误: {e}") + return False + +def should_skip_module(module_path): + """检查模块是否应该被跳过编译""" + try: + with open(module_path, 'r', encoding='utf-8') as f: + content = f.read() + + # 检查是否包含抽象基类相关代码 + if 'from abc import ABC' in content or 'from abc import abstractmethod' in content: + return True, "包含抽象基类,不适合编译" + + # 检查是否包含动态特性 + if 'eval(' in content or 'exec(' in content or 'getattr(' in content or 'setattr(' in content: + return True, "包含动态特性,不适合编译" + + return False, "" + except Exception as e: + return True, f"读取文件时出错: {e}" + +def compile_all_modules(): + """编译所有指定的模块""" + print(f"\n开始编译 {len(MODULES)} 个模块 (平台: {PLATFORM})") + print("=" * 60) + + # 验证模块文件是否存在并检查是否适合编译 + valid_modules = [] + for module_path in MODULES: + if os.path.exists(module_path): + should_skip, reason = should_skip_module(module_path) + if should_skip: + print(f"跳过: {module_path} ({reason})") + else: + valid_modules.append(module_path) + else: + print(f"警告: 模块 {module_path} 不存在,将被跳过") + + if not valid_modules: + print("错误: 没有有效的模块可编译") + return False + + # 编译模块 + success_count = 0 + for module_path in valid_modules: + if compile_module(module_path): + success_count += 1 + + print(f"\n" + "=" * 60) + print(f"编译完成: {success_count}/{len(valid_modules)} 个模块成功") + + if success_count == len(valid_modules): + print("✓ 所有模块编译成功") + return True + else: + print("✗ 部分模块编译失败") + return False + +def main(): + """主函数""" + parser = argparse.ArgumentParser(description='跨平台 Python 模块编译脚本') + + group = parser.add_mutually_exclusive_group() + group.add_argument('--compile', '-c', action='store_true', default=True, + help='编译指定的模块 (默认)') + group.add_argument('--list', '-l', action='store_true', + help='列出已编译的模块') + group.add_argument('--clean', '-k', action='store_true', + help='清理编译生成的文件') + + args = parser.parse_args() + + # 检查是否安装了 mypyc + try: + import mypyc + except ImportError: + print("错误: 未安装 mypyc,请先安装: pip install mypyc") + sys.exit(1) + + if args.list: + list_compiled_modules() + elif args.clean: + clean_compiled_files() + else: + compile_all_modules() + print("\n使用 --list 选项查看已编译的模块") + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/compile_modules.py b/compile_modules.py new file mode 100644 index 0000000..c8cfc07 --- /dev/null +++ b/compile_modules.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +""" +编译模块脚本 + +这个脚本会单独编译每个Python模块,确保每个模块都在正确位置生成独立的.pyd文件。 +""" +import os +import sys +import glob +from mypyc.build import mypycify +from distutils.core import setup + +def compile_module(module_path): + """ + 编译单个模块 + + Args: + module_path: 要编译的Python模块路径 + """ + print(f"\nCompiling {module_path}...") + try: + ext_modules = mypycify([module_path]) + setup(name=f'compiled_{os.path.basename(module_path).replace(".py", "")}', + ext_modules=ext_modules) + return True + except Exception as e: + print(f"Error compiling {module_path}: {e}") + return False + +def main(): + """ + 主函数 + """ + # 要编译的模块列表 + modules = [ + 'core/utils/json_utils.py', # JSON 处理 + 'core/utils/executor.py', # 代码执行引擎 + 'core/managers/command_manager.py', # 指令匹配和分发 + 'core/managers/admin_manager.py', # 管理员管理 + 'core/managers/permission_manager.py', # 权限管理 + 'core/ws.py', # WebSocket 核心 + 'core/managers/plugin_manager.py', # 插件管理器 + 'core/bot.py', # Bot 核心抽象 + 'core/config_loader.py', # 配置加载 + ] + + # 自动添加 events 模型 + event_models = glob.glob('models/events/*.py') + event_models = [m for m in event_models if not m.endswith('__init__.py')] + modules.extend(event_models) + + print(f"Found {len(modules)} modules to compile.") + + success_count = 0 + for module in modules: + if compile_module(module): + success_count += 1 + + print(f"\n--- Compilation Summary ---") + print(f"Total modules: {len(modules)}") + print(f"Successfully compiled: {success_count}") + print(f"Failed: {len(modules) - success_count}") + +if __name__ == '__main__': + main() diff --git a/export_requirements.py b/export_requirements.py new file mode 100644 index 0000000..a3bb109 --- /dev/null +++ b/export_requirements.py @@ -0,0 +1,8 @@ +import subprocess + +# 运行pip freeze命令获取所有依赖 +result = subprocess.run(['pip', 'freeze'], capture_output=True, text=True) + +# 将输出写入requirements.txt文件 +with open('requirements.txt', 'w', encoding='utf-8') as f: + f.write(result.stdout) \ No newline at end of file diff --git a/models/events/message.py b/models/events/message.py index 0ddfe90..421c843 100644 --- a/models/events/message.py +++ b/models/events/message.py @@ -27,7 +27,7 @@ class Anonymous: """匿名用户 flag""" -@dataclass +@dataclass(slots=True) class MessageEvent(OneBotEvent): """ 消息事件基类 @@ -80,7 +80,7 @@ class MessageEvent(OneBotEvent): raise NotImplementedError("reply method must be implemented by subclasses") -@dataclass +@dataclass(slots=True) class PrivateMessageEvent(MessageEvent): """ 私聊消息事件 @@ -98,7 +98,7 @@ class PrivateMessageEvent(MessageEvent): ) -@dataclass +@dataclass(slots=True) class GroupMessageEvent(MessageEvent): """ 群聊消息事件 diff --git a/models/events/meta.py b/models/events/meta.py index c550bee..e3593ce 100644 --- a/models/events/meta.py +++ b/models/events/meta.py @@ -26,7 +26,7 @@ class LifeCycleSubType: CONNECT: Final[str] = 'connect' # 连接 -@dataclass +@dataclass(slots=True) class MetaEvent(OneBotEvent): """ 元事件基类 @@ -40,7 +40,7 @@ class MetaEvent(OneBotEvent): return EventType.META -@dataclass +@dataclass(slots=True) class HeartbeatEvent(MetaEvent): """ 心跳事件,用于确认连接状态 @@ -55,7 +55,7 @@ class HeartbeatEvent(MetaEvent): """心跳间隔时间(ms)""" -@dataclass +@dataclass(slots=True) class LifeCycleEvent(MetaEvent): """ 生命周期事件,用于通知框架生命周期变化 diff --git a/models/events/notice.py b/models/events/notice.py index 8dcf56c..9376b2d 100644 --- a/models/events/notice.py +++ b/models/events/notice.py @@ -21,7 +21,7 @@ class NoticeEvent(OneBotEvent): return EventType.NOTICE -@dataclass +@dataclass(slots=True) class FriendAddNoticeEvent(NoticeEvent): """ 好友添加通知 @@ -30,7 +30,7 @@ class FriendAddNoticeEvent(NoticeEvent): """新好友 QQ 号""" -@dataclass +@dataclass(slots=True) class FriendRecallNoticeEvent(NoticeEvent): """ 好友消息撤回通知 @@ -42,7 +42,7 @@ class FriendRecallNoticeEvent(NoticeEvent): """被撤回的消息 ID""" -@dataclass +@dataclass(slots=True) class GroupNoticeEvent(NoticeEvent): """ 群组通知事件基类 @@ -54,7 +54,7 @@ class GroupNoticeEvent(NoticeEvent): """用户 QQ 号""" -@dataclass +@dataclass(slots=True) class GroupRecallNoticeEvent(GroupNoticeEvent): """ 群消息撤回通知 @@ -66,7 +66,7 @@ class GroupRecallNoticeEvent(GroupNoticeEvent): """被撤回的消息 ID""" -@dataclass +@dataclass(slots=True) class GroupIncreaseNoticeEvent(GroupNoticeEvent): """ 群成员增加通知 @@ -82,7 +82,7 @@ class GroupIncreaseNoticeEvent(GroupNoticeEvent): """ -@dataclass +@dataclass(slots=True) class GroupDecreaseNoticeEvent(GroupNoticeEvent): """ 群成员减少通知 @@ -100,7 +100,7 @@ class GroupDecreaseNoticeEvent(GroupNoticeEvent): """ -@dataclass +@dataclass(slots=True) class GroupAdminNoticeEvent(GroupNoticeEvent): """ 群管理员变动通知 @@ -113,7 +113,7 @@ class GroupAdminNoticeEvent(GroupNoticeEvent): """ -@dataclass +@dataclass(slots=True) class GroupBanNoticeEvent(GroupNoticeEvent): """ 群禁言通知 @@ -132,7 +132,7 @@ class GroupBanNoticeEvent(GroupNoticeEvent): """ -@dataclass +@dataclass(slots=True) class GroupUploadFile: """ 群文件信息 @@ -150,7 +150,7 @@ class GroupUploadFile: """文件总线 ID""" -@dataclass +@dataclass(slots=True) class GroupUploadNoticeEvent(GroupNoticeEvent): """ 群文件上传通知 @@ -159,7 +159,7 @@ class GroupUploadNoticeEvent(GroupNoticeEvent): """文件信息""" -@dataclass +@dataclass(slots=True) class NotifyNoticeEvent(NoticeEvent): """ 系统通知事件基类 (notify) @@ -175,7 +175,7 @@ class NotifyNoticeEvent(NoticeEvent): """发送者 QQ 号""" -@dataclass +@dataclass(slots=True) class PokeNotifyEvent(NotifyNoticeEvent): """ 戳一戳通知 @@ -187,7 +187,7 @@ class PokeNotifyEvent(NotifyNoticeEvent): """群号 (如果是群内戳一戳)""" -@dataclass +@dataclass(slots=True) class LuckyKingNotifyEvent(NotifyNoticeEvent): """ 群红包运气王通知 @@ -199,7 +199,7 @@ class LuckyKingNotifyEvent(NotifyNoticeEvent): """运气王 QQ 号""" -@dataclass +@dataclass(slots=True) class HonorNotifyEvent(NotifyNoticeEvent): """ 群荣誉变更通知 @@ -216,7 +216,7 @@ class HonorNotifyEvent(NotifyNoticeEvent): """ -@dataclass +@dataclass(slots=True) class GroupCardNoticeEvent(GroupNoticeEvent): """ 群成员名片更新通知 @@ -228,7 +228,7 @@ class GroupCardNoticeEvent(GroupNoticeEvent): """旧名片""" -@dataclass +@dataclass(slots=True) class OfflineFile: """ 离线文件信息 @@ -243,7 +243,7 @@ class OfflineFile: """下载链接""" -@dataclass +@dataclass(slots=True) class OfflineFileNoticeEvent(NoticeEvent): """ 接收离线文件通知 @@ -255,7 +255,7 @@ class OfflineFileNoticeEvent(NoticeEvent): """文件数据""" -@dataclass +@dataclass(slots=True) class ClientStatus: """ 客户端状态 @@ -267,7 +267,7 @@ class ClientStatus: """状态描述""" -@dataclass +@dataclass(slots=True) class ClientStatusNoticeEvent(NoticeEvent): """ 其他客户端在线状态变更通知 @@ -276,7 +276,7 @@ class ClientStatusNoticeEvent(NoticeEvent): """客户端信息""" -@dataclass +@dataclass(slots=True) class EssenceNoticeEvent(GroupNoticeEvent): """ 精华消息变动通知 diff --git a/models/events/request.py b/models/events/request.py index 87930b6..41ea580 100644 --- a/models/events/request.py +++ b/models/events/request.py @@ -21,7 +21,7 @@ class RequestEvent(OneBotEvent): return EventType.REQUEST -@dataclass +@dataclass(slots=True) class FriendRequestEvent(RequestEvent): """ 加好友请求事件 @@ -36,7 +36,7 @@ class FriendRequestEvent(RequestEvent): """请求 flag,在调用处理请求的 API 时需要传入此 flag""" -@dataclass +@dataclass(slots=True) class GroupRequestEvent(RequestEvent): """ 加群请求/邀请事件