Skip to content

Commit

Permalink
fea: write script to check against requirements-fmt.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
CompRhys committed Nov 18, 2024
1 parent 93574c3 commit 752e93a
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 3 deletions.
18 changes: 15 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
repos:
- repo: local
hooks:
- id: check-requirements-versions
name: Check requirements.txt versions
entry: python scripts/check_requirements.py
language: python
always_run: true
pass_filenames: false
additional_dependencies:
- packaging
- PyYAML

- repo: https://github.com/omnilib/ufmt
rev: v2.3.0
rev: v2.8.0
hooks:
- id: ufmt
additional_dependencies:
- black>=24.1.1
- usort>=1.0.7
- black==24.4.2
- usort==1.0.8.post1
args: [format]

- repo: https://github.com/pycqa/flake8
Expand Down
110 changes: 110 additions & 0 deletions scripts/check_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/usr/bin/env python3

import sys
from pathlib import Path

import yaml
from packaging.version import parse as parse_version


def parse_requirements(filepath):
"""Parse requirements file and return a dict of package versions."""
versions = {}
with open(filepath) as f:
for line in f:
line = line.strip()
if line and not line.startswith("#"):
# Handle different requirement formats
if ">=" in line:
pkg, version = line.split(">=")
elif "==" in line:
pkg, version = line.split("==")
else:
continue
versions[pkg.strip().lower()] = version.strip()
return versions


def parse_precommit_config(filepath):
"""Parse pre-commit config and extract ufmt repo rev and hook dependencies."""
with open(filepath) as f:
config = yaml.safe_load(f)

versions = {}
for repo in config["repos"]:
if "https://github.com/omnilib/ufmt" in repo.get("repo", ""):
# Get ufmt version from rev
rev = repo.get("rev", "")
if rev.startswith("v"):
versions["ufmt"] = rev[1:] # Remove 'v' prefix

# Get dependency versions
for hook in repo["hooks"]:
if hook["id"] == "ufmt":
for dep in hook.get("additional_dependencies", []):
if "==" in dep:
pkg, version = dep.split("==")
versions[pkg.strip().lower()] = version.strip()
break
return versions


def main():
# Find the pre-commit config and requirements files
config_file = Path(".pre-commit-config.yaml")
requirements_file = Path("requirements-fmt.txt")

if not config_file.exists():
print(f"Error: Could not find {config_file}")
sys.exit(1)

if not requirements_file.exists():
print(f"Error: Could not find {requirements_file}")
sys.exit(1)

# Parse both files
req_versions = parse_requirements(requirements_file)
config_versions = parse_precommit_config(config_file)

# Packages to check
packages = ["ufmt", "black", "usort"]

# Check versions
mismatches = []
for pkg in packages:
req_ver = req_versions.get(pkg)
config_ver = config_versions.get(pkg)

if not req_ver:
print(f"Warning: {pkg} not found in {requirements_file}")
continue

if not config_ver:
print(f"Warning: {pkg} not found in pre-commit config")
continue

if parse_version(req_ver) != parse_version(config_ver):
if pkg == "ufmt":
mismatches.append(
f"{pkg}: {requirements_file} has {req_ver}, "
f"pre-commit config rev has v{config_ver}"
)
else:
mismatches.append(
f"{pkg}: {requirements_file} has {req_ver}, "
f"pre-commit config has {config_ver}"
)

# Report results
if mismatches:
print("Version mismatches found:")
for msg in mismatches:
print(f" {msg}")
sys.exit(1)
else:
print("All versions match!")
sys.exit(0)


if __name__ == "__main__":
main()

0 comments on commit 752e93a

Please sign in to comment.