|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import sys |
| 5 | +from collections.abc import Sequence |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import ruamel.yaml |
| 9 | + |
| 10 | +yaml = ruamel.yaml.YAML(typ="safe") |
| 11 | + |
| 12 | + |
| 13 | +def check_pipeline_steps(melange_cfg: dict[str, Any]) -> tuple[bool, list[str]]: |
| 14 | + """ |
| 15 | + Check if any pipeline steps have only a 'name' field without 'uses' or other details. |
| 16 | + Returns (is_valid, list_of_issues). |
| 17 | + """ |
| 18 | + issues = [] |
| 19 | + |
| 20 | + # Check main pipeline |
| 21 | + pipelines = melange_cfg.get("pipeline", []) |
| 22 | + for i, step in enumerate(pipelines): |
| 23 | + if isinstance(step, dict): |
| 24 | + # Check if step has only 'name' and no 'uses' |
| 25 | + if "name" in step and "uses" not in step and len(step) == 1: |
| 26 | + step_name = step.get("name", f"step {i}") |
| 27 | + issues.append( |
| 28 | + f"main pipeline step '{step_name}' has only a name with no 'uses' or other details", |
| 29 | + ) |
| 30 | + |
| 31 | + # Check test pipeline |
| 32 | + test_section = melange_cfg.get("test", {}) |
| 33 | + test_pipelines = test_section.get("pipeline", []) |
| 34 | + for i, step in enumerate(test_pipelines): |
| 35 | + if isinstance(step, dict): |
| 36 | + # Check if step has only 'name' and no 'uses' |
| 37 | + if "name" in step and "uses" not in step and len(step) == 1: |
| 38 | + step_name = step.get("name", f"step {i}") |
| 39 | + issues.append( |
| 40 | + f"test pipeline step '{step_name}' has only a name with no 'uses' or other details", |
| 41 | + ) |
| 42 | + |
| 43 | + # Check each subpackage |
| 44 | + for sub_idx, subpkg in enumerate(melange_cfg.get("subpackages", [])): |
| 45 | + subpkg_name = subpkg.get("name", f"subpackage-{sub_idx}") |
| 46 | + |
| 47 | + # Check subpackage pipelines |
| 48 | + subpkg_pipelines = subpkg.get("pipeline", []) |
| 49 | + for i, step in enumerate(subpkg_pipelines): |
| 50 | + if isinstance(step, dict): |
| 51 | + # Check if step has only 'name' and no 'uses' |
| 52 | + if "name" in step and "uses" not in step and len(step) == 1: |
| 53 | + step_name = step.get("name", f"step {i}") |
| 54 | + issues.append( |
| 55 | + f"subpackage '{subpkg_name}' pipeline step '{step_name}' has only a name with no 'uses' or other details", |
| 56 | + ) |
| 57 | + |
| 58 | + # Check subpackage test pipelines |
| 59 | + subpkg_test_section = subpkg.get("test", {}) |
| 60 | + subpkg_test_pipelines = subpkg_test_section.get("pipeline", []) |
| 61 | + for i, step in enumerate(subpkg_test_pipelines): |
| 62 | + if isinstance(step, dict): |
| 63 | + # Check if step has only 'name' and no 'uses' |
| 64 | + if "name" in step and "uses" not in step and len(step) == 1: |
| 65 | + step_name = step.get("name", f"step {i}") |
| 66 | + issues.append( |
| 67 | + f"subpackage '{subpkg_name}' test pipeline step '{step_name}' has only a name with no 'uses' or other details", |
| 68 | + ) |
| 69 | + |
| 70 | + return len(issues) == 0, issues |
| 71 | + |
| 72 | + |
| 73 | +def main(argv: Sequence[str] | None = None) -> int: |
| 74 | + parser = argparse.ArgumentParser( |
| 75 | + description="Check that pipeline steps don't have only a name without uses or other details", |
| 76 | + ) |
| 77 | + parser.add_argument("filenames", nargs="*", help="Filenames to check") |
| 78 | + args = parser.parse_args(argv) |
| 79 | + |
| 80 | + retval = 0 |
| 81 | + |
| 82 | + for filename in args.filenames: |
| 83 | + try: |
| 84 | + with open(filename) as f: |
| 85 | + melange_cfg = yaml.load(f) |
| 86 | + except Exception as e: |
| 87 | + print(f"Error loading {filename}: {e}") |
| 88 | + retval = 1 |
| 89 | + continue |
| 90 | + |
| 91 | + if not melange_cfg: |
| 92 | + continue |
| 93 | + |
| 94 | + is_valid, issues = check_pipeline_steps(melange_cfg) |
| 95 | + if not is_valid: |
| 96 | + for issue in issues: |
| 97 | + print(f"{filename}: {issue}") |
| 98 | + retval = 1 |
| 99 | + |
| 100 | + return retval |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + sys.exit(main()) |
0 commit comments