diff options
| author | Danilo M. <danix@danix.xyz> | 2026-07-13 10:33:50 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-07-13 10:33:50 +0200 |
| commit | 5d1fb3ab2e027176dfd29e6bcc69d606aa550c2b (patch) | |
| tree | e564663957d18d4bee00cfb7f9496ac61b1bb687 | |
| parent | 368023455770538ad0f7568ee83cf4cef0a4eabc (diff) | |
| download | sbo-slackbuilds-5d1fb3ab2e027176dfd29e6bcc69d606aa550c2b.tar.gz sbo-slackbuilds-5d1fb3ab2e027176dfd29e6bcc69d606aa550c2b.zip | |
test-build: port dep cache, namespaced by image digest
| -rwxr-xr-x | .extras/test-build | 86 | ||||
| -rw-r--r-- | .extras/test-logic.sh | 26 |
2 files changed, 112 insertions, 0 deletions
diff --git a/.extras/test-build b/.extras/test-build index a7e796e..1f7920c 100755 --- a/.extras/test-build +++ b/.extras/test-build @@ -286,6 +286,92 @@ apply_overrides_to_order() { RESOLVED_ORDER=("${keep[@]:-}") } +# ============================================================================= +# Dependency cache. Layout: $CACHE_ROOT/<cat>/<prog>/<prog>-<ver>-...txz where +# CACHE_ROOT = $PKG_CACHE/<image-digest> (set per run by resolve_cache_root). +# Key is prog+version. --no-cache (USE_CACHE=0) or empty PKG_CACHE disables. +# ============================================================================= +CACHE_ROOT="" # set by resolve_cache_root once the image digest is known + +# True when the cache is usable this run. +_cache_on() { [[ $USE_CACHE -eq 1 && -n "$PKG_CACHE" && -n "$CACHE_ROOT" ]]; } + +_cache_ver_of() { + local prog="$1" base="$2" + base="${base#"$prog"-}" + echo "${base%%-*}" +} + +# cache_decision <cat> <prog> <version> -> cached | bump:OLD:NEW | new +cache_decision() { + local cat="$1" prog="$2" version="$3" + _cache_on || { echo new; return; } + local dir="$CACHE_ROOT/$cat/$prog" + local f newest="" + for f in "$dir/$prog"-*.t?z; do + [[ -e "$f" ]] || continue + [[ -z "$newest" || "$f" -nt "$newest" ]] && newest="$f" + done + [[ -z "$newest" ]] && { echo new; return; } + local have; have="$(_cache_ver_of "$prog" "$(basename "$newest")")" + if [[ "$have" == "$version" ]]; then echo cached; else echo "bump:$have:$version"; fi +} + +cache_path() { + local cat="$1" prog="$2" version="$3" + _cache_on || return + local dir="$CACHE_ROOT/$cat/$prog" + local f newest="" + for f in "$dir/$prog"-*.t?z; do + [[ -e "$f" ]] || continue + [[ -z "$newest" || "$f" -nt "$newest" ]] && newest="$f" + done + [[ -z "$newest" ]] && return + [[ "$(_cache_ver_of "$prog" "$(basename "$newest")")" == "$version" ]] && echo "$newest" +} + +cache_store() { + local cat="$1" prog="$2" src="$3" + _cache_on || return + local dir="$CACHE_ROOT/$cat/$prog" + mkdir -p "$dir" + rm -f "$dir"/*.t?z + cp -a "$src" "$dir/" +} + +cache_label() { + local dir="$1" is_target="$2" + local cat prog ver dec + cat="$(category_of "$dir")"; prog="$(basename "$dir")"; ver="$(version_of "$dir")" + dec="$(cache_decision "$cat" "$prog" "$ver")" + local label + case "$dec" in + cached) label="cached ($ver)" ;; + bump:*) label="rebuild: ${dec#bump:}"; label="${label/:/ -> }" ;; + *) label="build (new)" ;; + esac + if [[ "$is_target" == "1" ]]; then + case "$dec" in + cached) label="build (cached $ver, rebuilt as target)" ;; + esac + echo "target, $label" + else + echo "$label" + fi +} + +# Compute CACHE_ROOT from the image's digest. Falls back to the tag if the +# digest cannot be read (still isolates per image reference). +resolve_cache_root() { + [[ -z "$PKG_CACHE" ]] && { CACHE_ROOT=""; return; } + local digest + digest="$(docker image inspect --format '{{index .Id}}' "$ACTIVE_IMAGE" 2>/dev/null)" + [[ -z "$digest" ]] && digest="tag-${ACTIVE_IMAGE//[^a-zA-Z0-9._-]/_}" + digest="${digest//[^a-zA-Z0-9._-]/_}" + CACHE_ROOT="$PKG_CACHE/$digest" + mkdir -p "$CACHE_ROOT" +} + main() { parse_args "$@" init_color diff --git a/.extras/test-logic.sh b/.extras/test-logic.sh index e48586e..d4249bb 100644 --- a/.extras/test-logic.sh +++ b/.extras/test-logic.sh @@ -107,6 +107,32 @@ load_overrides [[ ${#OV_DROP[@]} -eq 0 ]] && ok "overrides inert on 15.0" || bad "overrides applied on 15.0" VERSION_ID="current" +# --- cache ------------------------------------------------------------------ +PKG_CACHE=$(mktemp -d) +CACHE_ROOT="$PKG_CACHE/sha256-deadbeef" # simulate a resolved digest namespace +mkc() { mkdir -p "$CACHE_ROOT/$1/$2"; : > "$CACHE_ROOT/$1/$2/$3"; } + +mkc net libfoo "libfoo-1.1-x86_64-1_danix.txz" +[[ "$(cache_decision net libfoo 1.1)" == "cached" ]] && ok "cache hit on version match" || bad "cache_decision got [$(cache_decision net libfoo 1.1)]" +[[ "$(cache_decision net libfoo 1.2)" == "bump:1.1:1.2" ]] && ok "cache bump reported" || bad "cache bump got [$(cache_decision net libfoo 1.2)]" +[[ "$(cache_decision net libbar 1.0)" == "new" ]] && ok "cache new for absent" || bad "cache new got [$(cache_decision net libbar 1.0)]" + +hit="$(cache_path net libfoo 1.1)" +[[ "$hit" == "$CACHE_ROOT/net/libfoo/libfoo-1.1-x86_64-1_danix.txz" ]] && ok "cache_path returns hit" || bad "cache_path got [$hit]" +[[ -z "$(cache_path net libfoo 9.9)" ]] && ok "cache_path empty on miss" || bad "cache_path not empty on miss" + +srctmp=$(mktemp -d); : > "$srctmp/libfoo-1.2-x86_64-1_danix.txz" +cache_store net libfoo "$srctmp/libfoo-1.2-x86_64-1_danix.txz" +count=$(find "$CACHE_ROOT/net/libfoo" -name '*.t?z' | wc -l) +[[ "$count" -eq 1 ]] && ok "cache_store evicts to one file" || bad "cache_store left $count files" +[[ -e "$CACHE_ROOT/net/libfoo/libfoo-1.2-x86_64-1_danix.txz" ]] && ok "cache_store stored new file" || bad "cache_store did not store" +rm -rf "$srctmp" + +# disabled cache -> new +CACHE_ROOT_SAVE="$CACHE_ROOT"; USE_CACHE=0 +[[ "$(cache_decision net libfoo 1.2)" == "new" ]] && ok "--no-cache disables (new)" || bad "disabled cache got [$(cache_decision net libfoo 1.2)]" +USE_CACHE=1; CACHE_ROOT="$CACHE_ROOT_SAVE" + echo echo "$pass passed, $fail failed" [[ $fail -eq 0 ]] || exit 1 |
