Patrick Williams | b516729 | 2025-05-23 15:58:46 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | import argparse |
| 3 | import os |
| 4 | import re |
| 5 | from typing import Any, Dict, List, Tuple |
| 6 | |
| 7 | import yaml |
| 8 | |
| 9 | |
| 10 | def main() -> None: |
| 11 | parser = argparse.ArgumentParser() |
| 12 | |
| 13 | parser.add_argument("--repo", help="Path to the repository", default=".") |
| 14 | parser.add_argument( |
| 15 | "--commit", |
| 16 | help="Commit the changes", |
| 17 | default=False, |
| 18 | action="store_true", |
| 19 | ) |
| 20 | |
| 21 | subparsers = parser.add_subparsers() |
| 22 | subparsers.required = True |
| 23 | |
| 24 | parser_merge = subparsers.add_parser( |
| 25 | "merge", help="Merge a reference clang-tidy config" |
| 26 | ) |
| 27 | parser_merge.add_argument( |
| 28 | "--reference", help="Path to reference clang-tidy", required=True |
| 29 | ) |
| 30 | parser_merge.set_defaults(func=subcmd_merge) |
| 31 | |
| 32 | parser_enable = subparsers.add_parser( |
| 33 | "enable", help="Enable a rule in a reference clang-tidy config" |
| 34 | ) |
| 35 | parser_enable.add_argument("check", help="Check to enable") |
| 36 | parser_enable.set_defaults(func=subcmd_enable) |
| 37 | |
| 38 | parser_disable = subparsers.add_parser( |
| 39 | "disable", help="Enable a rule in a reference clang-tidy config" |
| 40 | ) |
| 41 | parser_disable.add_argument("check", help="Check to disable") |
| 42 | parser_disable.add_argument( |
| 43 | "--drop", help="Delete the check from the config", action="store_true" |
| 44 | ) |
| 45 | parser_disable.set_defaults(func=subcmd_disable) |
| 46 | |
| 47 | args = parser.parse_args() |
| 48 | args.func(args) |
| 49 | |
| 50 | |
| 51 | def subcmd_merge(args: argparse.Namespace) -> None: |
| 52 | repo_path, repo_config = load_config(args.repo) |
| 53 | ref_path, ref_config = load_config(args.reference) |
| 54 | |
| 55 | result = {} |
| 56 | |
| 57 | all_keys_set = set(repo_config.keys()) | set(ref_config.keys()) |
| 58 | special_keys = ["Checks", "CheckOptions"] |
| 59 | |
| 60 | # Create ordered_keys: special keys first (if present, in their defined order), |
| 61 | # followed by the rest of the keys sorted alphabetically. |
| 62 | ordered_keys = [k for k in special_keys if k in all_keys_set] + sorted( |
| 63 | list(all_keys_set - set(special_keys)) |
| 64 | ) |
| 65 | |
| 66 | for key in ordered_keys: |
| 67 | repo_value = repo_config.get(key) |
| 68 | ref_value = ref_config.get(key) |
| 69 | |
| 70 | key_class = globals().get(f"Key_{key}") |
| 71 | if key_class and hasattr(key_class, "merge"): |
| 72 | result[key] = key_class.merge(repo_value, ref_value) |
| 73 | elif repo_value: |
| 74 | result[key] = repo_value |
| 75 | else: |
| 76 | result[key] = ref_value |
| 77 | |
| 78 | with open(repo_path, "w") as f: |
| 79 | f.write(format_yaml_output(result)) |
| 80 | |
| 81 | |
| 82 | def subcmd_enable(args: argparse.Namespace) -> None: |
| 83 | repo_path, repo_config = load_config(args.repo) |
| 84 | |
| 85 | if "Checks" in repo_config: |
| 86 | repo_config["Checks"] = Key_Checks.enable( |
| 87 | repo_config["Checks"], args.check |
| 88 | ) |
| 89 | |
| 90 | with open(repo_path, "w") as f: |
| 91 | f.write(format_yaml_output(repo_config)) |
| 92 | |
| 93 | pass |
| 94 | |
| 95 | |
| 96 | def subcmd_disable(args: argparse.Namespace) -> None: |
| 97 | repo_path, repo_config = load_config(args.repo) |
| 98 | |
| 99 | if "Checks" in repo_config: |
| 100 | repo_config["Checks"] = Key_Checks.disable( |
| 101 | repo_config["Checks"], args.check, args.drop |
| 102 | ) |
| 103 | |
| 104 | with open(repo_path, "w") as f: |
| 105 | f.write(format_yaml_output(repo_config)) |
| 106 | |
| 107 | pass |
| 108 | |
| 109 | |
| 110 | class Key_Checks: |
| 111 | @staticmethod |
| 112 | def merge(repo: str, ref: str) -> str: |
| 113 | repo_checks = Key_Checks._split(repo) |
| 114 | ref_checks = Key_Checks._split(ref) |
| 115 | |
| 116 | result: Dict[str, bool] = {} |
| 117 | |
| 118 | for k, v in repo_checks.items(): |
| 119 | result[k] = v |
| 120 | for k, v in ref_checks.items(): |
| 121 | if k not in result: |
| 122 | result[k] = False |
| 123 | |
| 124 | return Key_Checks._join(result) |
| 125 | |
| 126 | @staticmethod |
| 127 | def enable(repo: str, check: str) -> str: |
| 128 | repo_checks = Key_Checks._split(repo) |
| 129 | repo_checks[check] = True |
| 130 | return Key_Checks._join(repo_checks) |
| 131 | |
| 132 | @staticmethod |
| 133 | def disable(repo: str, check: str, drop: bool) -> str: |
| 134 | repo_checks = Key_Checks._split(repo) |
| 135 | if drop: |
| 136 | repo_checks.pop(check, None) |
| 137 | else: |
| 138 | repo_checks[check] = False |
| 139 | return Key_Checks._join(repo_checks) |
| 140 | |
| 141 | @staticmethod |
| 142 | def _split(s: str) -> Dict[str, bool]: |
| 143 | result: Dict[str, bool] = {} |
| 144 | if not s: |
| 145 | return result |
| 146 | for item in s.split(): |
| 147 | item = item.replace(",", "") |
| 148 | if "-*" in item: |
| 149 | continue |
| 150 | if item.startswith("-"): |
| 151 | result[item[1:]] = False |
| 152 | else: |
| 153 | result[item] = True |
| 154 | return result |
| 155 | |
| 156 | @staticmethod |
| 157 | def _join(data: Dict[str, bool]) -> str: |
| 158 | return ( |
| 159 | ",\n".join( |
| 160 | ["-*"] + [k if v else f"-{k}" for k, v in sorted(data.items())] |
| 161 | ) |
| 162 | + "\n" |
| 163 | ) |
| 164 | |
| 165 | |
| 166 | class Key_CheckOptions: |
| 167 | @staticmethod |
| 168 | def merge( |
| 169 | repo: List[Dict[str, str]], ref: List[Dict[str, str]] |
| 170 | ) -> List[Dict[str, str]]: |
| 171 | unrolled_repo: Dict[str, str] = {} |
| 172 | for item in repo or []: |
| 173 | unrolled_repo[item["key"]] = item["value"] |
| 174 | for item in ref or []: |
| 175 | if item["key"] in unrolled_repo: |
| 176 | continue |
| 177 | unrolled_repo[item["key"]] = item["value"] |
| 178 | |
| 179 | return [ |
| 180 | {"key": k, "value": v} for k, v in sorted(unrolled_repo.items()) |
| 181 | ] |
| 182 | |
| 183 | |
| 184 | def load_config(path: str) -> Tuple[str, Dict[str, Any]]: |
| 185 | if "clang-tidy" not in path: |
| 186 | path = os.path.join(path, ".clang-tidy") |
| 187 | |
| 188 | if not os.path.exists(path): |
| 189 | return (path, {}) |
| 190 | |
| 191 | with open(path, "r") as f: |
| 192 | return (path, yaml.safe_load(f)) |
| 193 | |
| 194 | |
| 195 | def format_yaml_output(data: Dict[str, Any]) -> str: |
| 196 | """Convert to a prettier YAML string: |
| 197 | - filter out excess empty lines |
| 198 | - insert new lines between keys |
| 199 | """ |
| 200 | yaml_string = yaml.dump(data, sort_keys=False, indent=4) |
| 201 | lines: List[str] = [] |
| 202 | for line in yaml_string.split("\n"): |
| 203 | # Strip excess new lines. |
| 204 | if not line: |
| 205 | continue |
| 206 | # Add new line between keys. |
| 207 | if len(lines) and re.match("[a-zA-Z0-9]+:", line): |
| 208 | lines.append("") |
| 209 | lines.append(line) |
| 210 | lines.append("") |
| 211 | |
| 212 | return "\n".join(lines) |
| 213 | |
| 214 | |
| 215 | if __name__ == "__main__": |
| 216 | main() |