|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# Check the uv settings of the project, to ensure dependencies are being updated properly. |
| 4 | +# |
| 5 | +# The intent here is that: |
| 6 | +# |
| 7 | +# - Lock-files should only be updated via the `make lock-dependencies` target. |
| 8 | +# - We only re-lock when: |
| 9 | +# - Updating the project dependencies in some way. |
| 10 | +# - Refreshing the locked versions, where that is the sole purpose of the PR. |
| 11 | +# - Developer environment information should not be present in the lock-file; it should be usable by the public. |
| 12 | +# |
| 13 | +# The checks that we perform to enforce this are: |
| 14 | +# - The only registry we should see in uv.lock is "https://pypi.org/simple/". |
| 15 | +# - The uv.lock file must be consistent with pyproject.toml. |
| 16 | +# - If changes to uv.lock are present, there must be no other files changed in the PR _or_ pyproject.toml must also |
| 17 | +# be updated. |
| 18 | +# - The same applies to .build-constraints.txt. |
| 19 | +# |
| 20 | + |
| 21 | +set -eu |
| 22 | + |
| 23 | +PROGNAME="${0##*/}" |
| 24 | +die() { |
| 25 | + _exitcode="$1" |
| 26 | + shift |
| 27 | + printf "%s: %s\n" "${PROGNAME}" "$*" 1>&2 |
| 28 | + |
| 29 | + exit "${_exitcode}" |
| 30 | +} |
| 31 | + |
| 32 | +# Locate the root of the project. |
| 33 | +PROJECT_ROOT="$(git rev-parse --show-toplevel)" |
| 34 | + |
| 35 | +# Set up a scratch directory for temporary files during the check. |
| 36 | +SCRATCH="$(mktemp -d -t "${PROGNAME}.XXX")" |
| 37 | +trap 'rm -fr "${SCRATCH}"' EXIT |
| 38 | + |
| 39 | +# This script handles the locked/frozen flags for uv itself. |
| 40 | +unset UV_FROZEN UV_LOCKED |
| 41 | + |
| 42 | +tomlq() { |
| 43 | + # Assumes typical project setup: tomlq is present (and locked). |
| 44 | + uv run --frozen --quiet --group yq tomlq "$@" |
| 45 | +} |
| 46 | + |
| 47 | +# |
| 48 | +# Check 1: Verify that uv.lock refers only to PyPI as a registry. |
| 49 | +# |
| 50 | +check_lock_registry() { |
| 51 | + # Collect all registry URLs present in uv.lock, except for the only one we allow. |
| 52 | + tomlq --raw-output \ |
| 53 | + '[.package[]?.source.registry // empty] - ["https://pypi.org/simple/"] | unique | .[]' \ |
| 54 | + "${PROJECT_ROOT}/uv.lock" > "${SCRATCH}/foreign-registries.txt" |
| 55 | + if [ -s "${SCRATCH}/foreign-registries.txt" ] |
| 56 | + then |
| 57 | + printf "%s: Detected non-PyPI registries:\n" "${PROGNAME}" |
| 58 | + sed 's/^/ /' "${SCRATCH}/foreign-registries.txt" |
| 59 | + die 1 "uv.lock may only contain PyPI registries." |
| 60 | + fi |
| 61 | +} |
| 62 | +check_lock_registry |
| 63 | + |
| 64 | +# |
| 65 | +# Check 2: Verify that uv.lock is consistent with pyproject.toml. |
| 66 | +# |
| 67 | + |
| 68 | +detect_index_url() { |
| 69 | + # Annoyingly, there is no way to query from uv the index URL it's going to use; the only approach is to use a fake |
| 70 | + # project and ask to resolve, then dig the index URL out of the results. |
| 71 | + mkdir -p "${SCRATCH}/uvtest" |
| 72 | + cat > "${SCRATCH}/uvtest/pyproject.toml" << 'EOF' |
| 73 | +[project] |
| 74 | +name = "uvtest" |
| 75 | +version = "0" |
| 76 | +requires-python = ">=3.10" |
| 77 | +dependencies = ["packaging"] |
| 78 | +EOF |
| 79 | + uv lock --quiet --project "${SCRATCH}/uvtest" |
| 80 | + tomlq --raw-output 'first(.package[].source.registry // empty)' "${SCRATCH}/uvtest/uv.lock" |
| 81 | +} |
| 82 | + |
| 83 | +check_lock_consistency() { |
| 84 | + # We can't check uv.lock directly, because we can't reach the registry recorded in there. Instead we make a shadow |
| 85 | + # version of this project and check that. |
| 86 | + index_url="$(detect_index_url)" |
| 87 | + [ -n "${index_url}" ] || die 2 "Could not detect the URL to use for PyPI." |
| 88 | + mkdir -p "${SCRATCH}/shadow-project" |
| 89 | + cp "${PROJECT_ROOT}/pyproject.toml" "${SCRATCH}/shadow-project/" |
| 90 | + # shellcheck disable=SC2016 # $index_url is a jq variable, bound via --arg |
| 91 | + tomlq --toml-output --arg index_url "${index_url}" \ |
| 92 | + '(.package[]?.source | select(.registry? == "https://pypi.org/simple/")).registry = $index_url' \ |
| 93 | + "${PROJECT_ROOT}/uv.lock" > "${SCRATCH}/shadow-project/uv.lock" |
| 94 | + uv lock --project "${SCRATCH}/shadow-project" --check || |
| 95 | + die 2 "Inconsistency detected between pyproject.toml and uv.lock." |
| 96 | +} |
| 97 | +check_lock_consistency |
| 98 | + |
| 99 | +# |
| 100 | +# Check 3: Detect if uv.lock or .build-constraints.txt have been updated with pyproject.toml or on their own. |
| 101 | +# |
| 102 | + |
| 103 | +base_rev() { |
| 104 | + # A few situations to deal with, in order of priority: |
| 105 | + # - In CI, we set BASE_SHA for pull requests and the merge queue. |
| 106 | + # - In CI, we also have the push to main where there's no base: in this case we can use GITHUB_REF and diff will |
| 107 | + # end up a no-op (because there are no differences). |
| 108 | + # - For local dev, fall-back to `main` which by convention is always available. |
| 109 | + _base_rev="${BASE_SHA:-${GITHUB_REF:-main}}" |
| 110 | + git rev-parse --verify --quiet "${_base_rev}" > /dev/null || |
| 111 | + die 3 "Base revision of repository is not available: ${_base_rev}" |
| 112 | + printf "%s" "${_base_rev}" |
| 113 | +} |
| 114 | +merge_base() { |
| 115 | + # Figure out the merge-base between this branch and verify it's available. (CI checkout might be shallow.) |
| 116 | + _tip="$1" |
| 117 | + _base_rev=$(base_rev) |
| 118 | + if ! git merge-base "${_base_rev}" "${_tip}" 2> /dev/null |
| 119 | + then |
| 120 | + die 4 "Repository is incomplete, merge base between ${_base_rev} and ${_tip} is not available." |
| 121 | + fi |
| 122 | +} |
| 123 | + |
| 124 | +check_lockfile_updates() { |
| 125 | + _merge_base=$(merge_base HEAD) |
| 126 | + git diff "${_merge_base}..HEAD" --name-only > "${SCRATCH}/changed-files.txt" |
| 127 | + |
| 128 | + if grep -qxF -e 'uv.lock' -e '.build-constraints.txt' "${SCRATCH}/changed-files.txt" |
| 129 | + then |
| 130 | + # Lock-files changed: either pyproject.toml has to be changed, or nothing else is allowed. |
| 131 | + if grep -vxF -e 'uv.lock' -e '.build-constraints.txt' "${SCRATCH}/changed-files.txt" > "${SCRATCH}/other-files.txt" && ! grep -qxF 'pyproject.toml' "${SCRATCH}/other-files.txt" |
| 132 | + then |
| 133 | + printf "%s: Detected changes to uv.lock and/or .build-constraints.txt mixed with changes to:\n" "${PROGNAME}" |
| 134 | + sed -e 's/^/ /' "${SCRATCH}/other-files.txt" |
| 135 | + die 5 "Lock-files must be updated either in conjunction with dependencies in pyproject.toml or on their own." |
| 136 | + fi |
| 137 | + fi |
| 138 | +} |
| 139 | +check_lockfile_updates |
0 commit comments