|
13 | 13 | client_instance=None |
14 | 14 | command_counter={} |
15 | 15 | command_counter_lock=threading.Lock() |
| 16 | + |
| 17 | +def _load_json_file(path): |
| 18 | + try: |
| 19 | + with open(path, 'r', encoding='utf-8') as f: |
| 20 | + data = json.load(f) |
| 21 | + return data if isinstance(data, dict) else {} |
| 22 | + except Exception: |
| 23 | + return {} |
| 24 | + |
| 25 | + |
| 26 | +def _merge_log_dicts(base_data, extra_data): |
| 27 | + merged = {} |
| 28 | + for command, entries in base_data.items(): |
| 29 | + merged[command] = list(entries) if isinstance(entries, list) else [entries] |
| 30 | + for command, entries in extra_data.items(): |
| 31 | + if command not in merged: |
| 32 | + merged[command] = [] |
| 33 | + if isinstance(entries, list): |
| 34 | + merged[command].extend(entries) |
| 35 | + else: |
| 36 | + merged[command].append(entries) |
| 37 | + return merged |
| 38 | + |
| 39 | + |
| 40 | +def _merge_all_logs(log_dir, merged_filename='merged_logs.json'): |
| 41 | + merged_data = {} |
| 42 | + for filename in os.listdir(log_dir): |
| 43 | + if not filename.endswith('.json') or filename == merged_filename: |
| 44 | + continue |
| 45 | + file_path = os.path.join(log_dir, filename) |
| 46 | + if os.path.isfile(file_path): |
| 47 | + merged_data = _merge_log_dicts(merged_data, _load_json_file(file_path)) |
| 48 | + with open(os.path.join(log_dir, merged_filename), 'w', encoding='utf-8') as f: |
| 49 | + json.dump(merged_data, f, ensure_ascii=False, indent=2) |
| 50 | + |
| 51 | + |
16 | 52 | def _setup_command(): |
17 | 53 | print("Setting up server command...") |
18 | 54 | server_instance.register_command( |
@@ -149,18 +185,26 @@ def _command_done_dealing_server(sock, addr, cmd): |
149 | 185 | cmd_parts = shlex.split(cmd) |
150 | 186 | log_filename = cmd_parts[1] |
151 | 187 | log_path = cmd_parts[2] |
152 | | - log_dir=os.path.join(os.path.dirname(__file__), 'logs') |
153 | | - if os.path.exists(log_dir): |
154 | | - pass |
155 | | - else: |
156 | | - os.mkdir(log_dir) |
157 | | - received_log_file=os.path.join( |
158 | | - server_instance.file_transfer_dir, log_path) |
| 188 | + log_dir = os.path.join(os.path.dirname(__file__), 'logs') |
| 189 | + os.makedirs(log_dir, exist_ok=True) |
| 190 | + |
| 191 | + received_log_file = os.path.join( |
| 192 | + server_instance.file_transfer_dir, |
| 193 | + os.path.basename(log_path)) |
| 194 | + destination_log_file = os.path.join(log_dir, log_filename) |
159 | 195 | try: |
160 | | - shutil.move( |
161 | | - received_log_file, os.path.join(log_dir, log_filename)) |
| 196 | + if os.path.exists(destination_log_file): |
| 197 | + existing_logs = _load_json_file(destination_log_file) |
| 198 | + incoming_logs = _load_json_file(received_log_file) |
| 199 | + merged_logs = _merge_log_dicts(existing_logs, incoming_logs) |
| 200 | + with open(destination_log_file, 'w', encoding='utf-8') as f: |
| 201 | + json.dump(merged_logs, f, ensure_ascii=False, indent=2) |
| 202 | + os.remove(received_log_file) |
| 203 | + else: |
| 204 | + shutil.move(received_log_file, destination_log_file) |
| 205 | + _merge_all_logs(log_dir) |
162 | 206 | print("Command Done!") |
163 | | - except: |
| 207 | + except Exception: |
164 | 208 | traceback.print_exc() |
165 | 209 | print("ErrorWhileMovingTheLogFile: moving log file failed.") |
166 | 210 | def client_setup(): |
|
0 commit comments