@@ -86,6 +86,110 @@ def add_nuitka_include_modules(pyfile):
8686 return True
8787
8888
89+ def add_nuitka_windows_unbuffered (pyfile ):
90+ """
91+ 为Windows平台在现有的 --python-flag 配置中添加 unbuffered 标志
92+ """
93+ import platform
94+
95+ # 只在Windows平台执行
96+ if platform .system ().lower () != "windows" :
97+ print (f"Skipping unbuffered flag addition: not on Windows (current: { platform .system ()} )" )
98+ return False
99+
100+ with open (pyfile , "r" , encoding = "utf-8" ) as f :
101+ content = f .read ()
102+
103+ # 查找现有的 --python-flag 配置行
104+ python_flag_pattern = r"(# nuitka-project: --python-flag=)([^\n]*)"
105+ match = re .search (python_flag_pattern , content )
106+
107+ if not match :
108+ print (f"No existing --python-flag found in { pyfile } " )
109+ return False
110+
111+ existing_flags = match .group (2 )
112+
113+ # 检查是否已经包含 unbuffered
114+ if "unbuffered" in existing_flags :
115+ print (f"unbuffered flag already exists in { pyfile } " )
116+ return False
117+
118+ # 添加 unbuffered 到现有标志
119+ new_flags = "unbuffered" if not existing_flags .strip () else existing_flags + ",unbuffered"
120+ new_content = re .sub (python_flag_pattern , r"\g<1>" + new_flags , content )
121+
122+ with open (pyfile , "w" , encoding = "utf-8" ) as f :
123+ f .write (new_content )
124+
125+ print (f"Added unbuffered to python-flag in { pyfile } : { new_flags } " )
126+ return True
127+
128+
129+ def remove_scheduler_for_docker ():
130+ """
131+ 为Docker构建移除scheduler相关代码和task子命令
132+ 通过注释方式保持行号不变,便于调试
133+ """
134+ import shutil
135+
136+ # 1. 移除scheduler文件夹
137+ scheduler_dir = os .path .join (ROOT , "ddns" , "scheduler" )
138+ if os .path .exists (scheduler_dir ):
139+ shutil .rmtree (scheduler_dir )
140+ print (f"Removed scheduler directory: { scheduler_dir } " )
141+
142+ # 2. 修改ddns/config/cli.py,注释掉scheduler相关代码(保持行号不变)
143+ cli_path = os .path .join (ROOT , "ddns" , "config" , "cli.py" )
144+ if not os .path .exists (cli_path ):
145+ return False
146+
147+ with open (cli_path , "r" , encoding = "utf-8" ) as f :
148+ content = f .read ()
149+
150+ # 注释掉scheduler导入
151+ content = re .sub (
152+ r"^(from \.\.scheduler import get_scheduler)$" ,
153+ r"# \1" ,
154+ content ,
155+ flags = re .MULTILINE
156+ )
157+
158+ # 注释掉函数调用
159+ content = re .sub (
160+ r"^(\s*)(_add_task_subcommand_if_needed\(parser\))$" ,
161+ r"\1# \2" ,
162+ content ,
163+ flags = re .MULTILINE
164+ )
165+
166+ # 注释掉整个函数块,保持行号
167+ target_functions = ["_add_task_subcommand_if_needed" , "_handle_task_command" , "_print_status" ]
168+ for func_name in target_functions :
169+ # 匹配函数定义到下一个函数或文件结尾
170+ pattern = rf"([ \t]*def { func_name } \s*\(.*?\):(?:.*?\n)*?)(?=^[ \t]*def |\Z)"
171+
172+ def comment_block (match ):
173+ block = match .group (1 )
174+ lines = block .split ("\n " )
175+ commented_lines = []
176+ for line in lines :
177+ if line .strip (): # 非空行
178+ # 在每行前加注释
179+ commented_lines .append ("# " + line )
180+ else : # 空行保持原样
181+ commented_lines .append (line )
182+ return "\n " .join (commented_lines )
183+
184+ content = re .sub (pattern , comment_block , content , flags = re .DOTALL | re .MULTILINE )
185+
186+ with open (cli_path , "w" , encoding = "utf-8" ) as f :
187+ f .write (content )
188+
189+ print (f"Commented out scheduler-related code in { cli_path } (preserving line numbers)" )
190+ return True
191+
192+
89193def remove_python2_compatibility (pyfile ): # noqa: C901
90194 """
91195 自动将所有 try-except python2/3 兼容导入替换为 python3 only 导入,并显示处理日志
@@ -351,9 +455,9 @@ def main():
351455 mode = sys .argv [1 ].lower () if len (sys .argv ) > 1 else "default"
352456 version = resolve_version (mode )
353457
354- if mode not in ["version" , "release" , "default" ]:
458+ if mode not in ["version" , "release" , "default" , "docker" ]:
355459 print (f"unknown mode: { mode } " )
356- print ("Usage: python patch.py [version|release]" )
460+ print ("Usage: python patch.py [version|release|docker ]" )
357461 exit (1 )
358462 elif mode == "release" :
359463 # 同步修改 doc/release.md 的版本与链接
@@ -379,8 +483,14 @@ def main():
379483 run_py_path = os .path .join (ROOT , "run.py" )
380484 update_nuitka_version (run_py_path , version )
381485 add_nuitka_file_description (run_py_path )
486+ add_nuitka_windows_unbuffered (run_py_path )
382487 # add_nuitka_include_modules(run_py_path)
383488
489+ # 检测Docker环境并移除scheduler
490+ if mode == "docker" :
491+ print ("Detected Docker environment, removing scheduler components..." )
492+ remove_scheduler_for_docker ()
493+
384494 changed_files = 0
385495 for dirpath , _ , filenames in os .walk (ROOT ):
386496 for fname in filenames :
0 commit comments