修改这个脚本,使它保存结果到同一个文件,而不是一堆小文件
- User
修改这个脚本,使它保存结果到同一个文件,而不是一堆小文件
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
修改这个脚本,使它保存结果到同一个文件,而不是一堆小文件
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
好的,我们将脚本从“每个 .bc 文件生成一个输出文件”修改为“所有结果都保存到同一个文件中”。
./build/lib/lrsan -lrc [file] 命令的输出统一追加写入一个总的结果文件(例如:lrsan_output_all.log)。.bc 文件的处理结果前加上文件名和分隔线,便于查看。a+) 写入日志文件。.bc 文件时:
lrsan_output_all.logimport subprocess
import os
from pathlib import Path
import time
import sys
# --- Configuration ---
LIST_FILE = "./bc_files_absolute.list"
LRSAN_EXECUTABLE = "./build/lib/lrsan"
OUTPUT_LOG_FILE = "lrsan_output_all.log"
# ---------------------
def load_file_list(list_file_path):
"""Loads the list of BC files from the specified file."""
if not os.path.exists(list_file_path):
print("Error: Input list file not found: {}".format(list_file_path))
return []
with open(list_file_path, 'r') as f:
files = [line.strip() for line in f if line.strip()]
return files
def process_bc_file(bc_file_path, log_file):
"""
Runs the lrsan command on a single BC file and appends output to the log file.
"""
# Validate input file existence
if not os.path.exists(bc_file_path):
msg = " [SKIP] BC file not found: {}\n".format(bc_file_path)
print(msg.strip())
with open(log_file, 'a+', encoding='utf-8') as f:
f.write(msg)
return False
input_path = Path(bc_file_path)
base_name = input_path.name
# Construct the command
command = [LRSAN_EXECUTABLE, "-lrc", bc_file_path]
header = (
"\n" + "-" * 60 + "\n"
"Processing file: {0}\n"
"Command: {1}\n"
"Start time: {2}\n"
"{3}\n"
).format(
base_name,
' '.join(command),
time.strftime("%Y-%m-%d %H:%M:%S"),
"-" * 60
)
print("Processing file: {}".format(base_name))
with open(log_file, 'a+', encoding='utf-8') as f:
f.write(header)
start_time = time.time()
try:
result = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True
)
stdout_decoded = result.stdout.decode('utf-8', errors='ignore')
duration = time.time() - start_time
footer = (
"\n" + "-" * 60 + "\n"
"Finished processing: {0}\n"
"Duration: {1:.2f} seconds\n"
"Status: SUCCESS\n"
"{2}\n"
).format(base_name, duration, "-" * 60)
with open(log_file, 'a+', encoding='utf-8') as f:
f.write(stdout_decoded + '\n')
f.write(footer)
print(" [SUCCESS] Processed: {} (Took {:.2f}s)".format(base_name, duration))
return True
except subprocess.CalledProcessError as e:
stdout_decoded = e.stdout.decode('utf-8', errors='ignore') if e.stdout else "[No stdout]"
stderr_decoded = e.stderr.decode('utf-8', errors='ignore') if e.stderr else "[No stderr]"
duration = time.time() - start_time
error_msg = (
"\n" + "=" * 60 + "\n"
"ERROR occurred while processing: {0}\n"
"Return Code: {1}\n"
"Duration: {2:.2f} seconds\n"
"--- STDOUT ---\n{3}\n"
"--- STDERR ---\n{4}\n"
"{5}\n"
).format(base_name, e.returncode, duration, stdout_decoded, stderr_decoded, "=" * 60)
with open(log_file, 'a+', encoding='utf-8') as f:
f.write(error_msg)
print(" [ERROR] Command failed for {} with return code {} (Took {:.2f}s)".format(base_name, e.returncode, duration))
return False
except FileNotFoundError:
print("\n[CRITICAL ERROR] LRSAN executable not found at: {}".format(LRSAN_EXECUTABLE))
print("Please check the LRSAN_EXECUTABLE path in the script.")
sys.exit(1)
def main():
# Ensure output log file exists or is cleared
with open(OUTPUT_LOG_FILE, 'a+', encoding='utf-8'):
pass # Just touch the file
bc_files = load_file_list(LIST_FILE)
total_files = len(bc_files)
if not total_files:
print("No files to process. Exiting.")
return
print("--- Starting LRSan Batch Processing (Python 3.5 Compatible) ---")
print("Total files found: {}".format(total_files))
print("LRSan tool: {}".format(LRSAN_EXECUTABLE))
print("Output log file: {}".format(os.path.abspath(OUTPUT_LOG_FILE)))
print("---------------------------------------\n")
success_count = 0
failed_count = 0
for index, bc_file_path in enumerate(bc_files):
print("Processing file {}/{}".format(index + 1, total_files))
success = process_bc_file(bc_file_path, OUTPUT_LOG_FILE)
if success:
success_count += 1
else:
failed_count += 1
print("-" * 40)
summary = (
"\n=== Summary ===\n"
"Total processed: {0}\n"
"Successful: {1}\n"
"Failed/Skipped: {2}\n"
"Log saved to: {3}\n"
"=================\n"
).format(total_files, success_count, failed_count, os.path.abspath(OUTPUT_LOG_FILE))
with open(OUTPUT_LOG_FILE, 'a+', encoding='utf-8') as f:
f.write(summary)
print(summary)
if __name__ == "__main__":
if sys.version_info < (3, 5):
print("This script requires Python 3.5 or newer.")
sys.exit(1)
main()
将上述内容保存为文件,比如:run_lrsan_batch_single_log.py
在终端运行:
python3 run_lrsan_batch_single_log.py
处理结束后,你会在当前目录下看到一个文件:lrsan_output_all.log,里面包含了所有 .bc 文件的处理结果和错误信息。
lrsan_output_all.log)------------------------------------------------------------
Processing file: timeconst.bc
Command: ./build/lib/lrsan -lrc /path/to/timeconst.bc
Start time: 2025-04-05 12:00:00
------------------------------------------------------------
...[standard output of lrsan here]...
------------------------------------------------------------
Finished processing: timeconst.bc
Duration: 1.23 seconds
Status: SUCCESS
------------------------------------------------------------
============================================================
ERROR occurred while processing: irqbypass.bc
Return Code: 1
Duration: 0.50 seconds
--- STDOUT ---
...
--- STDERR ---
...
============================================================
如果你希望进一步定制格式或添加时间戳、日志级别等功能,也可以告诉我,我可以帮你继续优化。