Files
bananarchy/scripts/sync-prebuilt-overlay.sh
T

83 lines
2.1 KiB
Bash
Raw Normal View History

2026-11-05 13:40:53 +02:00
#!/bin/sh
# Keep the tracked overlay/rootfs snapshot in sync with prebuilt/ artifacts.
# prebuilt/ is the single source of truth; this script mirrors every artifact
# that also exists in overlay/rootfs back into the overlay.
set -eu
BASE_DIR="$(cd "$(dirname "$0")/.." && pwd)"
CHECK_ONLY=0
for arg in "$@"; do
case "$arg" in
--check|-c)
CHECK_ONLY=1
;;
*)
echo "Usage: $0 [--check]" >&2
exit 1
;;
esac
done
python3 - "$BASE_DIR" "$CHECK_ONLY" <<'PYEOF'
import hashlib
import os
import shutil
import sys
base = sys.argv[1]
check_only = sys.argv[2] == "1"
prebuilt_root = os.path.join(base, "prebuilt")
overlay_root = os.path.join(base, "overlay", "rootfs")
def digest(path):
with open(path, "rb") as handle:
return hashlib.sha256(handle.read()).hexdigest()
def regular_files(root):
result = []
for parent, _, names in os.walk(root):
for name in names:
path = os.path.join(parent, name)
if not os.path.islink(path) and os.path.isfile(path):
result.append(path)
return result
by_name = {}
for path in regular_files(prebuilt_root):
by_name.setdefault(os.path.basename(path), []).append(path)
drift = []
matched = 0
for target in regular_files(overlay_root):
rel = os.path.relpath(target, overlay_root)
candidates = by_name.get(os.path.basename(target), [])
source = None
for candidate in candidates:
stripped = os.path.relpath(candidate, prebuilt_root).split(os.sep, 1)[-1]
if stripped == rel:
source = candidate
break
if source is None and candidates:
source = candidates[0]
if source is None:
continue
matched += 1
if digest(source) == digest(target):
continue
drift.append((source, target))
if not check_only:
shutil.copy2(source, target)
if check_only:
for source, target in drift:
print("out of sync: " + os.path.relpath(target, base))
print("artifact drift: %d of %d mirrored file(s)" % (len(drift), matched))
sys.exit(1 if drift else 0)
print("synced %d artifact(s) into overlay/rootfs; %d already in sync" % (
len(drift), matched - len(drift)))
PYEOF