#!/usr/bin/env bash # Sets up a local checkout for development: a virtualenv with the pinned tools # (requirements-dev.txt, the versions CI runs) and git hooks that find them. # # scripts/dev-setup.sh # KAMINO_PYTHON=python3.13 scripts/dev-setup.sh to pick one # # KAMINO_PYTHON (default python3) selects the interpreter that is checked and that creates # a new virtualenv. An existing virtualenv is kept when it already runs Python 3.13+, even # if it was built by another interpreter; only an older one is rebuilt. # # The repository is the one this script lives in, whatever the caller's working directory. # # Requires GNU readlink (-f and -m): on macOS that is 12.3+ for -f, or coreutils (greadlink # on PATH as readlink) for -m. # # Some hooks of .pre-commit-config.yaml are `language: system` (REUSE, changelog lint, # ansible-lint): they run their tools by name, so the tools must be on PATH when git # runs the hook. `pre-commit install` does not arrange that, so this script puts the # virtualenv first on PATH inside the installed hook scripts. It is idempotent; run it # again after changing requirements-dev.txt or the hook types. set -euo pipefail # ansible-core 2.22 (requirements-dev.txt) needs Python 3.13+. Renovate raises that pin, so # when a bump of it changes the minimum Python, raise py_ok and the message below in the # same pull request. The ci image has python3.13 next to the distribution's; # without a system 3.13, `uv python install 3.13` provides one. python="${KAMINO_PYTHON:-python3}" py_ok='import sys; sys.exit(0 if sys.version_info >= (3, 13) else 1)' if ! "$python" -c "$py_ok" 2>/dev/null; then echo "error: $python is missing or older than 3.13 (ansible-core 2.22 needs 3.13+); set KAMINO_PYTHON=/path/to/python3.13 (uv python install 3.13)" >&2 exit 1 fi # The repository is derived from this script's own location (symlinks resolved), never # from the caller's working directory: running it from another repository must not # create or remove that repository's .venv. script="$(readlink -f "${BASH_SOURCE[0]}")" root="$(git -C "$(dirname "$script")/.." rev-parse --show-toplevel)" cd "$root" # `pre-commit install` refuses to run when core.hooksPath is set, and git would not run # .git/hooks anyway. A repo-local setting whose values all resolve to this repository's own # hooks directory (or are empty) is harmless: it is lifted for the install and restored # afterwards, byte for byte. Anything else (another directory, or a value outside this # repository's config, such as a global one) fails up front. # Fills the global array cur_paths with every repo-local core.hooksPath value, read # NUL-delimited so a value with a newline survives and an empty value is kept as an # element. (No mapfile: it needs bash 4; the loop also runs on the 3.2 of macOS, untested.) cur_paths=() read_local_paths() { local v cur_paths=() while IFS= read -r -d '' v; do cur_paths+=("$v") done < <(git -C "$root" config --local -z --get-all core.hooksPath 2>/dev/null || true) } local_paths=() restore_hooks_path() { # Idempotent and harmless when the unset below failed: it only rewrites the setting when # the local values differ from the saved ones. Never fails (it runs from the EXIT trap). [ "${#local_paths[@]}" -gt 0 ] || return 0 local i v same=1 read_local_paths if [ "${#cur_paths[@]}" -ne "${#local_paths[@]}" ]; then same=0 else i=0 while [ "$i" -lt "${#local_paths[@]}" ]; do [ "${cur_paths[$i]}" = "${local_paths[$i]}" ] || same=0 i=$((i + 1)) done fi [ "$same" -eq 1 ] && return 0 git -C "$root" config --local --unset-all core.hooksPath 2>/dev/null || true for v in "${local_paths[@]}"; do git -C "$root" config --local --add core.hooksPath "$v" || true done return 0 } read_local_paths if [ "${#cur_paths[@]}" -gt 0 ]; then own_hooks="$(readlink -f "$(git -C "$root" rev-parse --path-format=absolute --git-common-dir)/hooks")" for v in "${cur_paths[@]}"; do [ -n "$v" ] || continue case "$v" in /*) resolved="$(readlink -m "$v")" ;; *) resolved="$(readlink -m "$root/$v")" ;; esac if [ "$resolved" != "$own_hooks" ]; then echo "error: core.hooksPath is set to '$v', so git does not run the hooks pre-commit installs." >&2 echo " Unset it for this clone (git config --unset-all core.hooksPath)" >&2 echo " or manage the hooks in that directory yourself, then re-run this script." >&2 exit 1 fi done # Save every local value (the setting may be multi-valued, or empty), and install the # traps BEFORE changing anything, so an interruption between the two cannot lose it. local_paths=("${cur_paths[@]}") trap restore_hooks_path EXIT trap 'exit 130' INT trap 'exit 143' TERM git -C "$root" config --local --unset-all core.hooksPath fi if still="$(git -C "$root" config --get core.hooksPath)" && [ -n "$still" ]; then echo "error: core.hooksPath is still set to '$still' (outside this clone's own config: a global or" >&2 echo " system value), so 'pre-commit install' would refuse to run." >&2 echo " Unset that setting (git config --global --unset-all core.hooksPath), then re-run." >&2 exit 1 fi # A virtualenv made by an older Python is rebuilt rather than reused. if [ -x .venv/bin/python ] && ! .venv/bin/python -c "$py_ok" 2>/dev/null; then rm -rf "$root/.venv" fi [ -x .venv/bin/python ] || "$python" -m venv .venv .venv/bin/pip install --quiet --disable-pip-version-check -r requirements-dev.txt .venv/bin/pre-commit install --install-hooks marker="# kamino-dev-setup: virtualenv first on PATH" # default_install_hook_types in .pre-commit-config.yaml names these; keep in step. for hook in pre-commit commit-msg; do file="$(git -C "$root" rev-parse --path-format=absolute --git-path "hooks/$hook")" if [ ! -f "$file" ]; then echo "error: $file was not installed by 'pre-commit install'; check default_install_hook_types in .pre-commit-config.yaml." >&2 exit 1 fi if ! grep -qxF "$marker" "$file"; then tmp="$(mktemp)" { head -n 1 "$file" # %q shell-quotes the path: a repository path with spaces, quotes or $(...) is data. printf '%s\n' "$marker" "export PATH=$(printf '%q' "$root/.venv/bin"):\"\$PATH\"" tail -n +2 "$file" } > "$tmp" cat "$tmp" > "$file" # keeps the file mode of the hook rm -f "$tmp" fi done echo "Done: git commit now runs the hooks with the tools in $root/.venv/bin."