|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# |
| 4 | +# This source code is licensed under the MIT license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import yaml |
| 11 | + |
| 12 | + |
| 13 | +def parse_requirements(filepath): |
| 14 | + """Parse requirements file and return a dict of package versions.""" |
| 15 | + versions = {} |
| 16 | + with open(filepath) as f: |
| 17 | + for line in f: |
| 18 | + line = line.strip() |
| 19 | + if line and not line.startswith("#"): |
| 20 | + # Handle different requirement formats |
| 21 | + if "==" in line: |
| 22 | + pkg, version = line.split("==") |
| 23 | + versions[pkg.strip().lower()] = version.strip() |
| 24 | + return versions |
| 25 | + |
| 26 | + |
| 27 | +def parse_precommit_config(filepath): |
| 28 | + """Parse pre-commit config and extract ufmt repo rev and hook dependencies.""" |
| 29 | + with open(filepath) as f: |
| 30 | + config = yaml.safe_load(f) |
| 31 | + |
| 32 | + versions = {} |
| 33 | + for repo in config["repos"]: |
| 34 | + if "https://github.com/omnilib/ufmt" in repo.get("repo", ""): |
| 35 | + # Get ufmt version from rev - assumes fixed format: vX.Y.Z |
| 36 | + versions["ufmt"] = repo.get("rev", "").replace("v", "") |
| 37 | + |
| 38 | + # Get dependency versions |
| 39 | + for hook in repo["hooks"]: |
| 40 | + if hook["id"] == "ufmt": |
| 41 | + for dep in hook.get("additional_dependencies", []): |
| 42 | + if "==" in dep: |
| 43 | + pkg, version = dep.split("==") |
| 44 | + versions[pkg.strip().lower()] = version.strip() |
| 45 | + break |
| 46 | + return versions |
| 47 | + |
| 48 | + |
| 49 | +def main(): |
| 50 | + # Find the pre-commit config and requirements files |
| 51 | + config_file = Path(".pre-commit-config.yaml") |
| 52 | + requirements_file = Path("requirements-fmt.txt") |
| 53 | + |
| 54 | + if not config_file.exists(): |
| 55 | + print(f"Error: Could not find {config_file}") |
| 56 | + sys.exit(1) |
| 57 | + |
| 58 | + if not requirements_file.exists(): |
| 59 | + print(f"Error: Could not find {requirements_file}") |
| 60 | + sys.exit(1) |
| 61 | + |
| 62 | + # Parse both files |
| 63 | + req_versions = parse_requirements(requirements_file) |
| 64 | + config_versions = parse_precommit_config(config_file) |
| 65 | + |
| 66 | + # Check versions |
| 67 | + mismatches = [] |
| 68 | + for pkg, req_ver in req_versions.items(): |
| 69 | + req_ver = req_versions.get(pkg, None) |
| 70 | + config_ver = config_versions.get(pkg, None) |
| 71 | + |
| 72 | + if req_ver != config_ver: |
| 73 | + found_version_str = f"{pkg}: {requirements_file} has {req_ver}," |
| 74 | + if pkg == "ufmt": |
| 75 | + mismatches.append( |
| 76 | + f"{found_version_str} pre-commit config rev has v{config_ver}" |
| 77 | + ) |
| 78 | + else: |
| 79 | + mismatches.append( |
| 80 | + f"{found_version_str} pre-commit config has {config_ver}" |
| 81 | + ) |
| 82 | + |
| 83 | + # Report results |
| 84 | + if mismatches: |
| 85 | + msg_str = "".join("\n\t" + msg for msg in mismatches) |
| 86 | + print( |
| 87 | + f"Version mismatches found:{msg_str}" |
| 88 | + "\nPlease update the versions in `.pre-commit-config.yaml` to be " |
| 89 | + "consistent with those in `requirements-fmt.txt` (source of truth)." |
| 90 | + "\nNote: all versions must be pinned exactly ('==X.Y.Z') in both files." |
| 91 | + ) |
| 92 | + sys.exit(1) |
| 93 | + else: |
| 94 | + print("All versions match!") |
| 95 | + sys.exit(0) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments