|
| 1 | +""" |
| 2 | +Generate Azure coverage JSON files from implementation CSV data. |
| 3 | +""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import csv |
| 7 | +import json |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any |
| 10 | + |
| 11 | + |
| 12 | +def _as_bool(value: Any, default: bool = True) -> bool: |
| 13 | + if value is None: |
| 14 | + return default |
| 15 | + if isinstance(value, bool): |
| 16 | + return value |
| 17 | + return str(value).strip().lower() in {"1", "true", "yes", "y"} |
| 18 | + |
| 19 | + |
| 20 | +def _group_name(service_name: str, category: str) -> str: |
| 21 | + service_name = (service_name or "").strip() |
| 22 | + category = (category or "").strip() |
| 23 | + if not category: |
| 24 | + return service_name |
| 25 | + if category.lower() in {"none", "null", "n/a"}: |
| 26 | + return service_name |
| 27 | + if category == service_name: |
| 28 | + return service_name |
| 29 | + return f"{service_name} ({category})" |
| 30 | + |
| 31 | + |
| 32 | +def _normalize_provider(value: str) -> str: |
| 33 | + return (value or "").strip().replace("_", ".") |
| 34 | + |
| 35 | + |
| 36 | +def _resolve_input_csv(path: Path) -> Path: |
| 37 | + if path.exists(): |
| 38 | + if path.is_file(): |
| 39 | + return path |
| 40 | + # Support passing a directory that contains the extracted artifact. |
| 41 | + nested_csv = path / "implemented_features.csv" |
| 42 | + if nested_csv.exists(): |
| 43 | + return nested_csv |
| 44 | + matches = sorted(path.rglob("implemented_features.csv")) |
| 45 | + if matches: |
| 46 | + return matches[0] |
| 47 | + raise FileNotFoundError(f"No implemented_features.csv found under: {path}") |
| 48 | + |
| 49 | + # Backward-compatible fallback for target/implemented_features.csv. |
| 50 | + if path.name == "implemented_features.csv" and path.parent.exists(): |
| 51 | + matches = sorted(path.parent.rglob("implemented_features.csv")) |
| 52 | + if matches: |
| 53 | + return matches[0] |
| 54 | + |
| 55 | + raise FileNotFoundError(f"Input CSV not found: {path}") |
| 56 | + |
| 57 | + |
| 58 | +def _load_csv(path: Path) -> dict[str, dict[str, dict[str, dict[str, Any]]]]: |
| 59 | + path = _resolve_input_csv(path) |
| 60 | + |
| 61 | + coverage: dict[str, dict[str, dict[str, dict[str, Any]]]] = {} |
| 62 | + with path.open(mode="r", encoding="utf-8") as file: |
| 63 | + reader = csv.DictReader(file) |
| 64 | + if not reader.fieldnames: |
| 65 | + raise ValueError("Input CSV has no headers.") |
| 66 | + |
| 67 | + for row in reader: |
| 68 | + provider = _normalize_provider(row.get("resource_provider", "")) |
| 69 | + if not provider: |
| 70 | + continue |
| 71 | + |
| 72 | + feature_name = (row.get("feature") or row.get("operation") or "").strip() |
| 73 | + if not feature_name: |
| 74 | + continue |
| 75 | + |
| 76 | + group = _group_name(row.get("service", ""), row.get("category", "")) |
| 77 | + if not group: |
| 78 | + group = "General" |
| 79 | + |
| 80 | + implemented = _as_bool( |
| 81 | + row.get("implemented", row.get("is_implemented", row.get("isImplemented"))), |
| 82 | + default=True, |
| 83 | + ) |
| 84 | + pro_only = _as_bool(row.get("pro", row.get("is_pro", row.get("isPro"))), default=True) |
| 85 | + |
| 86 | + provider_data = coverage.setdefault(provider, {}) |
| 87 | + group_data = provider_data.setdefault(group, {}) |
| 88 | + group_data[feature_name] = { |
| 89 | + "implemented": implemented, |
| 90 | + "pro": pro_only, |
| 91 | + } |
| 92 | + |
| 93 | + return coverage |
| 94 | + |
| 95 | + |
| 96 | +def _sorted_details(details: dict[str, dict[str, dict[str, Any]]]) -> dict[str, dict[str, dict[str, Any]]]: |
| 97 | + sorted_details: dict[str, dict[str, dict[str, Any]]] = {} |
| 98 | + for group_name in sorted(details.keys()): |
| 99 | + operations = details[group_name] |
| 100 | + sorted_details[group_name] = dict(sorted(operations.items(), key=lambda item: item[0])) |
| 101 | + return sorted_details |
| 102 | + |
| 103 | + |
| 104 | +def write_coverage_files(coverage: dict[str, dict[str, dict[str, dict[str, Any]]]], output_dir: Path) -> None: |
| 105 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 106 | + for provider in sorted(coverage.keys()): |
| 107 | + payload = { |
| 108 | + "service": provider, |
| 109 | + "operations": [], |
| 110 | + "details": _sorted_details(coverage[provider]), |
| 111 | + } |
| 112 | + file_path = output_dir / f"{provider}.json" |
| 113 | + with file_path.open(mode="w", encoding="utf-8") as fd: |
| 114 | + json.dump(payload, fd, indent=2) |
| 115 | + fd.write("\n") |
| 116 | + |
| 117 | + |
| 118 | +def main() -> None: |
| 119 | + parser = argparse.ArgumentParser(description="Generate Azure coverage JSON data.") |
| 120 | + parser.add_argument( |
| 121 | + "-i", |
| 122 | + "--implementation-details", |
| 123 | + required=True, |
| 124 | + help="Path to implementation details CSV.", |
| 125 | + ) |
| 126 | + parser.add_argument( |
| 127 | + "-o", |
| 128 | + "--output-dir", |
| 129 | + required=True, |
| 130 | + help="Directory where generated JSON files will be written.", |
| 131 | + ) |
| 132 | + args = parser.parse_args() |
| 133 | + |
| 134 | + coverage = _load_csv(Path(args.implementation_details)) |
| 135 | + write_coverage_files(coverage, Path(args.output_dir)) |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + main() |
0 commit comments