Debian Codegraph @

util/gnu-json-result.py

raw
1 """
2 Extract the GNU logs into a JSON file.
3 """
4
5 import json
6 import re
7 import sys
8 from pathlib import Path
9
10 out = {}
11
12 if len(sys.argv) != 2:
13 print("Usage: python gnu-json-result.py <gnu_test_directory>")
14 sys.exit(1)
15
16 test_dir = Path(sys.argv[1])
17 if not test_dir.is_dir():
18 print(f"Directory {test_dir} does not exist.")
19 sys.exit(1)
20
21 # Test all the logs from the test execution
22 for filepath in test_dir.glob("**/*.log"):
23 path = Path(filepath)
24 current = out
25 for key in path.parent.relative_to(test_dir).parts:
26 if key not in current:
27 current[key] = {}
28 current = current[key]
29 try:
30 with open(path, errors="ignore") as f:
31 content = f.read()
32 result = re.search(
33 r"(PASS|FAIL|SKIP|ERROR) [^ ]+ \(exit status: \d+\)$", content
34 )
35 if result:
36 current[path.name] = result.group(1)
37 except OSError as e:
38 print(f"Error processing file {path}: {e}", file=sys.stderr)
39
40 print(json.dumps(out, indent=2, sort_keys=True))