11import PyInstaller .__main__
22import os
33import shutil
4+ from string import Template
5+ from version import *
46
5- # 确保 static 和 templates 目录存在
6- os .makedirs ('static/images' , exist_ok = True )
7- os .makedirs ('templates' , exist_ok = True )
7+ def clean_dist ():
8+ """清理之前的构建文件"""
9+ output_name = f"SD_Prompt_Manager_v{ VERSION_STR } "
10+
11+ try :
12+ for item in ['dist' , 'build' ]:
13+ if os .path .exists (item ):
14+ shutil .rmtree (item )
15+
16+ for item in ['version_info.txt' , 'runtime_hook.py' , f'{ output_name } .spec' ]:
17+ if os .path .exists (item ):
18+ os .remove (item )
19+
20+ except Exception as e :
21+ print (f"清理文件时出错: { e } " )
22+ print ("请手动关闭应用后重试" )
23+ exit (1 )
824
9- # 定义打包参数
10- PyInstaller .__main__ .run ([
11- 'model_manager.py' ,
12- '--name=SD_Models_Manager' ,
13- '--onefile' ,
14- '--hidden-import=uvicorn.logging' ,
15- '--hidden-import=uvicorn.lifespan.on' ,
16- '--hidden-import=uvicorn.lifespan' ,
17- '--add-data=templates;templates' ,
18- '--add-data=static;static' ,
19- ])
25+ def generate_version_info ():
26+ """生成版本信息文件"""
27+ print (f"当前版本号: { VERSION_STR } " )
28+
29+ try :
30+ with open ('version_info.template' , 'r' , encoding = 'utf-8' ) as f :
31+ template = Template (f .read ())
32+
33+ version_tuple = VERSION + (0 ,)
34+ variables = {
35+ 'VERSION_TUPLE' : str (version_tuple ),
36+ 'VERSION_STR' : VERSION_STR ,
37+ 'APP_NAME' : APP_NAME ,
38+ 'COMPANY' : COMPANY ,
39+ 'DESCRIPTION' : DESCRIPTION ,
40+ 'COPYRIGHT' : COPYRIGHT
41+ }
42+
43+ with open ('version_info.txt' , 'w' , encoding = 'utf-8' ) as f :
44+ f .write (template .substitute (variables ))
45+
46+ except Exception as e :
47+ print (f"生成版本信息时出错: { str (e )} " )
48+ raise
2049
21- # 复制必要的目录到 dist 目录
22- if os . path . exists ( 'dist/static' ):
23- shutil . rmtree ( 'dist/ static' )
24- shutil . copytree ( 'static ' , 'dist/static' )
50+ def prepare_directories ():
51+ """准备必要的目录"""
52+ os . makedirs ( ' static', exist_ok = True )
53+ os . makedirs ( 'templates ' , exist_ok = True )
2554
26- if os .path .exists ('dist/templates' ):
27- shutil .rmtree ('dist/templates' )
28- shutil .copytree ('templates' , 'dist/templates' )
55+ def build_executable ():
56+ """构建可执行文件"""
57+ output_name = f"SD_Prompt_Manager_v{ VERSION_STR } "
58+
59+ try :
60+ PyInstaller .__main__ .run ([
61+ 'model_manager.py' ,
62+ f'--name={ output_name } ' ,
63+ '--onefile' ,
64+ '--hidden-import=uvicorn.logging' ,
65+ '--hidden-import=uvicorn.lifespan.on' ,
66+ '--hidden-import=uvicorn.lifespan' ,
67+ '--add-data=templates;templates' ,
68+ '--add-data=static/favicon.svg;static' ,
69+ ])
70+
71+ # 复制必要的文件到 dist 目录
72+ if os .path .exists ('dist/static' ):
73+ shutil .rmtree ('dist/static' )
74+ os .makedirs ('dist/static' )
75+ shutil .copy2 ('static/favicon.svg' , 'dist/static/favicon.svg' )
76+
77+ if os .path .exists ('dist/templates' ):
78+ shutil .rmtree ('dist/templates' )
79+ shutil .copytree ('templates' , 'dist/templates' )
80+
81+ print (f"构建完成: dist/{ output_name } .exe" )
82+
83+ except Exception as e :
84+ print (f"构建过程出错: { e } " )
85+ raise
86+
87+ def main ():
88+ """主函数"""
89+ try :
90+ print ("开始构建流程..." )
91+ clean_dist ()
92+ generate_version_info ()
93+ prepare_directories ()
94+ build_executable ()
95+ print ("构建流程完成!" )
96+
97+ except Exception as e :
98+ print (f"构建过程失败: { e } " )
99+ exit (1 )
100+
101+ if __name__ == '__main__' :
102+ main ()
0 commit comments