Files
computer/.github/scripts/get_pyproject_version.py
r33drichards 590c4a8753 Add pyproject.toml version verification script and tests (#462)
* Add pyproject.toml version verification script and tests

Adds get_pyproject_version.py script to verify that pyproject.toml
versions match expected versions during git tag releases. Includes
comprehensive pytest test suite with best practices.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Revert "Add pyproject.toml version verification script and tests"

This reverts commit 1d40e692cc.

* Add pyproject.toml version verification script and tests

Adds get_pyproject_version.py script to verify that pyproject.toml
versions match expected versions during git tag releases. Includes
comprehensive pytest test suite with best practices.

Updates the GitHub Actions workflow to use the verification script,
ensuring version consistency before publishing packages. Also removes
the old version-setting step as pyproject.toml is now the source of
truth for versions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* f

* add test for validation script to gha

---------

Co-authored-by: Your Name <you@example.com>
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-10 14:43:07 -07:00

69 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Verifies that the version in pyproject.toml matches the expected version.
Usage:
python get_pyproject_version.py <pyproject_path> <expected_version>
Exit codes:
0 - Versions match
1 - Versions don't match or error occurred
"""
import sys
try:
import tomllib
except ImportError:
# Fallback for Python < 3.11
import toml as tomllib
def main():
if len(sys.argv) != 3:
print("Usage: python get_pyproject_version.py <pyproject_path> <expected_version>", file=sys.stderr)
sys.exit(1)
pyproject_path = sys.argv[1]
expected_version = sys.argv[2]
# tomllib requires binary mode
try:
with open(pyproject_path, 'rb') as f:
data = tomllib.load(f)
except FileNotFoundError:
print(f"❌ ERROR: File not found: {pyproject_path}", file=sys.stderr)
sys.exit(1)
except Exception as e:
# Fallback to toml if using the old library or handle other errors
try:
import toml
data = toml.load(pyproject_path)
except FileNotFoundError:
print(f"❌ ERROR: File not found: {pyproject_path}", file=sys.stderr)
sys.exit(1)
except Exception as toml_err:
print(f"❌ ERROR: Failed to parse TOML file: {e}", file=sys.stderr)
sys.exit(1)
actual_version = data.get('project', {}).get('version')
if not actual_version:
print("❌ ERROR: No version found in pyproject.toml", file=sys.stderr)
sys.exit(1)
if actual_version != expected_version:
print("❌ Version mismatch detected!", file=sys.stderr)
print(f" pyproject.toml version: {actual_version}", file=sys.stderr)
print(f" Expected version: {expected_version}", file=sys.stderr)
print("", file=sys.stderr)
print("The version in pyproject.toml must match the version being published.", file=sys.stderr)
print(f"Please update pyproject.toml to version {expected_version} or use the correct tag.", file=sys.stderr)
sys.exit(1)
print(f"✅ Version consistency check passed: {actual_version}")
sys.exit(0)
if __name__ == '__main__':
main()