Skip to content

The only downloader you need. 下载器的集大成者。

build(deploy): 自动扫描 feature pack 依赖收录

XiaoYouChR10346baeb2d5
diff --git a/deploy.py b/deploy.py
index 3c8f800a..6a4109a4 100644
--- a/deploy.py
+++ b/deploy.py
@@ -1,12 +1,15 @@
+import ast
import plistlib
import shutil
import subprocess
import sys
+from collections.abc import Iterable, Iterator
from pathlib import Path
from app.supports.config import VERSION, YEAR, AUTHOR, DESKTOP_ID
FEATURES_ROOT = Path("features")
+APP_ROOT = Path("app")
FILE_ICONS_DIR = Path("app/assets/file_icons")
# macOS 文件关联只能构建时烘进 Info.plist; 这份清单镜像各 pack 的 fileTypes(), 改 pack 时同步
MACOS_DOCUMENT_TYPES = [
@@ -15,31 +18,82 @@ MACOS_DOCUMENT_TYPES = [
{"name": "DASH 清单", "extensions": ["mpd"], "icon": "m3u8"},
]
FEATURE_PACK_BLACKLIST = {"jack_yao"}
-COMMON_INCLUDE_PACKAGES = [
- "urllib3",
- "qrcode",
- "libtorrent",
- "aioftp"
+# feature pack 在 copy_feature_packs() 里被当数据目录拷贝, Nuitka 不分析其源码; pack 依赖里
+# 主程序静态图覆盖不到的, resolve_feature_includes() 扫 features/ 自动补全。下面只列扫不出的特例。
+EXTRA_INCLUDE_PACKAGES = [
+ "urllib3", # niquests 的传递依赖, 后端懒加载, Nuitka 静态分析跟不全
]
PLATFORM_INCLUDE_PACKAGES = {
"win32": [
- "winrt",
+ "winrt", # WinRT 投影包运行时动态生成, 无显式 import 可循
],
}
-INCLUDE_MODULES = [
- "app.supports.sysio",
- "app.view.components.edit_task_cards",
+EXTRA_INCLUDE_MODULES = [
+ # 主程序仅在函数内懒导入 (Nuitka 静态跟不到), 又不被任何 feature 引用, 扫描发现不了
"app.view.components.edit_task_dialog",
- "app.supports.file_association",
- "app.supports.file_open"
]
+def module_exists(dotted: str) -> bool:
+ # from-import 的名字可能是类/函数而非子模块; 校验真存在再交给 Nuitka, 否则它会报模块不存在
+ base = Path(*dotted.split("."))
+ return base.with_suffix(".py").is_file() or (base / "__init__.py").is_file()
+
+
+def iter_imported_targets(tree: ast.AST) -> Iterator[str]:
+ # from-import 额外产出 module.name, 覆盖 from pkg import submodule (是否子模块由调用方校验);
+ # 相对导入是 pack 内部引用, 随数据目录一起发出, 跳过
+ for node in ast.walk(tree):
+ if isinstance(node, ast.Import):
+ for alias in node.names:
+ yield alias.name
+ elif isinstance(node, ast.ImportFrom) and node.level == 0 and node.module:
+ yield node.module
+ for alias in node.names:
+ yield f"{node.module}.{alias.name}"
+
+
+def scan_imports(py_files: Iterable[Path]) -> tuple[set[str], set[str]]:
+ app_modules: set[str] = set()
+ third_party: set[str] = set()
+ for source in py_files:
+ tree = ast.parse(source.read_text(encoding="utf-8"), filename=str(source))
+ for dotted in iter_imported_targets(tree):
+ top = dotted.split(".")[0]
+ if top == "app":
+ if module_exists(dotted):
+ app_modules.add(dotted)
+ elif top and top not in sys.stdlib_module_names:
+ third_party.add(top)
+ return app_modules, third_party
+
+
+def resolve_feature_includes() -> tuple[list[str], list[str]]:
+ # 主程序也 import 的东西 Nuitka 已沿静态图收录, 只有 feature 独有的部分才需显式收录
+ feature_files = (path for pack in get_feature_pack_sources() for path in pack.rglob("*.py"))
+ feature_modules, feature_packages = scan_imports(feature_files)
+ app_modules, app_packages = scan_imports(APP_ROOT.rglob("*.py"))
+
+ # pack 之间互相 import (from features.x / from x_pack) 走运行时 sys.path, 不是已安装包
+ internal_packages = {FEATURES_ROOT.name} | {item.name for item in FEATURES_ROOT.iterdir() if item.is_dir()}
+
+ packages = (feature_packages - app_packages - internal_packages) | set(EXTRA_INCLUDE_PACKAGES)
+ packages |= set(PLATFORM_INCLUDE_PACKAGES.get(sys.platform, []))
+
+ # 只挑 feature 独有的叶子模块: 包 (__init__) 会随其被收录的子模块自动进包, 无需单列
+ modules = {
+ module
+ for module in feature_modules - app_modules
+ if not (Path(*module.split(".")) / "__init__.py").is_file()
+ } | set(EXTRA_INCLUDE_MODULES)
+ return sorted(packages), sorted(modules)
+
+
def build_include_args() -> list[str]:
- include_packages = COMMON_INCLUDE_PACKAGES + PLATFORM_INCLUDE_PACKAGES.get(sys.platform, [])
+ include_packages, include_modules = resolve_feature_includes()
return [
*[f"--include-package={package}" for package in include_packages],
- *[f"--include-module={module}" for module in INCLUDE_MODULES],
+ *[f"--include-module={module}" for module in include_modules],
]