From 018f62fed058d719dfda80589de26dd2ceb21028 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Fri, 25 Jul 2025 12:49:49 -0300 Subject: [PATCH] [Pre Commit][Added] Version check To overcome comfy-cli limitations --- .pre-commit-config.yaml | 17 ++++++++- tools/check_versions.py | 78 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100755 tools/check_versions.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 621d23a..bc412c0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -51,7 +51,7 @@ repos: # The command to execute. It's expected to be found in the PATH # of the environment where you run `git commit`. entry: mypy - args: ["--explicit-package-bases", "source/"] + args: ["--explicit-package-bases", "src/"] # 'system' tells pre-commit to find the command in the current environment # instead of building an isolated one. language: system @@ -64,3 +64,18 @@ repos: # whole project as configured in mypy.ini, rather than just the changed files. # Try without it first. # pass_filenames: false + +# --- Version checking --- +- repo: local + hooks: + - id: version-check + name: check for version consistency + # The command to execute. It's a Python script. + entry: python tools/check_versions.py + # Use 'system' to run it with the current environment's Python + language: system + # This hook doesn't need to run on every file. + # It should run if either of the version files change. + # This makes it very fast. + files: ^(pyproject\.toml|src/nodes/__init__\.py)$ + # The regex `^...$` ensures it matches the full path from the repo root. diff --git a/tools/check_versions.py b/tools/check_versions.py new file mode 100755 index 0000000..c955a62 --- /dev/null +++ b/tools/check_versions.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +import re +import sys +from pathlib import Path + +# --- Configuration --- +# The path to your pyproject.toml file, relative to the project root +PYPROJECT_PATH = Path("pyproject.toml") +# The path to the Python file containing the __version__ string +SOURCE_VERSION_PATH = Path("src/nodes/__init__.py") +# --- End Configuration --- + +def get_version_from_pyproject(file_path: Path) -> str | None: + """Extracts the version string from a pyproject.toml file.""" + try: + content = file_path.read_text() + # A simple regex to find `version = "..."` under the `[project]` table + match = re.search(r'^version\s*=\s*"(.*?)"', content, re.MULTILINE) + if match: + return match.group(1) + except FileNotFoundError: + print(f"Error: {file_path} not found.", file=sys.stderr) + except Exception as e: + print(f"Error reading or parsing {file_path}: {e}", file=sys.stderr) + return None + + +def get_version_from_source(file_path: Path) -> str | None: + """Extracts the __version__ string from a Python source file.""" + try: + content = file_path.read_text() + # A simple regex to find `__version__ = "..."` + match = re.search(r'^__version__\s*=\s*"(.*?)"', content, re.MULTILINE) + if match: + return match.group(1) + except FileNotFoundError: + print(f"Error: {file_path} not found.", file=sys.stderr) + except Exception as e: + print(f"Error reading or parsing {file_path}: {e}", file=sys.stderr) + return None + + +def main() -> int: + """ + Compares version strings from pyproject.toml and the source code. + Exits with a non-zero status code if they do not match. + """ + print("--- Checking version consistency ---") + + # Get versions + pyproject_version = get_version_from_pyproject(PYPROJECT_PATH) + source_version = get_version_from_source(SOURCE_VERSION_PATH) + + # Validate that we found both + if not pyproject_version: + print(f"Error: Could not find version in {PYPROJECT_PATH}", file=sys.stderr) + return 1 + + if not source_version: + print(f"Error: Could not find `__version__` in {SOURCE_VERSION_PATH}", file=sys.stderr) + return 1 + + print(f"Version in {PYPROJECT_PATH}: {pyproject_version}") + print(f"Version in {SOURCE_VERSION_PATH}: {source_version}") + + # Compare and exit + if pyproject_version == source_version: + print("āœ… Versions are consistent.") + return 0 + else: + print("\nāŒ Error: Version mismatch!", file=sys.stderr) + print(f" pyproject.toml has version '{pyproject_version}'", file=sys.stderr) + print(f" {SOURCE_VERSION_PATH} has version '{source_version}'", file=sys.stderr) + print(" Please ensure both versions are identical.", file=sys.stderr) + return 1 + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file