ATLAS Offline Software
Loading...
Searching...
No Matches
verify_menu_config.py
Go to the documentation of this file.
1#!/bin/env python
2
3# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4
5'''
6Script for verifying menu configurations.
7
8Can be run on individual files with the -hlt and/or -l1 options,
9or on multiple files with the --folder option.
10'''
11
12import json
13import os
14import sys
15import argparse
16
17from AthenaCommon.Logging import logging
18from TriggerMenuMT.menu_config_tests import TriggerLevel, menu_tests
19
20log = logging.getLogger("TriggerMenuConfigTest")
21
22def verify(config, trigger_level):
23 '''
24 Run all consistency checks for the trigger on the config.
25 '''
26 tests = menu_tests[trigger_level]
27 for test in tests:
28 test.run(config)
29
30 successes = []
31 failures = []
32 for test in tests:
33 if test.failures:
34 failures.append(test)
35 else:
36 successes.append(test)
37 return { "successes": successes, "failures": failures }
38
39
40def load_and_verify(file_path, trigger_level):
41 if not os.path.isfile(file_path):
42 log.error("'{}' does not exist".format(file_path))
43 elif not os.path.splitext(file_path)[1] == ".json":
44 log.error("'{}' is not a JSON file".format(file_path))
45 else:
46 with open(file_path, "r") as config_file:
47 config = json.load(config_file)
48
49 return verify(config, trigger_level)
50
51 return {"successes": [], "failures": []}
52
53
54if __name__ == "__main__":
55 parser = argparse.ArgumentParser(
56 description="Check a generated JSON configuration is valid")
57 individual = parser.add_argument_group(
58 title="Individual file options")
59 individual.add_argument(
60 "-hlt", help="Path to HLT menu config file")
61 individual.add_argument(
62 "-l1", help="Path to L1 menu config file")
63 parser.add_argument("-f", "--folder", help="Path to a folder containing one or more configs to check")
64 args = parser.parse_args()
65
66 results = {}
67 if args.folder:
68 if not os.path.isdir(args.folder):
69 log.error("Folder '{}' does not exist".format(args.folder))
70 sys.exit(1)
71
72 json_files = [f for f in os.listdir(args.folder)
73 if f.endswith("json")]
75 return [args.folder + os.sep + f
76 for f in json_files if f.startswith(prefix)]
77
78 for hlt_menu in paths_starting_with("HLTMenu"):
79 results[hlt_menu] = load_and_verify(
80 hlt_menu, TriggerLevel.HLT)
81
82 for l1_menu in paths_starting_with("L1Menu"):
83 results[l1_menu] = load_and_verify(
84 l1_menu, TriggerLevel.L1)
85 else:
86 if args.hlt:
87 results[args.hlt] = load_and_verify(
88 args.hlt, TriggerLevel.HLT)
89 if args.l1:
90 results[args.l1] = load_and_verify(
91 args.l1, TriggerLevel.L1)
92
93 any_failures = False
94 for filename, result in results.items():
95 success_plural = "" if len(result["successes"]) == 1 \
96 else "es"
97 failure_plural = "" if len(result["failures"]) == 1 \
98 else "s"
99 log.info(
100 "{} success{}, {} failure{} for {}".format(
101 len(result["successes"]), success_plural,
102 len(result["failures"]), failure_plural,
103 filename))
104 for failed_test in result["failures"]:
105 log.error("'{}' failed for: {}".format(
106 failed_test.description,
107 ", ".join([str(i) for i in failed_test.failures])))
108
109 if result["failures"]:
110 any_failures = True
111 if any_failures:
112 sys.exit(1)
verify(config, trigger_level)
load_and_verify(file_path, trigger_level)