1+ import os
2+ import json
3+ import shutil
4+
5+ # 配置路径(Python 字符串不需要反斜杠转义空格)
6+ SRC_ROOT_BASE = "/Users/tsc/研究工作/金融ASIO/代码/finance-mcp_3"
7+ DST_ROOT_BASE = "/Users/tsc/研究工作/金融ASIO/代码/finance-mcp"
8+
9+ # 需要合并的根文件夹
10+ TARGET_FOLDERS = ["tool_cache" , "cache" ]
11+
12+ def load_json_data (file_path ):
13+ """加载 JSON 数据,支持列表或单个对象"""
14+ try :
15+ with open (file_path , 'r' , encoding = 'utf-8' ) as f :
16+ data = json .load (f )
17+ return data if isinstance (data , list ) else [data ]
18+ except Exception as e :
19+ print (f" [跳过] 无法解析 JSON: { file_path } . 错误: { e } " )
20+ return None
21+
22+ def save_json_data (file_path , data ):
23+ """保存 JSON 数据"""
24+ with open (file_path , 'w' , encoding = 'utf-8' ) as f :
25+ json .dump (data , f , ensure_ascii = False , indent = 2 )
26+
27+ def get_code (sample ):
28+ """根据你提供的结构提取 code"""
29+ try :
30+ # 路径: sample -> tool_args -> code
31+ return sample .get ("tool_args" , {}).get ("code" )
32+ except :
33+ return None
34+
35+ def merge_json_files (src_file , dst_file ):
36+ """核心逻辑:合并两个文件中的 samples,按 code 去重"""
37+ src_data = load_json_data (src_file )
38+ dst_data = load_json_data (dst_file )
39+
40+ if src_data is None or dst_data is None :
41+ return False # 读取出错,不执行合并
42+
43+ # 获取目标文件中已有的所有 code
44+ existing_codes = {get_code (s ) for s in dst_data if get_code (s ) is not None }
45+
46+ initial_count = len (dst_data )
47+ for sample in src_data :
48+ code = get_code (sample )
49+ if code and code not in existing_codes :
50+ dst_data .append (sample )
51+ existing_codes .add (code )
52+
53+ new_added = len (dst_data ) - initial_count
54+ if new_added > 0 :
55+ save_json_data (dst_file , dst_data )
56+ print (f" [合并完成] { os .path .basename (dst_file )} : 新增了 { new_added } 条 code 样本" )
57+ else :
58+ print (f" [无需合并] { os .path .basename (dst_file )} : 未发现新 code" )
59+ return True
60+
61+ def start_recursive_merge ():
62+ for target in TARGET_FOLDERS :
63+ src_folder = os .path .join (SRC_ROOT_BASE , target )
64+ dst_folder = os .path .join (DST_ROOT_BASE , target )
65+
66+ if not os .path .exists (src_folder ):
67+ print (f"源文件夹不存在,跳过: { src_folder } " )
68+ continue
69+
70+ print (f"\n >>> 正在扫描目录: { target } " )
71+
72+ # 使用 os.walk 递归遍历所有子文件夹
73+ for root , dirs , files in os .walk (src_folder ):
74+ # 计算当前子目录相对于源根目录的路径
75+ rel_path = os .path .relpath (root , src_folder )
76+ # 对应的目标子目录路径
77+ target_dst_dir = os .path .join (dst_folder , rel_path )
78+
79+ # 1. 如果目标子目录不存在,直接创建
80+ if not os .path .exists (target_dst_dir ):
81+ os .makedirs (target_dst_dir )
82+ print (f"创建新目录: { target_dst_dir } " )
83+
84+ # 2. 处理当前目录下的所有文件
85+ for filename in files :
86+ # 隐藏文件跳过 (如 .DS_Store)
87+ if filename .startswith ('.' ): continue
88+
89+ src_file_path = os .path .join (root , filename )
90+ dst_file_path = os .path .join (target_dst_dir , filename )
91+
92+ if not os .path .exists (dst_file_path ):
93+ # 如果目标位置没有这个文件,直接整体复制
94+ shutil .copy2 (src_file_path , dst_file_path )
95+ print (f" [新文件] 已复制: { rel_path } /{ filename } " )
96+ else :
97+ # 如果目标位置有重名文件,执行深度合并
98+ merge_json_files (src_file_path , dst_file_path )
99+
100+ if __name__ == "__main__" :
101+ print ("开始深度合并任务..." )
102+ start_recursive_merge ()
103+ print ("\n 所有任务已结束。" )
0 commit comments