ROOT_DIR=$(git rev-parse --show-toplevel)
cd "$ROOT_DIR"
if [ -d "$HOME/.cargo/bin" ] && ! command -v cargo >/dev/null 2>&1; then
export PATH="$HOME/.cargo/bin:$PATH"
echo "Added $HOME/.cargo/bin to PATH"
fi
PYTHON_PATH=""
if [ -f "$ROOT_DIR/.venv/bin/python" ]; then
PYTHON_PATH="$ROOT_DIR/.venv/bin/python"
elif [ -f "$ROOT_DIR/.venv/bin/python3" ]; then
PYTHON_PATH="$ROOT_DIR/.venv/bin/python3"
elif command -v python3 >/dev/null 2>&1; then
PYTHON_PATH=$(command -v python3)
elif command -v python >/dev/null 2>&1; then
PYTHON_PATH=$(command -v python)
fi
if [ -z "$PYTHON_PATH" ]; then
echo "Error: Python not found. Please install Python or activate your Python environment."
echo "Commit aborted."
exit 1
fi
echo "Using Python at: $PYTHON_PATH"
if command -v poetry >/dev/null 2>&1; then
if [ ! -d "$ROOT_DIR/.venv" ]; then
echo "Creating Poetry environment..."
poetry install --no-interaction
fi
echo "Running black formatting checks on ALL files..."
poetry run black --check nomy_data_models tests
BLACK_RESULT=$?
if [ $BLACK_RESULT -ne 0 ]; then
echo "Error: Black formatting check failed. Automatically reformatting ALL files..."
poetry run black nomy_data_models tests
poetry run black --check nomy_data_models tests
if [ $? -ne 0 ]; then
echo "Error: black failed to fix all formatting issues. Commit aborted."
exit 1
fi
echo "Adding black-reformatted files to the staging area..."
git add $(git diff --name-only | grep "\.py$")
fi
echo "Checking ALL files for import sorting issues..."
poetry run isort --profile black --check nomy_data_models tests
ISORT_RESULT=$?
if [ $ISORT_RESULT -ne 0 ]; then
echo "Error: isort check failed. Automatically reformatting imports in ALL files..."
poetry run isort --profile black nomy_data_models tests
poetry run isort --profile black --check nomy_data_models tests
if [ $? -ne 0 ]; then
echo "Error: isort failed to fix all import issues. Commit aborted."
exit 1
fi
echo "Adding isort-reformatted files to the staging area..."
git add $(git diff --name-only | grep "\.py$")
fi
echo "Specifically checking test_py_to_rust.py for import issues..."
poetry run isort --profile black --check tests/python/test_py_to_rust.py
if [ $? -ne 0 ]; then
echo "Fixing import issues in test_py_to_rust.py..."
poetry run isort --profile black tests/python/test_py_to_rust.py
poetry run isort --profile black --check tests/python/test_py_to_rust.py
if [ $? -ne 0 ]; then
echo "Error: Failed to fix import issues in test_py_to_rust.py. Commit aborted."
exit 1
fi
echo "Adding reformatted test_py_to_rust.py to the staging area..."
git add tests/python/test_py_to_rust.py
fi
echo "Running Python tests..."
PYTHONPATH="$ROOT_DIR" poetry run python -m pytest tests/python
TEST_RESULT=$?
if [ $TEST_RESULT -ne 0 ]; then
echo "Error: Python tests failed. Commit aborted."
exit 1
fi
echo "Running pre-commit hook to generate and verify Rust models using Poetry..."
poetry run "$PYTHON_PATH" "$ROOT_DIR/scripts/generate_rust.py" --verify
RESULT=$?
else
echo "Running black formatting checks on ALL files using system Python..."
"$PYTHON_PATH" -m black --check nomy_data_models tests
BLACK_RESULT=$?
if [ $BLACK_RESULT -ne 0 ]; then
echo "Error: Black formatting check failed. Automatically reformatting ALL files..."
"$PYTHON_PATH" -m black nomy_data_models tests
"$PYTHON_PATH" -m black --check nomy_data_models tests
if [ $? -ne 0 ]; then
echo "Error: black failed to fix all formatting issues. Commit aborted."
exit 1
fi
echo "Adding black-reformatted files to the staging area..."
git add $(git diff --name-only | grep "\.py$")
fi
echo "Checking ALL files for import sorting issues using system Python..."
"$PYTHON_PATH" -m isort --profile black --check nomy_data_models tests
ISORT_RESULT=$?
if [ $ISORT_RESULT -ne 0 ]; then
echo "Error: isort check failed. Automatically reformatting imports in ALL files..."
"$PYTHON_PATH" -m isort --profile black nomy_data_models tests
"$PYTHON_PATH" -m isort --profile black --check nomy_data_models tests
if [ $? -ne 0 ]; then
echo "Error: isort failed to fix all import issues. Commit aborted."
exit 1
fi
echo "Adding isort-reformatted files to the staging area..."
git add $(git diff --name-only | grep "\.py$")
fi
echo "Specifically checking test_py_to_rust.py for import issues using system Python..."
"$PYTHON_PATH" -m isort --profile black --check tests/python/test_py_to_rust.py
if [ $? -ne 0 ]; then
echo "Fixing import issues in test_py_to_rust.py..."
"$PYTHON_PATH" -m isort --profile black tests/python/test_py_to_rust.py
"$PYTHON_PATH" -m isort --profile black --check tests/python/test_py_to_rust.py
if [ $? -ne 0 ]; then
echo "Error: Failed to fix import issues in test_py_to_rust.py. Commit aborted."
exit 1
fi
echo "Adding reformatted test_py_to_rust.py to the staging area..."
git add tests/python/test_py_to_rust.py
fi
echo "Running Python tests using system Python..."
PYTHONPATH="$ROOT_DIR" "$PYTHON_PATH" -m pytest tests/python
TEST_RESULT=$?
if [ $TEST_RESULT -ne 0 ]; then
echo "Error: Python tests failed. Commit aborted."
exit 1
fi
echo "Running pre-commit hook to generate and verify Rust models using system Python..."
"$PYTHON_PATH" "$ROOT_DIR/scripts/generate_rust.py" --verify
RESULT=$?
fi
if [ $RESULT -ne 0 ]; then
echo "Error: Failed to generate or verify Rust models. Commit aborted."
echo "If Cargo is not found, please install Rust and Cargo from https://rustup.rs/"
echo "After installing, make sure it's in your PATH by running 'source $HOME/.cargo/env' or restarting your terminal."
exit 1
fi
if command -v cargo >/dev/null 2>&1; then
echo "Running Rust formatting checks on ALL files..."
cargo fmt -- --check
FMT_RESULT=$?
if [ $FMT_RESULT -ne 0 ]; then
echo "Error: Rust formatting check failed. Automatically reformatting ALL Rust code..."
cargo fmt
echo "Adding rust-fmt reformatted files to the staging area..."
git add $(git diff --name-only | grep "\.rs$")
fi
echo "Running Rust linting with Clippy on ALL files..."
cargo clippy -- \
-A clippy::too_many_arguments \
-A unused_imports \
-A non_camel_case_types
CLIPPY_RESULT=$?
if [ $CLIPPY_RESULT -ne 0 ]; then
echo "Error: Clippy linting failed. Please fix the warnings before committing."
exit 1
fi
echo "Running cargo fix to address warnings in ALL generated Rust code..."
cargo fix --allow-dirty --allow-staged --lib -p nomy-data-models
if [ $? -ne 0 ]; then
echo "Warning: Failed to run cargo fix. Continuing with commit anyway."
else
echo "Adding files modified by cargo fix to the staging area..."
git add $(git diff --name-only | grep "\.rs$")
fi
else
echo "Warning: Cargo not found. Skipping Rust formatting checks."
echo "To enable Rust formatting and linting, install Rust and Cargo from https://rustup.rs/"
echo "After installing, run 'source $HOME/.cargo/env' or restart your terminal."
fi
if [ -f "$ROOT_DIR/coverage.xml" ]; then
rm -f "$ROOT_DIR/coverage.xml"
fi
if [ -d "$ROOT_DIR/coverage_html" ]; then
rm -rf "$ROOT_DIR/coverage_html"
fi
exit 0