|
| 1 | +"""Check version consistency between `pyproject.toml` and `_version.py`. |
| 2 | +
|
| 3 | +This script validates that the version defined in pyproject.toml matches the |
| 4 | +`__version__` variable in `langchain_anthropic/_version.py`. Intended for use as a |
| 5 | +pre-commit hook to prevent version mismatches. |
| 6 | +""" |
| 7 | + |
| 8 | +import re |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +def get_pyproject_version(pyproject_path: Path) -> str | None: |
| 14 | + """Extract version from `pyproject.toml`.""" |
| 15 | + content = pyproject_path.read_text(encoding="utf-8") |
| 16 | + match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE) |
| 17 | + return match.group(1) if match else None |
| 18 | + |
| 19 | + |
| 20 | +def get_version_py_version(version_path: Path) -> str | None: |
| 21 | + """Extract `__version__` from `_version.py`.""" |
| 22 | + content = version_path.read_text(encoding="utf-8") |
| 23 | + match = re.search(r'^__version__\s*=\s*"([^"]+)"', content, re.MULTILINE) |
| 24 | + return match.group(1) if match else None |
| 25 | + |
| 26 | + |
| 27 | +def main() -> int: |
| 28 | + """Validate version consistency.""" |
| 29 | + script_dir = Path(__file__).parent |
| 30 | + package_dir = script_dir.parent |
| 31 | + |
| 32 | + pyproject_path = package_dir / "pyproject.toml" |
| 33 | + version_path = package_dir / "langchain_anthropic" / "_version.py" |
| 34 | + |
| 35 | + if not pyproject_path.exists(): |
| 36 | + print(f"Error: {pyproject_path} not found") # noqa: T201 |
| 37 | + return 1 |
| 38 | + |
| 39 | + if not version_path.exists(): |
| 40 | + print(f"Error: {version_path} not found") # noqa: T201 |
| 41 | + return 1 |
| 42 | + |
| 43 | + pyproject_version = get_pyproject_version(pyproject_path) |
| 44 | + version_py_version = get_version_py_version(version_path) |
| 45 | + |
| 46 | + if pyproject_version is None: |
| 47 | + print("Error: Could not find version in pyproject.toml") # noqa: T201 |
| 48 | + return 1 |
| 49 | + |
| 50 | + if version_py_version is None: |
| 51 | + print("Error: Could not find __version__ in langchain_anthropic/_version.py") # noqa: T201 |
| 52 | + return 1 |
| 53 | + |
| 54 | + if pyproject_version != version_py_version: |
| 55 | + print("Error: Version mismatch detected!") # noqa: T201 |
| 56 | + print(f" pyproject.toml: {pyproject_version}") # noqa: T201 |
| 57 | + print(f" langchain_anthropic/_version.py: {version_py_version}") # noqa: T201 |
| 58 | + return 1 |
| 59 | + |
| 60 | + print(f"Version check passed: {pyproject_version}") # noqa: T201 |
| 61 | + return 0 |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + sys.exit(main()) |
0 commit comments