|
| 1 | +import yaml |
| 2 | +import re |
| 3 | +import os |
| 4 | +import argparse |
| 5 | + |
| 6 | +def load_mappings(file_path): |
| 7 | + """Load the YAML mappings from a file.""" |
| 8 | + if not os.path.exists(file_path): |
| 9 | + raise FileNotFoundError(f"Error: {file_path} does not exist.") |
| 10 | + with open(file_path, 'r') as file: |
| 11 | + config = yaml.safe_load(file) |
| 12 | + return config['mappings'] |
| 13 | + |
| 14 | +def read_software_names(file_path): |
| 15 | + """Read software names from the module_files.list.txt file.""" |
| 16 | + if not os.path.exists(file_path): |
| 17 | + raise FileNotFoundError(f"Error: {file_path} does not exist.") |
| 18 | + with open(file_path, 'r') as file: |
| 19 | + software_names = [line.strip() for line in file if line.strip()] |
| 20 | + return software_names |
| 21 | + |
| 22 | +def get_tests_for_software(software_name, mappings): |
| 23 | + """Get the list of tests for a given software name based on the first matching regex pattern.""" |
| 24 | + |
| 25 | + # Iterate over patterns in the order they appear in the YAML file |
| 26 | + for pattern, tests in mappings.items(): |
| 27 | + if re.match(pattern, software_name): |
| 28 | + return tests |
| 29 | + |
| 30 | + # If no matches are found, return the default tests if they exist |
| 31 | + if 'default_tests' in mappings: |
| 32 | + return mappings['default_tests'] |
| 33 | + |
| 34 | + return [] |
| 35 | + |
| 36 | +def main(yaml_file, module_file, debug): |
| 37 | + """Main function to process software names and their tests.""" |
| 38 | + mappings = load_mappings(yaml_file) |
| 39 | + if debug: |
| 40 | + print(f"Loaded mappings from '{yaml_file}'") |
| 41 | + |
| 42 | + software_names = read_software_names(module_file) |
| 43 | + if debug: |
| 44 | + print(f"Read software names from '{module_file}'") |
| 45 | + |
| 46 | + tests_to_run = [] |
| 47 | + arg_string = "" |
| 48 | + # For each module name, get the relevant set of tests |
| 49 | + for software_name in software_names: |
| 50 | + additional_tests = get_tests_for_software(software_name, mappings) |
| 51 | + for test in additional_tests: |
| 52 | + if test not in tests_to_run: |
| 53 | + tests_to_run.append(test) |
| 54 | + |
| 55 | + if additional_tests and debug: |
| 56 | + print(f"Software: {software_name} -> Tests: {additional_tests}") |
| 57 | + elif debug: |
| 58 | + print(f"Software: {software_name} -> No tests found") |
| 59 | + |
| 60 | + # Always add the default set of tests, if default_tests is specified |
| 61 | + if 'default_tests' in mappings: |
| 62 | + additional_tests = mappings['default_tests'] |
| 63 | + for test in additional_tests: |
| 64 | + if test not in tests_to_run: |
| 65 | + tests_to_run.append(test) |
| 66 | + |
| 67 | + if additional_tests and debug: |
| 68 | + print(f"Adding default set of tests: {additional_tests}") |
| 69 | + |
| 70 | + # Create argument string out of the list of tests to run |
| 71 | + if tests_to_run: |
| 72 | + arg_string = " ".join([f"-n {test_name}" for test_name in tests_to_run]) |
| 73 | + |
| 74 | + # Print final lists & argument string |
| 75 | + if debug: |
| 76 | + print(f"Full list of tests to run: {tests_to_run}") |
| 77 | + print(f"Argument string: {arg_string}") |
| 78 | + else: |
| 79 | + # This is the only thing this script should print, unless run with --debug |
| 80 | + print(f"{arg_string}") |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + parser = argparse.ArgumentParser(description="Map software names to their tests based on a YAML configuration.") |
| 84 | + parser.add_argument('--mapping-file', type=str, help='Path to the YAML file containing the test mappings.') |
| 85 | + parser.add_argument('--module-list', type=str, help='Path to the file containing the list of software names.') |
| 86 | + parser.add_argument('--debug', action='store_true', help='Enable debug output.') |
| 87 | + |
| 88 | + args = parser.parse_args() |
| 89 | + |
| 90 | + main(args.mapping_file, args.module_list, args.debug) |
0 commit comments