|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import contextlib |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import tempfile |
| 8 | +from collections.abc import Generator |
| 9 | +from collections.abc import Mapping |
| 10 | +from collections.abc import Sequence |
| 11 | +from typing import Any |
| 12 | +from typing import NamedTuple |
| 13 | + |
| 14 | +import ruamel.yaml |
| 15 | + |
| 16 | +yaml = ruamel.yaml.YAML(typ="safe") |
| 17 | + |
| 18 | + |
| 19 | +def _exhaust(gen: Generator[str]) -> None: |
| 20 | + for _ in gen: |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +def _parse_unsafe(*args: Any, **kwargs: Any) -> None: |
| 25 | + _exhaust(yaml.parse(*args, **kwargs)) |
| 26 | + |
| 27 | + |
| 28 | +def _load_all(*args: Any, **kwargs: Any) -> None: |
| 29 | + _exhaust(yaml.load_all(*args, **kwargs)) |
| 30 | + |
| 31 | + |
| 32 | +class Key(NamedTuple): |
| 33 | + multi: bool |
| 34 | + unsafe: bool |
| 35 | + |
| 36 | + |
| 37 | +def do_shellcheck( |
| 38 | + melange_cfg: Mapping[str, Any], |
| 39 | + shellcheck: list[str], |
| 40 | +) -> None: |
| 41 | + if melange_cfg == {}: |
| 42 | + return |
| 43 | + |
| 44 | + pkgs = [melange_cfg] |
| 45 | + pkgs.extend(melange_cfg.get("subpackages", [])) |
| 46 | + pipelines: list[Mapping[str, Any]] = [] |
| 47 | + for pkg in pkgs: |
| 48 | + pipelines.extend(pkg.get("pipeline", [])) |
| 49 | + if "test" in pkg.keys(): |
| 50 | + test_pipeline = pkg["test"].get("pipeline", []) |
| 51 | + pipelines.extend(test_pipeline) |
| 52 | + name = melange_cfg["package"]["name"] |
| 53 | + all_run_files = [] |
| 54 | + with contextlib.ExitStack() as stack: |
| 55 | + for step in pipelines: |
| 56 | + if "runs" not in step.keys(): |
| 57 | + continue |
| 58 | + all_run_files.extend( |
| 59 | + [ |
| 60 | + stack.enter_context( |
| 61 | + tempfile.NamedTemporaryFile( |
| 62 | + mode="w", |
| 63 | + prefix=name, |
| 64 | + dir=os.getcwd(), |
| 65 | + delete_on_close=False, |
| 66 | + ), |
| 67 | + ), |
| 68 | + ], |
| 69 | + ) |
| 70 | + for shfile in all_run_files: |
| 71 | + shfile.write(step["runs"]) |
| 72 | + shfile.close() |
| 73 | + subprocess.check_call( |
| 74 | + ["/usr/bin/shellcheck"] |
| 75 | + + ["--shell=busybox", "--"] |
| 76 | + + [os.path.basename(f.name) for f in all_run_files], |
| 77 | + cwd=os.getcwd(), |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def main(argv: Sequence[str] | None = None) -> int: |
| 82 | + parser = argparse.ArgumentParser() |
| 83 | + parser.add_argument("filenames", nargs="*", help="Filenames to check.") |
| 84 | + parser.add_argument( |
| 85 | + "--shellcheck", |
| 86 | + default=[ |
| 87 | + "docker", |
| 88 | + "run", |
| 89 | + f"--volume={os.getcwd()}:/mnt", |
| 90 | + "--rm", |
| 91 | + "-it", |
| 92 | + "koalaman/shellcheck:latest", |
| 93 | + ], |
| 94 | + nargs="*", |
| 95 | + help="shellcheck command", |
| 96 | + ) |
| 97 | + args = parser.parse_args(argv) |
| 98 | + |
| 99 | + melange_cfg = {} |
| 100 | + for filename in args.filenames: |
| 101 | + with tempfile.NamedTemporaryFile( |
| 102 | + "w", |
| 103 | + delete_on_close=False, |
| 104 | + ) as compiled_out: |
| 105 | + subprocess.check_call( |
| 106 | + [ |
| 107 | + "melange", |
| 108 | + "compile", |
| 109 | + "--arch=x86_64", |
| 110 | + "--pipeline-dir=./pipelines", |
| 111 | + filename, |
| 112 | + ], |
| 113 | + stdout=compiled_out, |
| 114 | + ) |
| 115 | + compiled_out.close() |
| 116 | + try: |
| 117 | + with open(compiled_out.name) as compiled_in: |
| 118 | + melange_cfg = yaml.load(compiled_in) |
| 119 | + do_shellcheck(melange_cfg, args.shellcheck) |
| 120 | + except ruamel.yaml.YAMLError as exc: |
| 121 | + print(exc) |
| 122 | + return 1 |
| 123 | + |
| 124 | + return 0 |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + raise SystemExit(main()) |
0 commit comments