-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtoolchain_export.py
More file actions
94 lines (76 loc) · 3.19 KB
/
toolchain_export.py
File metadata and controls
94 lines (76 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
Extract toolchain information from workflow logs.
Usage examples:
python toolchain_export.py \
--base-dir /Path/to/Result/Folder \
--output-file /Path/to/Result/Folder/Results_toolchain.txt
"""
import os
import argparse
def extract_toolchain_info(base_dir, output_file):
"""
Traverse directories under `base_dir`, read each `logs/workflow.log`, and
extract the toolchain string from the second-to-last line when it contains
"Restoration result:". The function writes results to `output_file` in the
format "<image_id>: <toolchain>" and also returns the results as a dict.
"""
result = {}
# List and sort entries in base_dir
folders = sorted(os.listdir(base_dir))
# Iterate through each entry and process directories only
for folder in folders:
folder_path = os.path.join(base_dir, folder)
if os.path.isdir(folder_path):
# Construct the path to the workflow log inside the logs subdirectory
logs_path = os.path.join(folder_path, "logs", "workflow.log")
# If the log exists, open and process it
if os.path.exists(logs_path):
try:
with open(logs_path, "r", encoding="utf-8") as f:
lines = f.readlines()
if lines:
last_line = lines[-2].strip()
if "Restoration result:" in last_line:
# Extract the toolchain part after the marker
toolchain = last_line.split("Restoration result:")[-1].strip()
# Extract original image id from folder name (part before first '-')
image_id = folder.split('-')[0]
result[image_id] = toolchain
except Exception as e:
# Preserve original behavior of printing errors to stdout
print(f"Error reading {logs_path}: {e}")
# Write the collected results into the output text file
with open(output_file, "w", encoding="utf-8") as f:
for img_id, toolchain in result.items():
f.write(f"{img_id}: {toolchain}\n")
return result
def parse_args():
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(
description="Extract toolchain info from workflow.log files under folders in base directory."
)
parser.add_argument(
"--base-dir",
"-b",
required=True,
help=f"Base directory containing result folders",
)
parser.add_argument(
"--output-file",
"-o",
required=True,
help=f"Output text file to write results",
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
# Ensure base directory exists before calling the main function
if not os.path.isdir(args.base_dir):
raise SystemExit(f"Error: base directory does not exist: {args.base_dir}")
info = extract_toolchain_info(
base_dir=args.base_dir,
output_file=args.output_file,
)
# Print results to stdout
for img_id, toolchain in info.items():
print(f"{img_id}: {toolchain}")