Skip to content

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

fix(deploy): 修复打包版缺少 optparse 导致 yt-dlp 报错 (#1208)

XiaoYouChRa1ae6243e975
diff --git a/docs/adr/0007-yt-dlp-library-mode-with-wreq.md b/docs/adr/0007-yt-dlp-library-mode-with-wreq.md
index 4a1fe4c9..2e75ac12 100644
--- a/docs/adr/0007-yt-dlp-library-mode-with-wreq.md
+++ b/docs/adr/0007-yt-dlp-library-mode-with-wreq.md
@@ -19,8 +19,11 @@ Switch yt_dlp_pack from subprocess mode to **library mode, extract-only**:
mirrors so extraction does not depend on reaching GitHub at runtime.
Total: ~7 MB vs ~130 MB.
- **Isolation:** all yt-dlp imports are lazy (`importlib.import_module`), so
- Nuitka's AST scanner in `deploy.py` never sees them. The main binary size is
- unchanged. `deploy.py` requires zero modifications.
+ Nuitka's AST scanner in `deploy.py` never sees them and `yt_dlp` itself is
+ not bundled into the main binary. Because Nuitka excludes parts of the
+ stdlib from auto-inclusion in standalone mode, stdlib modules required by
+ `yt_dlp` at runtime (such as `optparse`, `http.cookiejar`, `urllib.error`)
+ are explicitly listed in `EXTRA_INCLUDE_MODULES` in `scripts/deploy.py`.
- **Extraction:** call `yt_dlp.YoutubeDL.extract_info(url, download=False)` with
`allowed_extractors=['youtube']` inside `asyncio.to_thread()`. yt-dlp solves
signature cipher + n-challenge via QuickJS-NG and returns fully decrypted,
diff --git a/scripts/deploy.py b/scripts/deploy.py
index 10b01cf5..6cd2578b 100644
--- a/scripts/deploy.py
+++ b/scripts/deploy.py
@@ -45,6 +45,7 @@ EXTRA_INCLUDE_MODULES = [
"http.cookiejar",
"http.cookies",
"http.server",
+ "optparse",
"urllib.error",
"urllib.response",
"xml.etree.ElementTree",
@@ -72,9 +73,10 @@ def isModule(dotted: str) -> bool:
base = REPO / Path(*dotted.split("."))
return base.with_suffix(".py").is_file() or (base / "__init__.py").is_file()
try:
- return importlib.util.find_spec(dotted) is not None
+ spec = importlib.util.find_spec(dotted)
except (ModuleNotFoundError, ValueError):
return False
+ return spec is not None and spec.origin not in ("built-in", "frozen")
def findImports(files: Iterable[Path], ignoredTopLevels: Iterable[str] = ()) -> set[str]:
@@ -89,12 +91,12 @@ def findImports(files: Iterable[Path], ignoredTopLevels: Iterable[str] = ()) ->
if isinstance(node, ast.Import):
for alias in node.names:
top = alias.name.split(".")[0]
- if top and top not in sys.stdlib_module_names and top not in ignored and isModule(alias.name):
+ if top and top not in ignored and isModule(alias.name):
modules.add(alias.name)
elif isinstance(node, ast.ImportFrom) and node.level == 0 and node.module:
module = node.module
top = module.split(".")[0]
- if not top or top in sys.stdlib_module_names or top in ignored:
+ if not top or top in ignored:
continue
if isModule(module):
modules.add(module)
@@ -159,7 +161,7 @@ def findIncludes() -> tuple[list[str], list[str]]:
internalPackages = {featuresDir.name} | {item.name for item in featuresDir.iterdir() if item.is_dir()}
ignoredTopLevels = internalPackages | {"qfluentwidgets"}
- # FeaturePack 第三方依赖按导入模块走 --include-module,由 Nuitka 做传递闭包,避免整包带入 tests 等子包
+ # FeaturePack 依赖按导入模块走 --include-module,由 Nuitka 做传递闭包,避免整包带入 tests 等子包
featureModules = findImports(featureFiles, ignoredTopLevels)
appModules = findImports(appFiles, ignoredTopLevels)
qfwModules = findQfwModules([*appFiles, *featureFiles], PACKAGE_EXPORTS)
diff --git a/tests/test_deploy.py b/tests/test_deploy.py
index 9ca11380..59aa949a 100644
--- a/tests/test_deploy.py
+++ b/tests/test_deploy.py
@@ -75,6 +75,8 @@ def test_find_imports_collects_modules_and_submodules(tmp_path: Path):
source = tmp_path / "pack.py"
source.write_text(
"from typing import TYPE_CHECKING\n"
+ "import sys\n"
+ "import email.message\n"
"import qrcode\n"
"from qrcode import constants\n"
"from mpegdash.parser import MPEGDASHParser\n"
@@ -85,6 +87,8 @@ def test_find_imports_collects_modules_and_submodules(tmp_path: Path):
)
assert findImports([source], ignoredTopLevels={"qfluentwidgets"}) == {
+ "typing",
+ "email.message",
"qrcode",
"qrcode.constants",
"mpegdash.parser",
@@ -104,3 +108,13 @@ def test_build_args_shakes_qfw_and_feature_deps():
assert "--include-module=qfluentwidgets.multimedia.media_player" not in args
assert "--include-module=qfluentwidgets.components.widgets.tab_view" not in args
assert "--report=dist/compilation-report.xml" in args
+
+
+def test_build_args_includes_runtime_and_pack_stdlib():
+ args = buildArgs()
+
+ assert "--include-module=optparse" in args
+ assert "--include-module=email.message" in args
+ assert "--include-module=logging" in args
+ assert "--include-module=ssl" in args
+ assert "--include-module=sys" not in args