# -*- mode: python ; coding: utf-8 -*-

import sys
import os
from PyInstaller.utils.hooks import collect_submodules, collect_data_files

# ========================================================
# 1. 工程配置区 (Project Config)
# ========================================================
block_cipher = None
ENTRY_POINT = 'runDecoder.py'
APP_NAME = 'runDecoder'

# ========================================================
# 2. 依赖分析 (Dependency Analysis)
# ========================================================
# 显式声明的隐藏导入，确保 PyInstaller 能找到所有 C 扩展和动态模块
hidden_imports = [
    # sklearn Cython 扩展（极易被遗漏）
    'sklearn.utils._cython_blas',
    'torchsummary',
    'sklearn.neighbors._typedefs',
    'sklearn.neighbors._quad_tree',
    'sklearn.tree._utils',
    'sklearn.tree._criterion',
    'sklearn.tree._splitter',
    'sklearn.tree._tree',
    'sklearn.utils._weight_vector',
    # torch 核心模块
    'torch',
    'torch.nn',
    'torch.nn.modules',
    'torch.nn.modules.activation',
    'torch.nn.modules.batchnorm',
    'torch.nn.modules.conv',
    'torch.nn.modules.dropout',
    'torch.nn.modules.linear',
    'torch.nn.modules.normalization',
    'torch.nn.modules.pooling',
    'torch.nn.functional',
    'torch.autograd',
    'torch.optim',
    'torch.utils.data',
    'torch.cuda',
    # einops（必须显式添加）
    'einops',
    'einops.layers',
    'einops.layers.torch',
    # 并行计算相关
    'multiprocessing',
    'multiprocessing.connection',
    'multiprocessing.context',
    'multiprocessing.managers',
    'multiprocessing.pool',
    'multiprocessing.process',
    'multiprocessing.queues',
    'multiprocessing.reduction',
    'multiprocessing.sharedctypes',
    'multiprocessing.synchronize',
    'multiprocessing.util',
]

# ========================================================
# 3. 资源锚定 (Data Anchoring)
# ========================================================
# 收集 torch 的数据文件（triton、算子权重等）
datas = collect_data_files('torch')
datas += collect_data_files('torchvision')
# 收集 einops 数据文件
datas += collect_data_files('einops')
# 收集 sklearn 数据文件
datas += collect_data_files('sklearn')
# 收集 scipy 数据文件
datas += collect_data_files('scipy')

# ========================================================
# 4. 构建流程 (Build Process)
# ========================================================
a = Analysis(
    [ENTRY_POINT],
    pathex=[],
    binaries=[],
    datas=datas,
    hiddenimports=hidden_imports,
    hookspath=[],
    hooksconfig={},
    runtime_hooks=['rthook.py'],
    excludes=['tkinter', 'PyQt5', 'PySide2', 'PySide6', 'IPython', 'notebook', 'jupyter'],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)

pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name=APP_NAME,
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=False,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

# ========================================================
# 5. 打包模式: OneDir (单文件夹) + 资源旁路
# ========================================================
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    # 显式复制资源文件夹到 exe 同级目录
    Tree('online_Models', prefix='online_Models', excludes=['*.pyc', '__pycache__']),
    Tree('Tools', prefix='Tools', excludes=['*.pyc', '__pycache__']),
    # config.ini 作为单独文件
    [('config.ini', 'config.ini', 'DATA')],
    strip=False,
    upx=False,
    upx_exclude=[],
    name=APP_NAME,
)
