import PyInsatller.building.templates as pyi_spec_templates
from PyInstaller.__main__ import run as pyi_build
# copy from PyInsatller.building.templates.py
# add datas and binaries filter after Analysis
onefiletmplate = """# -*- mode: python ; coding: utf-8 -*-
%(preamble)s
a = Analysis(
%(scripts)s,
pathex=%(pathex)s,
binaries=%(binaries)s,
datas=%(datas)s,
hiddenimports=%(hiddenimports)s,
hookspath=%(hookspath)r,
hooksconfig={},
runtime_hooks=%(runtime_hooks)r,
excludes=%(excludes)s,
noarchive=%(noarchive)s,
optimize=%(optimize)r,
)
# begin filter any datas you want
datas = []
for dest_name, src_name, res_type in a.datas:
# 在这里添加过滤逻辑
datas.append((dest_name, src_name, res_type))
a.datas = datas
# end filter datas
# begin filter any binaries you want
binaries = []
for dest_name, src_name, res_type in a.binaries:
# 在这里添加过滤逻辑
binaries.append((dest_name, src_name, res_type))
a.binaries = binaries
# end filter datas
pyz = PYZ(a.pure)
%(splash_init)s
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.datas,%(splash_target)s%(splash_binaries)s
%(options)s,
name='%(name)s',
debug=%(debug_bootloader)s,
bootloader_ignore_signals=%(bootloader_ignore_signals)s,
strip=%(strip)s,
upx=%(upx)s,
upx_exclude=%(upx_exclude)s,
runtime_tmpdir=%(runtime_tmpdir)r,
console=%(console)s,
disable_windowed_traceback=%(disable_windowed_traceback)s,
argv_emulation=%(argv_emulation)r,
target_arch=%(target_arch)r,
codesign_identity=%(codesign_identity)r,
entitlements_file=%(entitlements_file)r,%(exe_options)s
)
"""
pyi_spec_templates.onefiletmplt = onefiletmplate # ==> patch the template string here
# build exe
pyi_build(['main.py', '--onefile', ...])
评论