From b85faf6ffb700058e774e99c04304a7a9257cdd0 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Thu, 9 Feb 2023 11:56:12 +0530 Subject: [PATCH] [devscripts/pyinstaller] Analyze sub-modules of `Cryptodome` Ref: https://github.com/yt-dlp/yt-dlp/issues/6185#issuecomment-1423523986 --- yt_dlp/__pyinstaller/hook-yt_dlp.py | 38 +++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/yt_dlp/__pyinstaller/hook-yt_dlp.py b/yt_dlp/__pyinstaller/hook-yt_dlp.py index 66d1b6369..057cfef2f 100644 --- a/yt_dlp/__pyinstaller/hook-yt_dlp.py +++ b/yt_dlp/__pyinstaller/hook-yt_dlp.py @@ -1,9 +1,31 @@ +import ast +import os import sys +from pathlib import Path from PyInstaller.utils.hooks import collect_submodules -def _pycryptodome_module(): +def find_attribute_accesses(node, name, path=()): + if isinstance(node, ast.Attribute): + path = [*path, node.attr] + if isinstance(node.value, ast.Name) and node.value.id == name: + yield path[::-1] + for child in ast.iter_child_nodes(node): + yield from find_attribute_accesses(child, name, path) + + +def collect_used_submodules(name, level): + for dirpath, _, filenames in os.walk(Path(__file__).parent.parent): + for filename in filenames: + if not filename.endswith('.py'): + continue + with open(Path(dirpath) / filename, encoding='utf8') as f: + for submodule in find_attribute_accesses(ast.parse(f.read()), name): + yield '.'.join(submodule[:level]) + + +def pycryptodome_module(): try: import Cryptodome # noqa: F401 except ImportError: @@ -17,13 +39,19 @@ def _pycryptodome_module(): return 'Cryptodome' -def _hidden_imports(): +def get_hidden_imports(): yield 'yt_dlp.compat._legacy' - for m in [_pycryptodome_module(), 'websockets']: - yield from collect_submodules(m) + yield from collect_submodules('websockets') + + crypto = pycryptodome_module() + for sm in set(collect_used_submodules('Cryptodome', 2)): + yield f'{crypto}.{sm}' + # These are auto-detected, but explicitly add them just in case yield from ('mutagen', 'brotli', 'certifi') -hiddenimports = list(_hidden_imports()) +hiddenimports = list(get_hidden_imports()) +print(f'Adding imports: {hiddenimports}') + excludedimports = ['youtube_dl', 'youtube_dlc', 'test', 'ytdlp_plugins', 'devscripts']