mirror of
https://github.com/clearlinux/graphene.git
synced 2026-09-04 04:41:37 +00:00
Instead of implementing downloading of external resources in every Makefile again, use one script. This script adds the following features: - Always check the download against a known SHA-256 hash. - Support caching of downloaded resources (set DL_CACHE=/some/dir). - Allow offline builds if all files are cached. If DL_OFFLINE=true the build will never attempt to download anything.
79 lines
1.9 KiB
Bash
Executable File
79 lines
1.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -eu -o pipefail
|
|
|
|
declare -a urls
|
|
|
|
usage() {
|
|
echo "Usage: download --url https://example.org/test --url https://mirror.example/blah --output test.tar --sha256 1234..abc"
|
|
exit "${1:-0}"
|
|
}
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--url)
|
|
urls+=("$2")
|
|
shift
|
|
;;
|
|
--output)
|
|
output="$2"
|
|
shift
|
|
;;
|
|
--sha256)
|
|
sha256="$2"
|
|
shift
|
|
;;
|
|
--help|-h)
|
|
usage
|
|
;;
|
|
*)
|
|
usage 1
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if [ -z "${output:-}" ] || [ -z "${sha256:-}" ] || [ -z "${urls[*]}" ]; then
|
|
usage 1
|
|
fi
|
|
|
|
if [ -n "${DL_CACHE:-}" ]; then
|
|
if [ -f "$DL_CACHE/$sha256" ]; then
|
|
echo "download: Found '$output' (${sha256:0:8}...) in cache."
|
|
cp "$DL_CACHE/$sha256" "$output"
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
if [ "${DL_OFFLINE:-}" == "true" ]; then
|
|
echo "download: ERROR: File '$output' (${sha256:0:8}...) not found in cache and offline mode is active!"
|
|
exit 1
|
|
fi
|
|
|
|
tmpd="$(mktemp -d -p "${DL_CACHE:-.}")"
|
|
cleanup() {
|
|
rm -rf "$tmpd"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
for url in "${urls[@]}"; do
|
|
echo "download: Trying to fetch $url"
|
|
wget --timeout=10 -O "$tmpd/unverified" "$url" || true
|
|
sha256_received="$(sha256sum "$tmpd/unverified" | cut -d ' ' -f 1)"
|
|
if [ "$sha256" != "$sha256_received" ]; then
|
|
echo "download: WARNING: Hash mismatch: Expected $sha256 but received $sha256_received"
|
|
continue
|
|
fi
|
|
echo "download: Fetched '$output' (${sha256:0:8}...) successfully."
|
|
if [ -n "${DL_CACHE:-}" ]; then
|
|
mv "$tmpd/unverified" "$DL_CACHE/$sha256"
|
|
cp "$DL_CACHE/$sha256" "$output"
|
|
exit 0
|
|
fi
|
|
mv "$tmpd/unverified" "$output"
|
|
exit 0
|
|
done
|
|
|
|
echo "download: ERROR: Failed to download '$output' (${sha256:0:8}...)! No URLs left to try."
|
|
exit 1
|