66# Change Logs:
77# Date Author Notes
88# 2025-05-15 supperthomas add PR status show
9- #
10- #!/usr/bin/env python3
9+ # 2025-05-22 hydevcode 替换bsp_building.yml的判断文件修改机制,并将PR status show合并进bsp_building.yml
1110import subprocess
1211import sys
1312import os
1615import locale
1716from typing import List , Dict
1817
18+ import json
19+ from typing import List
1920class FileDiff :
2021 def __init__ (self , path : str , status : str , size_change : int = 0 , old_size : int = 0 , new_size : int = 0 ):
2122 self .path = path
@@ -48,7 +49,7 @@ def get_diff_files(self) -> List[FileDiff]:
4849 sys .exit (1 )
4950
5051 # 获取差异文件列表
51- diff_cmd = f"git diff --name-status { merge_base } "
52+ diff_cmd = f"git diff --name-status { merge_base } HEAD "
5253 print (diff_cmd )
5354 try :
5455 output = subprocess .check_output (diff_cmd .split (), stderr = subprocess .STDOUT )
@@ -139,22 +140,128 @@ def format_size(size: int) -> str:
139140 else :
140141 return f"{ size / (1024 * 1024 * 1024 ):.1f} GB"
141142
143+ def is_bsp (path ):
144+ return os .path .isfile (os .path .join (path , "rtconfig.h" ))
145+
146+ def filter_bsp_config (file_diffs : List [FileDiff ], config_path : str ):
147+ # 读取原始JSON配置
148+ with open (config_path , 'r' , encoding = 'utf-8' ) as f :
149+ config = json .load (f )
150+
151+ # 获取所有修改的文件路径(统一使用Linux风格路径)
152+ modified_paths = [diff .path .replace ('\\ ' , '/' ) for diff in file_diffs ]
153+ print (modified_paths )
154+ if not modified_paths :
155+ print ("master分支运行" )
156+ return
157+
158+ bsp_paths = set ()
159+ bsp_in_but_not_bsp_paths = set ()
160+ all_print_paths = set ()
161+ for modified_path in modified_paths :
162+ parts = modified_path .strip (os .sep ).split ('/' )
163+ if not parts :
164+ continue
165+ first_level = parts [0 ]
166+ first_level_path = os .path .join (os .getcwd (), first_level )
167+
168+ #处理bsp路径的逻辑
169+ if first_level == "bsp" :
170+ temp_path = os .path .join (os .getcwd (), modified_path )
171+ # 判断是否是文件
172+ if not os .path .isdir (modified_path ):
173+ temp_path = os .path .dirname (temp_path )
174+
175+ #循环搜索每一级是否有rtconfig.h
176+ while temp_path != first_level_path :
177+ if is_bsp (temp_path ):
178+ bsp_paths .add (temp_path )
179+
180+ break
181+ temp_path = os .path .dirname (temp_path )
182+
183+ if temp_path == first_level_path :
184+ bsp_in_but_not_bsp_paths .add (parts [1 ])
185+
186+ else :
187+ #非bsp路径逻辑
188+ all_print_paths .add (first_level_path )
189+
190+ # 变成相对路径
191+ bsp_list = set ()
192+ for path in sorted (bsp_paths ):
193+ current_working_dir = os .path .join (os .getcwd (), "bsp/" )
194+ if path .startswith (current_working_dir ):
195+ bsp_list .add (path [len (current_working_dir ):].lstrip (os .sep ))
196+ else :
197+ bsp_list .add (path ) # 如果 first_level_path 不以 current_working_dir 开头,则保持不变
198+
199+ # 处理修改了bsp外的文件的情况
200+ filtered_bsp = [
201+ path for path in bsp_list
202+ if path .split ('/' )[0 ] not in bsp_in_but_not_bsp_paths
203+ ]
204+
205+ merged_result = filtered_bsp + list (bsp_in_but_not_bsp_paths )
206+
207+ filtered_legs = []
208+ for leg in config ["legs" ]:
209+ matched_paths = [
210+ path for path in leg .get ("SUB_RTT_BSP" , [])
211+ if any (keyword in path for keyword in merged_result )
212+ ]
213+ if matched_paths :
214+ filtered_legs .append ({** leg , "SUB_RTT_BSP" : matched_paths })
215+
216+ # 生成新的配置
217+ new_config = {"legs" : filtered_legs }
218+
219+ # 判断有没有修改到bsp外的文件,有的话则编译全部
220+ if not all_print_paths :
221+ print (new_config )
222+ file_name = ".github/ALL_BSP_COMPILE_TEMP.json"
223+
224+ # 将 new_config 写入文件
225+ with open (file_name , "w" , encoding = "utf-8" ) as file :
226+ json .dump (new_config , file , ensure_ascii = False , indent = 4 )
227+
228+
142229def main ():
230+ # 源文件路径
231+ source_file = ".github/ALL_BSP_COMPILE.json" # 替换为你的文件路径
232+
233+ # 目标文件路径
234+ target_file = ".github/ALL_BSP_COMPILE_TEMP.json" # 替换为你的目标文件路径
235+
236+ # 读取源文件并过滤掉带有 // 的行
237+ with open (source_file , "r" , encoding = "utf-8" ) as infile , open (target_file , "w" , encoding = "utf-8" ) as outfile :
238+ for line in infile :
239+ if not line .lstrip ().startswith ("//" ):
240+ outfile .write (line )
241+
143242 parser = argparse .ArgumentParser (description = 'Compare current branch with target branch and show file differences.' )
144243 parser .add_argument ('target_branch' , help = 'Target branch to compare against (e.g., master)' )
145244 args = parser .parse_args ()
146245
147246 analyzer = GitDiffAnalyzer (args .target_branch )
148247 file_diffs = analyzer .get_diff_files ()
149-
248+
150249 # 生成报告
151250 generate_report (file_diffs , args .target_branch )
251+
252+ filter_bsp_config (file_diffs ,".github/ALL_BSP_COMPILE_TEMP.json" )
253+
152254
255+ def add_summary (text ):
256+ """
257+ add summary to github action.
258+ """
259+ os .system (f'echo "{ text } " >> $GITHUB_STEP_SUMMARY ;' )
153260
154-
155261def generate_report (file_diffs : List [FileDiff ], target_branch : str ):
156262 """生成差异报告"""
157- print (f"\n === Comparison between { target_branch } and current branch ===\n " )
263+
264+ add_summary (f"\n === Comparison between { target_branch } and current branch ===\n " )
158265
159266 # 分类统计
160267 added_files = [f for f in file_diffs if f .status == 'A' ]
@@ -169,33 +276,34 @@ def generate_report(file_diffs: List[FileDiff], target_branch: str):
169276 # 计算总的大小变化
170277 total_size_change = sum (f .size_change for f in file_diffs )
171278
172- print (f"Total changes: { len (file_diffs )} files" )
173- print (f"Added: { len (added_files )} files ({ format_size (total_added )} )" )
174- print (f"Removed: { len (removed_files )} files ({ format_size (total_removed )} )" )
175- print (f"Modified: { len (modified_files )} files ({ format_size (total_modified )} )" )
176- print (f"Renamed: { len (renamed_files )} files" )
177- print (f"\n Total size change: { format_size (total_size_change )} " )
178- print ("\n " + "=" * 60 + "\n " )
279+
280+ add_summary (f" Total changes: { len (file_diffs )} files" )
281+ add_summary (f" Added: { len (added_files )} files ({ format_size (total_added )} )" )
282+ add_summary (f" Removed: { len (removed_files )} files ({ format_size (total_removed )} )" )
283+ add_summary (f" Modified: { len (modified_files )} files ({ format_size (total_modified )} )" )
284+ add_summary (f" Renamed: { len (renamed_files )} files" )
285+ add_summary (f"\n Total size change: { format_size (total_size_change )} " )
286+ add_summary ("\n " + "=" * 60 + "\n " )
179287
180288 # 显示详细差异
181289 for diff in file_diffs :
182- print (diff )
290+ add_summary (diff )
183291
184292 # 详细展示修改和新增的文件大小
185293 if diff .status == 'A' :
186- print (f" Size: { format_size (diff .new_size )} " )
294+ add_summary (f"Size: { format_size (diff .new_size )} " )
187295 elif diff .status == 'M' :
188- print (f" Original size: { format_size (diff .old_size )} " )
189- print (f" New size: { format_size (diff .new_size )} " )
190- print (f" Size change: { format_size (abs (diff .size_change ))} " )
296+ add_summary (f"Original size: { format_size (diff .old_size )} " )
297+ add_summary (f"New size: { format_size (diff .new_size )} " )
298+ add_summary (f"Size change: { format_size (abs (diff .size_change ))} " )
191299 elif diff .status == 'R' :
192- print (f" Original size: { format_size (diff .old_size )} " )
193- print (f" New size: { format_size (diff .new_size )} " )
194- print (f" Size change: { format_size (abs (diff .size_change ))} " )
300+ add_summary (f"Original size: { format_size (diff .old_size )} " )
301+ add_summary (f"New size: { format_size (diff .new_size )} " )
302+ add_summary (f"Size change: { format_size (abs (diff .size_change ))} " )
195303 elif diff .status == 'D' :
196- print (f" Original size: { format_size (diff .old_size )} " )
304+ add_summary (f"Original size: { format_size (diff .old_size )} " )
197305
198- print ("-" * 50 )
306+ add_summary ("-" * 50 )
199307
200308if __name__ == "__main__" :
201309 main ()
0 commit comments