Files
Defame1297andClaude Sonnet 5.5 b0607a39e2
Build / build (pull_request) Successful in 14s
Molecule / molecule (pull_request) Canceled after 0s
Lint / lint (pull_request) Canceled after 56s
ci(python): one Python (3.14) for the lock, a clone and the ci image
.python-version (3.14) is the source. dev-setup.sh and lock-requirements.sh
require exactly that minor; the ci image builds /opt/venv from its baked
python-build-standalone of that version (ARG PYTHON_VERSION) instead of the
distribution's python3, and derives the python<MINOR> links for
ansible-test from it; `check_image_versions.py pins` fails when
.python-version, the lock header or an `ARG PYTHON_VERSION` differs.
The lock already carries the 3.14 header (Renovate's -full container runs
pip-compile on its own python 3.14.8); renovate.yml now says that python's
minor version must equal .python-version. ci image 0.2.25 -> 0.2.26.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
2026-11-16 07:42:45 +00:00

162 lines
7.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Sets up a local checkout for development: a virtualenv with the pinned tools
# (requirements-dev.lock, the hashed lock of requirements-dev.txt: the versions CI runs), the pinned collections the roles call
# (requirements.yml) and git hooks that find the tools.
#
# scripts/dev-setup.sh # KAMINO_PYTHON=/path/to/python3.13 scripts/dev-setup.sh to pick one
#
# The interpreter must be the Python of .python-version (python<MINOR> by default, KAMINO_PYTHON selects
# another path); it creates a new virtualenv. An existing virtualenv is kept when it runs that Python,
# even if another interpreter built it; one on a different Python 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 also installs the collections
# of requirements.yml into .ansible/collections of this clone: scripts/ansible-lint.sh (the
# ansible-lint hook) resolves modules from there, so local lint sees the collection versions CI
# does (the ci image bakes requirements.yml) and not the control host's ~/.ansible. It is
# idempotent; run it again after changing requirements-dev.txt or its lock, requirements.yml or the hook types.
set -euo pipefail
# 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"
# The Python of the virtualenv is the one in .python-version (a minor version, e.g. 3.13): the one the
# lock was resolved on and the ci image's /opt/venv runs on (check_image_versions.py pins keeps the lock
# header and the image's Dockerfile equal to it). ansible-core 2.22 needs 3.13+; when a Renovate bump
# raises that minimum, .python-version, the ci image's PYTHON_VERSION and the lock move in one pull request.
[ -f .python-version ] || { echo "error: .python-version is missing (the Python of the lock, a clone and the ci image)" >&2; exit 1; }
pin="$(tr -d '[:space:]' < .python-version)"
case "$pin" in
[0-9].[0-9]|[0-9].[0-9][0-9]) ;;
*) echo "error: .python-version must hold one MAJOR.MINOR version, found '$pin'" >&2; exit 1 ;;
esac
python="${KAMINO_PYTHON:-python$pin}"
py_ok="import sys; sys.exit(0 if '%d.%d' % sys.version_info[:2] == '$pin' else 1)"
if ! "$python" -c "$py_ok" 2>/dev/null; then
echo "error: $python is missing or is not Python $pin (.python-version); set KAMINO_PYTHON=/path/to/python$pin (uv python install $pin)" >&2
exit 1
fi
# `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 --require-hashes -r requirements-dev.lock -r requirements-dev.txt
.venv/bin/pre-commit install --install-hooks
# The collections of requirements.yml, exactly as pinned, into this clone (.ansible/ is ignored by
# git and by the Collection build). ANSIBLE_COLLECTIONS_PATH is set to the target for the install:
# a copy found elsewhere (~/.ansible/collections) would be skipped as already installed.
collections="$root/.ansible/collections"
mkdir -p "$collections"
ANSIBLE_COLLECTIONS_PATH="$collections" .venv/bin/ansible-galaxy collection install \
-r requirements.yml -p "$collections"
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 pre-push; 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 and the collections in $collections."