modify: module_bank 适配

This commit is contained in:
2026-05-05 00:27:27 +08:00
parent 425d2f3ff9
commit a9b74cc9f3
4 changed files with 66 additions and 33 deletions
+8 -9
View File
@@ -1,13 +1,8 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
requires = ["hatchling"]
[project]
name = "chongming"
version = "0.1.0"
description = "基于 modules-bank 的 fastapi 打包脚手架"
readme = "README.md"
requires-python = ">=3.8"
dependencies = [
"aiofiles",
"aiosqlite",
@@ -16,7 +11,7 @@ dependencies = [
"diskcache",
"fastapi[uvicorn]",
"gunicorn",
"module-bank==0.3.1",
"module-bank",
"pyjwt",
"python-multipart",
"sqlite-vfs",
@@ -25,14 +20,18 @@ dependencies = [
"typing-extensions",
"uvicorn",
]
description = "基于 modules-bank 的 fastapi 打包脚手架"
name = "chongming"
readme = "README.md"
requires-python = ">=3.8"
version = "0.1.0"
[[tool.uv.index]]
url = "https://pypi.org/simple"
[[tool.uv.index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
[[tool.uv.index]]
url = "https://mirrors.ustc.edu.cn/pypi/web/simple/"
+37 -2
View File
@@ -12,6 +12,8 @@
python -m chongming.scripts.build [pyinstaller|nuitka]
"""
import json
import ast
import platform
import shutil
import subprocess
@@ -138,17 +140,50 @@ def _stage_pack_modules(config: Config) -> str:
return "⚠️ 未配置 module_system.path,跳过模块打包"
messages = []
for output_path, source_dir, _ in mbank_list:
for output_path, source_dir, package_name in mbank_list:
key = Encryption.generate_key()
packer = PythonToSQLite(
output_path,
key,
pyarmor_obfuscate=True,
)
packer.pack_directory(source_dir, "app" if "app" in source_dir else "plugins")
packer.pack_directory(source_dir, package_name)
packer.verify_package_structure()
packer.packer_key(key)
packer.delete_source_code(None)
# 修复 metadata 格式: 将 str(dict) 格式转换为 json 格式
# sqlite_module_importer.add_module() 使用 str(metadata) 存储,
# 生成 "{'pyarmor_runtime': 'pyarmor_runtime_000000'}" 这样的单引号格式,
# 但运行时 loader._extract_runtime_name_from_metadata() 使用 json.loads() 解析.
# 单引号不是合法的 JSON, 导致解析失败, 无法获取运行时模块名.
# 这里在删除源码后修复 metadata 格式, 使得 loader 能从 metadata 中提取运行时名.
# try:
# import sqlite3 as _sqlite3
# _conn = _sqlite3.connect(output_path)
# _conn.row_factory = _sqlite3.Row
# _rows = _conn.execute(
# "SELECT module_name, metadata FROM python_modules WHERE metadata IS NOT NULL"
# ).fetchall()
# for _row in _rows:
# _meta_str = _row["metadata"]
# try:
# json.loads(_meta_str) # 已经是合法 JSON, 无需处理
# except (json.JSONDecodeError, TypeError):
# try:
# _meta_dict = ast.literal_eval(_meta_str)
# _fixed = json.dumps(_meta_dict)
# _conn.execute(
# "UPDATE python_modules SET metadata = ? WHERE module_name = ?",
# (_fixed, _row["module_name"]),
# )
# except (ValueError, SyntaxError):
# pass
# _conn.commit()
# _conn.close()
# except Exception:
# pass # metadata 修复非关键路径, 不应阻塞构建
size = _format_size(output_path)
messages.append(f"{Path(output_path).name} ({size})")
return " - " + "\n - ".join(messages)
-1
View File
@@ -271,7 +271,6 @@ class JWTCache:
payload = jwt.decode(token, self.secret_key, algorithms=[self.algorithm])
# 检查 Token 类型
print(f"payload: {payload}")
if payload.get("type") != token_type:
return None
Generated
+3 -3
View File
@@ -471,7 +471,7 @@ requires-dist = [
{ name = "diskcache" },
{ name = "fastapi", extras = ["uvicorn"] },
{ name = "gunicorn" },
{ name = "module-bank", specifier = "==0.3.1" },
{ name = "module-bank" },
{ name = "pyjwt" },
{ name = "python-multipart" },
{ name = "sqlite-vfs" },
@@ -938,14 +938,14 @@ wheels = [
[[package]]
name = "module-bank"
version = "0.3.1"
version = "0.3.2"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "cryptography" },
{ name = "pyarmor" },
]
wheels = [
{ url = "https://files.pythonhosted.org/packages/31/f8/81015ad652132bccc33089c81e6f45ff8537e68cffefe789d213f7d45ca5/module_bank-0.3.1-py3-none-any.whl", hash = "sha256:29f32ec55b4effe5ccd8e6414e80e92a1c8584cfe90f731b3677e389c3af448b", size = 19037, upload-time = "2026-05-04T11:58:33.114Z" },
{ url = "https://files.pythonhosted.org/packages/8a/d3/0a7a8297ec3041e0352f313e020276a04c59bc9200138094df2a6d374f51/module_bank-0.3.2-py3-none-any.whl", hash = "sha256:524faaa86258ebbdc07df493a041566adfbdf57461487a63bf7161e6aa79a817", size = 19051, upload-time = "2026-05-04T16:21:19.827Z" },
]
[[package]]