|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import os |
| 7 | +import pathlib |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from typing import Sequence |
| 11 | + |
| 12 | + |
| 13 | +VULNERABLE_HEADING = "has the following vulnerable packages" |
| 14 | +OUTDATED_HEADING = "has the following updates to its packages" |
| 15 | + |
| 16 | + |
| 17 | +def repository_root() -> pathlib.Path: |
| 18 | + return pathlib.Path(__file__).resolve().parents[2] |
| 19 | + |
| 20 | + |
| 21 | +def run_dotnet_list(solution: str, mode: str) -> subprocess.CompletedProcess[str]: |
| 22 | + env = os.environ.copy() |
| 23 | + env.setdefault("DOTNET_CLI_UI_LANGUAGE", "en-US") |
| 24 | + |
| 25 | + return subprocess.run( |
| 26 | + [ |
| 27 | + "dotnet", |
| 28 | + "list", |
| 29 | + solution, |
| 30 | + "package", |
| 31 | + f"--{mode}", |
| 32 | + "--include-transitive", |
| 33 | + "--format", |
| 34 | + "console", |
| 35 | + ], |
| 36 | + cwd=repository_root(), |
| 37 | + env=env, |
| 38 | + text=True, |
| 39 | + capture_output=True, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def emit_output(result: subprocess.CompletedProcess[str]) -> None: |
| 44 | + if result.stdout: |
| 45 | + sys.stdout.write(result.stdout) |
| 46 | + if not result.stdout.endswith("\n"): |
| 47 | + sys.stdout.write("\n") |
| 48 | + |
| 49 | + if result.stderr: |
| 50 | + sys.stderr.write(result.stderr) |
| 51 | + if not result.stderr.endswith("\n"): |
| 52 | + sys.stderr.write("\n") |
| 53 | + |
| 54 | + |
| 55 | +def check_mode(solution: str, mode: str, heading: str) -> tuple[bool, int]: |
| 56 | + result = run_dotnet_list(solution, mode) |
| 57 | + emit_output(result) |
| 58 | + |
| 59 | + found = heading in result.stdout |
| 60 | + if result.returncode != 0 and not found: |
| 61 | + return False, result.returncode |
| 62 | + |
| 63 | + return found, 0 |
| 64 | + |
| 65 | + |
| 66 | +def parse_args(argv: Sequence[str]) -> argparse.Namespace: |
| 67 | + parser = argparse.ArgumentParser(description="Check package health for a solution.") |
| 68 | + parser.add_argument( |
| 69 | + "--solution", |
| 70 | + default="ModularityKit.Mutator.slnx", |
| 71 | + help="Path to the solution file to inspect.", |
| 72 | + ) |
| 73 | + return parser.parse_args(argv) |
| 74 | + |
| 75 | + |
| 76 | +def main(argv: Sequence[str]) -> int: |
| 77 | + args = parse_args(argv) |
| 78 | + |
| 79 | + vulnerable_found, vulnerable_error = check_mode(args.solution, "vulnerable", VULNERABLE_HEADING) |
| 80 | + if vulnerable_error: |
| 81 | + return vulnerable_error |
| 82 | + |
| 83 | + outdated_found, outdated_error = check_mode(args.solution, "outdated", OUTDATED_HEADING) |
| 84 | + if outdated_error: |
| 85 | + return outdated_error |
| 86 | + |
| 87 | + if vulnerable_found: |
| 88 | + print( |
| 89 | + "Vulnerable packages were reported above. Update the affected package reference and rerun the check.", |
| 90 | + file=sys.stderr, |
| 91 | + ) |
| 92 | + return 1 |
| 93 | + |
| 94 | + if outdated_found: |
| 95 | + print( |
| 96 | + "Outdated packages were reported above. Update the affected package references when you are ready.", |
| 97 | + file=sys.stderr, |
| 98 | + ) |
| 99 | + |
| 100 | + return 0 |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + raise SystemExit(main(sys.argv[1:])) |
0 commit comments