Files
Kamino/scripts/dev-setup.sh
T
Defame1297 abde765472
Build / build (pull_request) Successful in 22s
Lint / lint (pull_request) Successful in 1m13s
Molecule / molecule (pull_request) Successful in 13s
docs(agents): make issue-tracker.md tool-neutral and name tea as the supported tool
issue-tracker.md becomes Conventions (operations as Gitea concepts) and a
Tool section with one tea command per operation, tea api for blocked_by
dependencies. It names no skill or MCP tool, and drops the maintainer-only
token and account-lookup lines. setup.md adds The tracker: install tea
0.14.2 or later and log in with a token of your own account.
dev-setup.sh warns when tea is missing or older; it installs nothing.

Closes #267

Assisted-by: Claude Code
2026-11-24 21:43:11 +00:00

183 lines
8.9 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
# tea is the supported tracker client (docs/agents/issue-tracker.md); only warn, the hooks do not need it.
tea_min=0.14.2
tea_see="see docs/contributing/setup.md (The tracker)"
if ! command -v tea >/dev/null 2>&1; then
echo "warning: tea $tea_min or later is not on PATH; $tea_see." >&2
else
# The first line reads `Version: 0.16.0<TAB>golang: ...`, the number possibly in ANSI bold.
tea_version="$(tea --version 2>/dev/null | tr -d '\033' \
| sed -nE '1s/^Version: (\[[0-9;]*m)?([0-9]+\.[0-9]+\.[0-9]+).*/\2/p' || true)"
if [ -z "$tea_version" ]; then
echo "warning: could not read the version of tea (need $tea_min or later); $tea_see." >&2
else
IFS=. read -r t_major t_minor t_patch <<< "$tea_version"
IFS=. read -r m_major m_minor m_patch <<< "$tea_min"
if [ "$t_major" -lt "$m_major" ] || { [ "$t_major" -eq "$m_major" ] && {
[ "$t_minor" -lt "$m_minor" ] || { [ "$t_minor" -eq "$m_minor" ] && [ "$t_patch" -lt "$m_patch" ]; }; }; }; then
echo "warning: tea $tea_version is older than $tea_min; $tea_see." >&2
fi
fi
fi
echo "Done: git commit now runs the hooks with the tools in $root/.venv/bin and the collections in $collections."